简体   繁体   English

滚动时recylerview中的位置发生变化

[英]Position in recylerview changes while scrolling

Seems like position in recylerview changes while scrolling. 看起来像滚动时recylerview中的位置变化。

What I want to do is like this. 我想做的就是这样。

Adapter.java

@Override
public void onBindViewHolder(aViewHolder holder, int position) {

    if (position == 0) {
        holder.zeroIcon.setVisibility(View.VISIBLE);
    } else if (position == 1) {
        holder.oneIcon.setVisiblity(View.VISIBLE);
    } else {
        holder.otherIcon.setVisiblity(View.VISIBLE);
    }

    // Set text on each item
    ...
}


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

public class aViewHolder extends RecyclerView.ViewHolder {

    private ImageView zeroIcon;
    private ImageView oneIcon;
    private ImageView otherIcon;


    public aViewHolder(View itemView) {
        super(itemView);
        zeroIcon = itemview.findViewById(...);
        ...
    }
}

I set these icon's visibility GONE as default in xml file. 我将这些图标的可见性GONE设置为xml文件中的默认值。

When I see the recylerview at first, the icons show up as I expected depending on its position. 当我第一次看到recylerview时,图标会根据其位置显示出我的预期。

However, when I scroll down and scroll up, incorrect icons also show up on incorrect position. 但是,当我向下滚动并向上滚动时,不正确的图标也会显示在错误的位置上。 Like otherIcon shows up on the first and second item while scrolling down and up. 就像otherIcon在向下和向上滚动时显示在第一个和第二个项目上一样。 While scrolling down, zeroIcon and oneIcon show up on some other items. 向下滚动时, zeroIcononeIcon显示在其他一些项目上。

How can I fix this? 我怎样才能解决这个问题?

list_item.xml is like this. list_item.xml是这样的。

<RelativeLayout ...>

     <ImageView
         android:id="@+id/zero"
         android:visiblity="gone"
         android:background="@drawable/zero" />

     <ImageView
         android:id="@id/one"
         android:visiblity="gone"
         android:background="@drawable/one" />

     <ImageView
         android:id="@id/other"
         android:visiblity="gone"
         android:background="@drawable/other" />

Modify it in this way, 以这种方式修改它,

if (position == 0) {
    holder.zeroIcon.setVisibility(View.VISIBLE);
    holder.otherIcon.setVisiblity(View.GONE);
    holder.oneIcon.setVisiblity(View.GONE);
} else if (position == 1) {
    holder.oneIcon.setVisiblity(View.VISIBLE);
    holder.zeroIcon.setVisibility(View.GONE);
    holder.otherIcon.setVisiblity(View.GONE);
} else {
    holder.otherIcon.setVisiblity(View.VISIBLE);
    holder.oneIcon.setVisiblity(View.GONE);
    holder.zeroIcon.setVisibility(View.GONE);
}

In RecyclerView you should manage other views also while changing an item. RecyclerView您还应该在更改项目时管理其他视图。

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

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