1

enter image description here

The picture is when I clicked 1.

I want to change the background of other items when clicking the recyclerview item

But looking at my code and thinking, I can't change other holder items when I click the itemview

I did a Google search, but I couldn't find an answer because I couldn't find the right keyword.

It would be a great help if you give me an answer

first my recyclerview Adapter

class GiftShowCategoryAdapterHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.item_giftshow_category, parent, false)
    ) {
        fun onBind(item: ArrayList<GiftCardResponse.brandCategories>, viewModel: GiftShowViewModel?, position: Int, holder: RecyclerView.ViewHolder) {
            itemView.run {
                val displaymetrics = DisplayMetrics()
                (context as Activity).windowManager.defaultDisplay.getMetrics(displaymetrics)
                val devicewidth: Int = displaymetrics.widthPixels / 4
                val deviceheight: Int = displaymetrics.heightPixels / 8

                itemView.iv_giftshow_category.layoutParams.width = devicewidth
                itemView.iv_giftshow_category.layoutParams.height = deviceheight

                Glide.with(this).load(item[position].categoryIcon).error(R.drawable.choice_cash)
                    .into(iv_giftshow_category)

                tv_giftshow_category.text = item[position].categoryName

                this.background = context.getDrawable(R.drawable.shape_gray_gift_recycler_stroke)

                setOnClickListener {
                    for(i in 0 until item.size){
                        if(i == position){
                            Timber.d("Checked i $i , position $position")
                            linear_background.background = context.getDrawable(R.drawable.shape_white_gift_recycler_stroke)
                            //it.background = context.getDrawable(R.drawable.shape_white_gift_recycler_stroke)
                        }else{
                            Timber.d("Checked else i $i , position $position")
                        }
                    }
                    viewModel?.getBrandItemsResult(item[position].categorySeq)
                }
            }
        }
    }

    private var giftShowCategoryList = ArrayList<GiftCardResponse.brandCategories>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = GiftShowCategoryAdapterHolder(parent);

    override fun getItemCount() = giftShowCategoryList.size

    private var viewModel: GiftShowViewModel? = null

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as? GiftShowCategoryAdapterHolder)?.onBind(giftShowCategoryList, viewModel, position, holder)
    }

    fun addItem(items: List<GiftCardResponse.brandCategories>) {
        giftShowCategoryList = items as ArrayList<GiftCardResponse.brandCategories>
    }

    fun addViewModel(viewModel: GiftShowViewModel) {
        this.viewModel = viewModel
    }
Duseop
  • 137
  • 2
  • 11

3 Answers3

0

I put oldPosition and newPosition in the adapter, compare it with the current position, pass the click event to the viewmodel, observe the viewmodel livedata, and replace the data with only the corresponding value in the adapter.

Duseop
  • 137
  • 2
  • 11
0

You can do this on you onBindViewHolder Method:

    if(position == selected){
        holder.layout.setBackground(mContext.getDrawable(R.drawable.selected_layout));
    }else {
        holder.layout.setBackground(mContext.getDrawable(R.drawable.layout_background));
    }


    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selected = holder.getAdapterPosition();
            notifyDataSetChanged();
        }
    });
-1
 if((position % 2 == 0)){
       holder.cardView.setCardBackgroundColor(R.color.list_even_color);
 }else{
     holder.cardView.setCardBackgroundColor(R.color.list_odd_color);
}
Rinkal Patel
  • 156
  • 3