简体   繁体   English

如何仅在 Recylerview 中更改最后一项的背景

[英]how to change Background of the last item only in Recylerview

I am trying to change the background in the last item, but I encountered a strange problem, When making a condition, I put the last position I find that the result changes the background in the last element and another element!我正在尝试改变最后一项的背景,但是遇到了一个奇怪的问题,在做条件的时候,我把最后一个position 发现结果改变了最后一个元素和另一个元素的背景!

All I want is to change the background in the last element only我想要的只是改变最后一个元素的背景

This my code这是我的代码

        if(position==(getItemCount()-1)) {

        holder.coin.setBackgroundResource(R.drawable.coin0);

       }

I think the problem is like,我认为问题是,

  1. You set background for the last element您为最后一个元素设置背景
  2. Then you click another and change background for the last element然后单击另一个并更改最后一个元素的背景

But then the last to last element's background is also there.但是最后一个元素的背景也在那里。 If that's the problem then here is the solution.如果这是问题,那么这里就是解决方案。

Before setting background do this in ViewHodler Class in the recycler view class: In the onClick, do在设置背景之前,在回收站视图 class 中的 ViewHodler Class 中执行此操作:在 onClick 中,执行

notifyItemChanged(positionOfLastToLastElement); 

then set the background in this way you will have only one item with that background.然后以这种方式设置背景,您将只有一个具有该背景的项目。

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

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