简体   繁体   English

Android 中的回收视图和查看器适配器的错误

[英]Bug with the recycle view and the viewholder adapater in Android

I am getting a very strange bug in my application.我的应用程序中出现了一个非常奇怪的错误。 To make it more clear I will create a similar example with minus code.为了更清楚,我将创建一个带有减号的类似示例。

I am reading objects from Firebase Realtime Database with an addListenerForSingleValueEvent .我正在使用addListenerForSingleValueEventFirebase 实时数据库读取对象。 While I am reading the objects, I am stored them in an Array that I passed to an Adapter in a Recycleview .在读取对象时,我将它们存储在一个数组中,该数组传递给了Recycleview中的适配器 At this point, I can say, after debugging, that all seems to work correctly.在这一点上,我可以说,经过调试,一切似乎都正常工作。

Then in the Adapter, I have a code similar to this:然后在适配器中,我有类似这样的代码:

public class AdapterObject extends RecyclerView.Adapter<AdapterObject.ViewHolder> {

    ArrayList<Object> objectList;
    Context mContext;

    public AdapterObject (Context context, ArrayList<Object> objectList){
        this.mContext = context;
        this.objectList = objectList;
    }

    @NonNull
    @Override
    public AdapterObject.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.object_grid_layout, parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterObject.ViewHolder holder, int position) {
        Object o = objectList.get(position);
        Log.d("TAG", o.getAtribute());
        if (o.getAtribute().equals("A")){
            holder.atribute.setVisibility(View.VISIBLE);
        }

    @Override
    public int getItemCount() {
        return objectList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView atribute;

        boolean favorite;
        String descuento, precioOriginal;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            atribute = itemView.findViewById(R.id.atribute);
        }
    }
}

As you can see in the code if the current object has the attribute value == "A" , then his Textview is displayed, otherwise, the Textview remains hidden.如您在代码中所见,如果当前 object 具有属性值 == "A" ,则显示他的Textview ,否则, Textview 将保持隐藏状态。

All seems correct when I debug it because the objects and their attribute corresponds to the Database, but when I deploy the application in the Android simulator and I start going up and down on the Recycleview , the holders start to display the Textviews although the console debugs seems correct...当我调试它时一切似乎都是正确的,因为对象及其属性对应于数据库,但是当我在 Android 模拟器中部署应用程序并开始在Recycleview上上下移动时,持有者开始显示Textviews尽管控制台调试似乎正确...

Is this normal in RecycleViews?这在 RecycleViews 中正常吗? How can I fix that?我该如何解决? I have found this , do you think it has any relation?我找到了这个,你认为它有什么关系吗?

This is an extract from the RecyclerView documentation这是RecyclerView 文档的摘录

As the name implies, RecyclerView recycles those individual elements.顾名思义,RecyclerView 回收那些单独的元素。 When an item scrolls off the screen, RecyclerView doesn't destroy its view.当一个项目滚出屏幕时,RecyclerView 不会破坏它的视图。 Instead, RecyclerView reuses the view for new items that have scrolled onscreen.相反,RecyclerView 将视图重用于在屏幕上滚动的新项目。 This reuse vastly improves performance, improving your app's responsiveness and reducing power consumption.这种重用极大地提高了性能,提高了应用程序的响应能力并降低了功耗。

That means that when the view gets reused it will keep the current properties.这意味着当视图被重用时,它将保留当前属性。 It's up to you to change them when onBindViewHolder gets called.onBindViewHolder被调用时,由你来改变它们。

In your specific case在您的具体情况下

@Override
public void onBindViewHolder(@NonNull AdapterObject.ViewHolder holder, int position) {
    Object o = objectList.get(position);
    Log.d("TAG", o.getAtribute());
    if (o.getAtribute().equals("A")){
        holder.atribute.setVisibility(View.VISIBLE);
    } else. {
        holder.atribute.setVisibility(View.GONE);
    }
  }

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

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