Recyclerview多选视图问题

时间:2017-07-21 11:25:42

标签: android android-recyclerview

我有一个回收站视图适配器,其中有一个fav图标视图。当我选择一个fav图标时,滚动时会选择多个图标。

    <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Seema Gupta"
            android:textSize="16sp"
            android:textStyle="bold"
            android:textColor="@color/black"/>

        <ImageView
            android:id="@+id/iv_favourite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/favourite_icon"
            android:layout_alignParentRight="true"
            android:padding="10dp"
            android:layout_marginRight="0dp"/>

适配器类: -

      @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_maid_list_row_view, parent, false);

        return new MyViewHolder(itemView);
    }
 @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        if (maidLists.get(position).isFav()){
           holder.iv_favourite.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
           holder.iv_favourite.setAlpha(0.7f);
        }else {
            holder.iv_favourite.getDrawable().setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN);
            holder.iv_favourite.setAlpha(0.7f);
        }


        holder.iv_favourite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (maidLists.get(position).isFav()){
                    holder.iv_favourite.getDrawable().setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN);
                    holder.iv_favourite.setAlpha(0.7f);
                    maidLists.get(position).setFav(false);
                }else {
                    holder.iv_favourite.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
                    holder.iv_favourite.setAlpha(0.7f);
                    maidLists.get(position).setFav(true);
                }
            }
        });

模特课: -

  public boolean isFav() {
    return isFav;
}

public void setFav(boolean fav) {
    isFav = fav;
}

1 个答案:

答案 0 :(得分:2)

在onClick方法中添加此方法 notifyItemChanged(position)

holder.iv_favourite.setOnClickListener(new View.OnClickListener() 
{
         @Override
         public void onClick(View v) {
                if (maidLists.get(position).isFav())
                {                        
                holder.iv_favourite.getDrawable().setColorFilter(Color.GRAY, 
                    PorterDuff.Mode.SRC_IN);
                    holder.iv_favourite.setAlpha(0.7f);
                    maidLists.get(position).setFav(false);
                }
                else 
                 {
                 holder.iv_favourite.getDrawable().setColorFilter(Color.RED, 
                 PorterDuff.Mode.SRC_IN);
                 holder.iv_favourite.setAlpha(0.7f);
                 maidLists.get(position).setFav(true);
                }
               notifyItemChanged(position);
     }
});