【问题标题】:how to android recyclerview change different holder view background如何 android recyclerview 更改不同的持有者视图背景
【发布时间】:2021-12-04 16:22:35
【问题描述】:

图片是我点击1的时候。

我想在点击recyclerview项目时改变其他项目的背景

但是看我的代码思考一下,点击itemview的时候不能改变其他的holder item

我进行了谷歌搜索,但由于找不到正确的关键字,我找不到答案。

如果你能给我一个答案,那将是一个很大的帮助

首先是我的 recyclerview 适配器

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
    }

【问题讨论】:

  • 你想说的是,如果你点击特定的项目,点击的项目背景应该改变吗??
  • 不,这是您点击的另一个视图
  • “如何让 android recyclerview 更改不同的持有者视图背景”是什么意思
  • 你能解释清楚吗,这样我就能明白你到底想达到什么目的......
  • 您想要实现的目标的直观表示可能会有所帮助

标签: android kotlin android-recyclerview android-viewholder


【解决方案1】:
 if((position % 2 == 0)){
       holder.cardView.setCardBackgroundColor(R.color.list_even_color);
 }else{
     holder.cardView.setCardBackgroundColor(R.color.list_odd_color);
}

【讨论】:

  • 如果你回答这个问题,你就会知道只有你拥有的持有人在改变。
【解决方案2】:

我把oldPosition和newPosition放在adapter里面,和当前位置比较,把click事件传给viewmodel,观察viewmodel的livedata,只用adapter里面对应的值替换数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多