0

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

enter image description here

Sumit Shukla
  • 3,134
  • 4
  • 26
  • 33

3 Answers3

0

This can be done in this way

  1. Create ArrayList with a custom object that contains the image URL
  2. Add only one object on the ArrayList
  3. Set the adapter to show from ArrayList
  4. In the recycler view's bind view holder, if url not exist then show addimageLogo
  5. Then on the click on the image add an image(Path/URL) on the object at the position
  6. Once an image is added on the object, then check if the last object has the URL then again point 2
  7. 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

You need to set by default the add Image Logo to the ImageView.Inside the adapter add an OnClickListener() to the ImageView and write the code to open the Gallery for getting the Images, this will help you.

Vikas
  • 116
  • 5
0
  1. Add a viewType variable to your data class. In your case you could use 'Button' and 'Image'.
  2. Set the first object's viewType value as 'Button'
  3. 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