简体   繁体   English

Kotlin:通过单击更改 RecyclerView 项目的背景颜色

[英]Kotlin:change background color of RecyclerView item by click on it

I want to change background color of recycler view row(item) by clicking on it, i use kotlin.我想通过单击更改回收站视图行(项目)的背景颜色,我使用 kotlin。
i tried different methods,The item that is clicked changed background color correctly, but when I scroll down I see that another item has also changed background color.我尝试了不同的方法,单击的项目正确更改了背景颜色,但是当我向下滚动时,我看到另一个项目也更改了背景颜色。

In fact, for every click on the item, another item in the part of the list that is not in the user's view is also affected.事实上,对于该项目的每次点击,列表中不在用户视图中的部分中的另一个项目也会受到影响。

Please explain this problem with a complete example.请用一个完整的例子来解释这个问题。
Please be sure to test it yourself请务必自行测试
I really need to fix this我真的需要解决这个问题

Thanks a lot非常感谢

it's quite easy to do:) You need to just remember how the RecyclerView works:这很容易做到:) 你只需要记住 RecyclerView 是如何工作的:

  1. It creates few ViewHolders它创建了几个 ViewHolders
  2. When you scroll your list, the ViewHolders are reused.当您滚动列表时,会重复使用 ViewHolders。
  3. If you have for example 100 items on the list, it will create 5-6 ViewHolders and will reuse them.例如,如果列表中有 100 个项目,它将创建 5-6 个 ViewHolders 并重复使用它们。

So having that in mind, when onBindViewHolder() is called, the easiest thing you can do is to check some state of the item, and then choose background color of the item.因此请记住,当onBindViewHolder()时,您可以做的最简单的事情是检查项目的一些 state,然后选择项目的背景颜色。 In this scenario you can select/deselect multiple items in the list.在这种情况下,您可以选择/取消选择列表中的多个项目。 If you want to have only one checked, you will need to change the items.map {} function a little bit:如果您只想检查一项,则需要稍微更改items.map {} function:

Please keep in mind that I wrote below from my head, you will also need to override some more functions in adapter etc.请记住我在下面写的,您还需要覆盖适配器等中的更多功能。

class MyAdapter(private val onItemClick: (YouItem) -> Unit): RecyclerView.Adapter() {

    private var items: List<YourItem>

    fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val payload = items[holder.adapterPosition]
        holder.itemView.setOnClickListener {
            onItemClick(payload)
        }
        holder.itemView.background = if(payload.someState) firstBackground else otherBackground
    }

    fun updateItems(updatedItems: List<YourItem>) {
        items = updatedItems
        notifyDataSetChanged()
    }
}
class YourActivity: Activity() {

    private lateinit var items: List<YourItem>

    fun onCreate(savedInstanceState: Bundle) {
         super.onCreate(savedInstanceState)
         ...
         items: List<YourItem> = getItemsFromSomewhere()
         val adapter = MyAdapter { clickedItem ->
             val updatedItems = items.map { 
                 if(it == clickedItem) {
                     it.copy(someState = !it.someState)
                 } else {
                     it
                 }
             items = updatedItems
             adapter.updateItems(updatedItems)
         }
    }
}
data class YourItem(val someState: Boolean)

This is mostly because of RecyclerView reuses the view(item row) so that the view need not to be generated multiple times.这主要是因为 RecyclerView 重用了视图(项目行),因此不需要多次生成视图。 you can solve the problem by setting the background only for a particular item by changing the value of the the object and for all others keep it default value.您可以通过更改 object 的值来仅为特定项目设置背景来解决问题,对于所有其他项目,请保持默认值。

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    if(position == selected_item) {
        holder.view.setBackgroundColor(Color.parseColor("#00aaff"));
    } else {
        holder.view.setBackgroundColor(Color.parseColor("#00000")); //actually you should set to the normal text color
    }
}

or或者

public void onItemClick() {
         Model model = itemList.get(clickedPosition);
         model.setBgColor = Color.parseColor("#00aaff");
    }

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Model model = itemList.get(position);
    holder.view.setBackgroundColor(model.getBgColor());
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM