Home > OS >  how to android recyclerview change different holder view background
how to android recyclerview change different holder view background

Time:10-18

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
    }

CodePudding user response:

 if((position % 2 == 0)){
       holder.cardView.setCardBackgroundColor(R.color.list_even_color);
 }else{
     holder.cardView.setCardBackgroundColor(R.color.list_odd_color);
}

CodePudding user response:

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.

  • Related