[英]RecyclerView lost its item position when scrolled
I'm currently working on RecyclerView
implementation on my project.我目前正在我的项目中实现
RecyclerView
。 When I set item on RecyclerView
and scroll, I lost its position.当我在
RecyclerView
上设置 item 并滚动时,我失去了它的位置。
I Use Some if and else condition to click and change the color of text but its change when I'll not scroll the RecyclerView
item.我使用一些 if 和 else 条件来单击并更改文本的颜色,但是当我不滚动
RecyclerView
项目时它会发生变化。 Please suggest me the right way how to handle this condition.请建议我如何处理这种情况的正确方法。
adapter.setOnItemClickListner(new LoadVehicleTypeAdapter.setOnitemclick() {
@Override
public void ImageClick(int position, String Name,String Description,int id) {
LinearLayout linearLayout = null;
CustomTextView customTextView = null;
try {
for (int i = 0; i < messages.size(); i++) {
linearLayout = (LinearLayout) rvVehicleTypes.getChildAt(i).findViewById(R.id.root1);
customTextView = (CustomTextView) rvVehicleTypes.getChildAt(i).findViewById(R.id.frag_cartypes_inflated_name);
if (i==position) {
linearLayout.setBackgroundColor(Color.parseColor("#999999"));
customTextView.setTextColor(Color.parseColor("#FFFFFF"));
} else {
linearLayout.setBackgroundColor(Color.parseColor("#f3f3f3"));
customTextView.setTextColor(Color.parseColor("#2196F3"));
}
}
//Toast.makeText(getActivity(),Name, Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
}
}
});
Seems you are new to the Android development world, with this assumption let me take you through the journey of ListView to RecyclerView.看来您是 Android 开发世界的新手,有了这个假设,让我带您完成从 ListView 到 RecyclerView 的旅程。
ListView, we were used to follow ViewHolder pattern to manage items being recycled when you scroll the listview. ListView,我们习惯于遵循 ViewHolder 模式来管理滚动列表视图时回收的项目。
Now RecyclerView have that functionality in-built, not in-built but you have to implement it forcefully, I am talking about onCreateViewHolder
and onBindViewHolder
.现在 RecyclerView 具有内置功能,而不是内置功能,但您必须强制实施它,我说的是
onCreateViewHolder
和onBindViewHolder
。
So now let's take one step ahead in understanding how it works, as soon as you start scrolling down the list, inflated item views start getting recycled, with that your previously inflated ViewHolder (which gets actually created in onCreateViewHolder) is reused.所以现在让我们先一步了解它是如何工作的,一旦你开始向下滚动列表,膨胀的项目视图就会开始被回收,你之前膨胀的 ViewHolder(实际上是在 onCreateViewHolder 中创建的)被重用。
Now to solve this issue, you have to remember the position and for the same most of us used to use SparseBooleanArray
现在要解决这个问题,你必须记住位置,因为我们大多数人曾经使用过
SparseBooleanArray
Please read the documents of RecyclerView
.请阅读
RecyclerView
的文档。 Also I encourage you to read difference between ListView
and RecyclerView
我也鼓励你阅读
ListView
和RecyclerView
之间的区别
Are you using RecyclerView
or ListView
?您使用的是
RecyclerView
还是ListView
? From your code it seems that you are using ListView
.从您的代码看来,您正在使用
ListView
。
If you want to use RecyclerView
please go through the tutorials.如果您想使用
RecyclerView
请阅读教程。
Please go through this tutorial.请阅读本教程。 It will tell you how to use RecyclerView.
它会告诉你如何使用 RecyclerView。 For click listener follow this tutorial.
对于单击侦听器,请遵循本教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.