RecyclerView在多选

时间:2017-01-09 16:17:10

标签: android android-recyclerview android-adapter

我已经实现了自定义RecyclerView来执行此操作: enter image description here

我已经使用了这个自定义xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:clickable="true"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="50dp">

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:id="@+id/color"
        android:background="@drawable/appborder" />

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/item"
        android:textColor="@color/colorPrimary"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/color"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp" />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        app:srcCompat="@drawable/check"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:visibility="invisible"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="7dp"
        android:layout_marginEnd="7dp"
        android:id="@+id/check" />

</RelativeLayout>

这是我的自定义适配器:

public class RecyclerColorsDialogAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private int [] colors;
    private boolean [] checked;
    private String [] text;
    private boolean isChecked;

    public RecyclerColorsDialogAdapter(Context context, int[] colors, boolean[] checked, String[] text) {
        this.context = context;
        this.colors = colors;
        this.checked = checked;
        this.text = text;
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_color_item, parent, false);
        PaHolder mHolder = new PaHolder(view);
        return mHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        String singleColor = text[position];
        isChecked = checked[position];

        final PaHolder mHolder = (PaHolder)holder;

        mHolder.text.setText(singleColor);

        setCorners(colors[position],mHolder.color);

        if(isChecked)
         mHolder.check.setVisibility(View.VISIBLE);
        else
         mHolder.check.setVisibility(View.INVISIBLE);

        mHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              if (isChecked) {
               checked[position] = false;
               isChecked = false;

              } else {
               checked[position] = true;
               isChecked = true;
              }

              mHolder.check.setVisibility(checked[position] ? View.VISIBLE : View.INVISIBLE);
            }
        });
       mHolder.text.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Avenir-Book.ttf"));
    }

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

    private class PaHolder extends RecyclerView.ViewHolder {
        View view;
        TextView text;
        ImageView color, check;

        public PaHolder(View itemView) {
            super(itemView);
            view = itemView;
            text = (TextView)itemView.findViewById(R.id.item);
            check = (ImageView)itemView.findViewById(R.id.check);
            color = (ImageView)itemView.findViewById(R.id.color);
        }
    }

    public void setCorners(int color,ImageView imageView) {
        GradientDrawable gd = new GradientDrawable();

        // Specify the shape of drawable
        gd.setShape(GradientDrawable.RECTANGLE);

        // Set the fill color of drawable
        gd.setColor(context.getResources().getColor(color));// make the background transparent

        // Create a 2 pixels width red colored border for drawable
        gd.setStroke(2, context.getResources().getColor(R.color.corners)); // border width and color

        // Make the border rounded
        gd.setCornerRadius(15.0f); // border corner radius

        // Finally, apply the GradientDrawable as TextView background
       imageView.setBackground(gd);
    }
}

问题在于,当我在第一次点击时多次点击一种颜色(如“红色”)时,它不会选择/取消选择该项目:在同一视图中第二次点击它可以工作。

我也试过放focusable = false,但问题是一样的。

任何人都可以对此问题提出建议吗?

由于

1 个答案:

答案 0 :(得分:4)

好的,只是摆脱那里的isChecked变量。仅使用checked[position]代替。

在你的持有人的notifyDataSetChanged中拨打onClickListener,而不是隐藏和显示该项目。

if(checked[position])
    mHolder.check.setVisibility(View.VISIBLE);
else
    mHolder.check.setVisibility(View.INVISIBLE);

mHolder.view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      if (checked[position]) {
       checked[position] = false;
      } else {
       checked[position] = true;
      }

      // Just add notify. Remove the show/hide code. 
      notifyDataSetChanged();
    }
});