通过 DiffUtil 更改卡片视图的背景颜色

问题描述 投票:0回答:1

美好时光。使用DiffUtil,我想在单击任何项目时更改所选文本和卡片视图的颜色。我写的代码是这样的,但似乎notifyItemChanged已经过期了。感谢您提供更多最新信息。帮助我编写更高效的代码。


class CategoriesAdapter @Inject constructor(@ApplicationContext private val context: Context) :
    RecyclerView.Adapter<CategoriesAdapter.ViewHolder>() {
    //Binding
    private lateinit var binding: ItemCategoriesBinding
    private var items = emptyList<ResponseCategoriesItem>()
    private var selectedItemPosition: Int = -1
    private var lastItemSelectedPos = -1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        binding = ItemCategoriesBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder()
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(items[position])

        ///////
        if (position == selectedItemPosition) holder.selectedBg() else holder.defaultBg()

        holder.itemView.setOnClickListener {
            selectedItemPosition = holder.bindingAdapterPosition
            lastItemSelectedPos = if (lastItemSelectedPos == -1) selectedItemPosition else {
                notifyItemChanged(lastItemSelectedPos)
                selectedItemPosition
            }
            notifyItemChanged(selectedItemPosition)

        }
    }

    override fun getItemCount() = items.size

    override fun getItemViewType(position: Int) = position

    override fun getItemId(position: Int) = position.toLong()

    inner class ViewHolder :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(item: ResponseCategoriesItem) {
            //initViews
            binding.apply {
                titleCategories.text = item.title
                //card-view

                //Click
                root.setOnClickListener {
                    onItemClickListener?.let {
                        it(item.id!!, item.title!!)
                    }
                }
            }

        }

        fun defaultBg() {
            binding.apply {
                bgCard.setCardBackgroundColor(ContextCompat.getColor(context, R.color.Bright_Gray))
                titleCategories.setTextColor(ContextCompat.getColor(context, R.color.Raisin_Black))
            }
        }

        fun selectedBg() {
            binding.apply {
                bgCard.setCardBackgroundColor(ContextCompat.getColor(context, R.color.Raisin_Black))
                titleCategories.setTextColor(ContextCompat.getColor(context, R.color.white))
            }

        }


    }

    private var onItemClickListener: ((String, String) -> Unit)? = null

    fun setOnItemClickListener(listener: (String, String) -> Unit) {
        onItemClickListener = listener
    }

    fun setData(data: List<ResponseCategoriesItem>) {
        val adapterDiffUtils = BaseDiffUtils(items, data)
        val diffUtils = DiffUtil.calculateDiff(adapterDiffUtils)
        items = data
        diffUtils.dispatchUpdatesTo(this)
    }

}

xml代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bg_card"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/_2mdp"
    android:layout_marginTop="@dimen/_2mdp"
    android:layout_marginHorizontal="@dimen/_6mdp"
    app:cardBackgroundColor="@color/Bright_Gray"
    app:cardCornerRadius="@dimen/_10mdp"
    app:contentPadding="@dimen/_8mdp">
    <!--title-->
    <TextView
        android:id="@+id/title_Categories"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:textAppearance="@style/TextAppearance_medium"
        android:textColor="@color/Raisin_Black"
        tools:text="popular" />

</androidx.cardview.widget.CardView>

我想要的用户界面

android kotlin android-layout android-recyclerview
1个回答
0
投票

ResponseCategoriesItem 是我认为的数据类,您可以使用 ResponseCategoriesItem 数据类添加一个变量 isSelected ,默认情况下所有变量均为 false。并单击特定项目,在此位置执行 isSelected true 。然后调用NotifyDataSetChanged();

© www.soinside.com 2019 - 2024. All rights reserved.