简体   繁体   English

RecyclerView 在滚动时丢失了它的项目位置

[英]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 具有内置功能,而不是内置功能,但您必须强制实施它,我说的是onCreateViewHolderonBindViewHolder

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我也鼓励你阅读ListViewRecyclerView之间的区别

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.

相关问题 滚动到特定位置时,Recyclerview崩溃 - Recyclerview crashed when scrolled to particular position 添加项目时,如何防止recyclerview自动滚动到最后一个项目的位置? - How to prevent a recyclerview from getting automatically scrolled to the last item's position when items are added to it? Android - 为什么在滚动 RecyclerView 时丢失值 EditText? - Android - why lost values EditText when scrolled RecyclerView? 获取点击的项目及其在 RecyclerView 中的位置 - Get clicked item and its position in RecyclerView 如何检测 RecyclerView 何时滚动到最顶部位置 - How to detect when RecyclerView is scrolled to most top position 在带有mysqlite数据库的RecyclerView中滚动时,切换按钮更改了其状态 - toggle button changed its state when scrolled in RecyclerView with mysqlite database 滚动视图android时,项目动画停止在recyclerview中 - Item animation stopping in recyclerview when scrolled off the view android 如何检测用户何时滚动到RecyclerView中的最顶层项目 - How to detect when user have scrolled to the top most item in a RecyclerView 滚动到视图之外时,将项目的状态保存在recyclerview中 - save state of an item in recyclerview when scrolled out of the view 触摸时如何获取recyclerView项的位置 - how to get the position of an Item of the recyclerView when touched
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM