I want to make recyclerview first item as imageview with add image logo and after click it choose an image from gallery and set as background to the image view and the background to the second item be add an image logo
Asked
Active
Viewed 1,282 times
0
-
This question is too broad. – Kaushik Burkule Sep 19 '19 at 12:08
-
Possible duplicate of [How to create RecyclerView with multiple view type?](https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type) – denvercoder9 Sep 19 '19 at 12:38
3 Answers
0
This can be done in this way
- Create ArrayList with a custom object that contains the image URL
- Add only one object on the ArrayList
- Set the adapter to show from ArrayList
- In the recycler view's bind view holder, if url not exist then show addimageLogo
- Then on the click on the image add an image(Path/URL) on the object at the position
- Once an image is added on the object, then check if the last object has the URL then again point 2
- notifydataSetChanged()
Custom Object
public class URLContainer{
public URLContainer(String url, String imageName) {
this.url = url;
this.imageName = imageName;
}
String url;
String imageName;// you can use other required properties if you want
}
ArrayList
private ArrayList<URLContainer> images = new ArrayList<>();
For Adding Single Object
private void addSingleContainer(){
list.add(new URLContainer("",""));
}
Lakhwinder Singh
- 6,145
- 4
- 22
- 39
0
- Add a viewType variable to your data class. In your case you could use 'Button' and 'Image'.
- Set the first object's viewType value as 'Button'
- Use Multiple ViewHolder method to implement Recyclerview
Override getItemViewType
override fun getItemViewType(position: Int): Int {
return when (orders[position].viewType) {
ViewType.Button-> 1
ViewType.Image-> 2
else -> 1
}
}
onCreateViewHolder
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val viewHolder: RecyclerView.ViewHolder
when (viewType) {
1 -> {
val buttonBinding = DataBindingUtil.inflate<ItemButtonBinding>(
LayoutInflater.from(parent.context),
R.layout.item_button, parent, false
)
viewHolder = ButtonViewHolder(buttonBinding .root)
}
2 -> {
val imageBinding = DataBindingUtil.inflate<ItemImageBinding>(
LayoutInflater.from(parent.context),
R.layout.item_image, parent, false)
viewHolder = ImageViewHolder(imageBinding .root)
}
else -> {
val imageBinding = DataBindingUtil.inflate<ItemImageBinding>(
LayoutInflater.from(parent.context),
R.layout.item_image, parent, false)
viewHolder = ImageViewHolder(imageBinding .root)
}
}
return viewHolder
}
onBindViewHolder
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder.itemViewType) {
1 -> {
val buttonViewHolder = holder as ButtonViewHolder
configureButtonViewHolder(buttonViewHolder , position)
}
2 -> {
val imageViewHolder = holder as ImageViewHolder
configureImageViewHolder(imageViewHolder , position)
}
else -> {
val imageViewHolder = holder as ImageViewHolder
configureImageViewHolder(imageViewHolder , position)
}
}
}
Set OnClickListener on Button ItemView in Button ViewHolder class and place the image picker intent codes inside OnClickListener
Refer this answer
Aravinth Velusamy
- 140
- 2
- 8
-
Thank you very much it very useful but i am use java can you write code in java – Malek Alzein Sep 19 '19 at 14:24