RecyclerView适配器更改列表滚动中的选定位置

时间:2016-01-31 08:40:23

标签: android android-recyclerview recycler-adapter

RecyclerView.Adapter我需要在每个行列表中更改favIcon,在选择每个位置后,只需指定行favIcon更改。

一切都是正确的但滚动RecyclerView时,所选行的位置会自动更改!

它是我的适配器类:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ItemData[] itemsData;
Activity activity;

public MyAdapter(Activity activity, ItemData[] itemsData) {
    this.activity=activity;
    this.itemsData = itemsData;
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    View itemLayoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_layout, null);

    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {

    viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
    viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
    viewHolder.imgViewIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!viewHolder.isFav) {
                Toast.makeText(activity,"Selected Row : "+position,Toast.LENGTH_SHORT).show();
                viewHolder.isFav = true;
                viewHolder.imgViewIcon.setImageResource(R.drawable.icon_fav);
            } else {
                viewHolder.imgViewIcon.setImageResource(R.drawable.pre_ic_ab_drawer);
                viewHolder.isFav = false;
            }
        }
    });


    if (viewHolder.isFav)
        viewHolder.imgViewIcon.setImageResource(R.drawable.icon_fav);

}

public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView txtViewTitle;
    public ImageView imgViewIcon;
    public Boolean isFav=false;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
        imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
    }
}

@Override
public int getItemCount() {
    return itemsData.length;
}
 } 

我在输出中看到的内容:

enter image description here

如图所示,我选择第一行图标,但在滚动RecyclerView后,选择了位置更改。

然后尝试在favIcon再次使用错误的位置更改imgViewIcon.setOnClickListeneronCreateViewHolder

我试图在sharedPreferences或modelClass中存储位置,但这在滚动中返回错误的位置

有办法避免这个问题吗?

我看到了这个解决方案:Get clicked item and its position in RecyclerView

2 个答案:

答案 0 :(得分:3)

尝试使用模型ItemData[] itemsData;来保存if元素。

例如

        if (itemsData[position].isFav){

                viewHolder.imgViewIcon.setImageResource(R.drawable.icon_fav);
         }else{

                viewHolder.imgViewIcon.setImageResource(R.drawable.pre_ic_ab_drawer);
         }


viewHolder.imgViewIcon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!itemsData[position].isFav) {
                            Toast.makeText(activity,"Selected Row : "+position,Toast.LENGTH_SHORT).show();
                            itemsData[position].isFav = true;
                            viewHolder.imgViewIcon.setImageResource(R.drawable.icon_fav);
                        } else {
                            viewHolder.imgViewIcon.setImageResource(R.drawable.pre_ic_ab_drawer);
                            itemsData[position].isFav = false;
                        }
                    }
                });

答案 1 :(得分:1)

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ItemData[] itemsData;
Activity activity;

public MyAdapter(Activity activity, ItemData[] itemsData) {
    this.activity=activity;
    this.itemsData = itemsData;
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    View itemLayoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_layout, null);

    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {

    viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
    viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
    viewHolder.imgViewIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!viewHolder.isFav) {
                Toast.makeText(activity,"Selected Row : "+position,Toast.LENGTH_SHORT).show();
                viewHolder.isFav = true;
                viewHolder.imgViewIcon.setImageResource(R.drawable.icon_fav);
            } else {
                viewHolder.imgViewIcon.setImageResource(R.drawable.pre_ic_ab_drawer);
                viewHolder.isFav = false;
            }
        }
    });


    if (viewHolder.isFav)
        viewHolder.imgViewIcon.setImageResource(R.drawable.icon_fav);
    else
        // The change 
        viewHolder.imgViewIcon.setImageResource(R.drawable.pre_ic_ab_drawer);

}

绑定ViewHolder时,如果它不是 fav ,则应重置ImageView图标。