2017-01-09 52 views
0

慢我实现一个自定义RecyclerView做到这一点: enter image description hereRecyclerView是多选

和我用这个自定义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); 
    } 
} 

问题是,当我点击例如颜色(如“红色”)在第一次点击时它不会选择/取消选择item:在第二次点击时,它在同一个视图中工作。

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

任何人都可以有这个问题的建议?

感谢

+0

您的'onBindViewHolder'正在使用一个成员变量'isChecked',它的作用域是类而不是方法。这可能是你的问题。 – Karakuri

+0

@Karakuri有什么解决方案? – ste9206

+0

将状态限制为ViewHolder/item /等。现在的问题是,当为一个项目调用“onBindViewHolder”时,它会影响其他项目的状态,因为它们都引用了您的类作用域成员变量。 – Karakuri

回答

4

好吧,刚刚摆脱你那里isChecked变量。请仅使用checked[position]

并在您的持有者的onClickListener中拨打notifyDataSetChanged而不是隐藏并显示该项目。

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(); 
    } 
});