使用RecyclerView中的单选按钮仅允许1个选择

时间:2017-09-05 19:20:41

标签: android android-recyclerview radio-button

我正在尝试在RecyclerView中使用RadioButtons,我只希望用户能够选择一个项目。因此,如果用户选择一个先前选择的需要被取消选择。我试图在我的onBindView方法中处理这个。这是我的适配器中的代码:

public class SelectCategoryAdapter extends RecyclerView.Adapter<SelectCategoryAdapter.MyViewHolder> {

    private Context mContext;
    private List<Category2> categoryList;
    private Category2 category;
    private RadioButton radioButton;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, count, nameCategory;

        public MyViewHolder(View view) {
            super(view);
            nameCategory = (TextView) view.findViewById(R.id.edittext_category);
            count = (TextView) view.findViewById(R.id.count);
            radioButton = (RadioButton) view.findViewById(R.id.radio1);
            //overflow = (ImageView) view.findViewById(R.id.overflow);
        }
    }


    public SelectCategoryAdapter(Context mContext, List<Category2> categoryList, RadioButton radioButton) {
        this.mContext = mContext;
        this.categoryList = categoryList;
        this.radioButton = radioButton;
    }

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

        return new SelectCategoryAdapter.MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final SelectCategoryAdapter.MyViewHolder holder, final int position) {
        category = categoryList.get(position);
        holder.nameCategory.setText(category.getName());
        if (category.getTasks() < 10){
            holder.count.setText(" " + Integer.toString(category.getTasks()) + " ");
        } else {
            holder.count.setText(Integer.toString(category.getTasks()));
        }

        holder.radioButton.setOnCheckedChangeListener(null);
        holder.radioButton.setChecked(position == radioButton);
        holder.radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                radioButton = position;
                notifyDataSetChanged();
            }
        });

    }


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

}

这是适配器的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_below="@+id/toolbar"
        android:id="@+id/add_category_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="20dp">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:checked="true"
            android:id="@+id/radio1"
            android:paddingTop="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingBottom="20dp"
            />

        <TextView
            android:id="@+id/edittext_category"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#494949"
            android:textSize="@dimen/s_text_size"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:maxLines="1"/>

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/circle"
            android:padding="3dp"
            android:text="20"
            android:textColor="@color/white"
            android:textSize="11sp"
            android:textStyle="bold"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"/>
    </LinearLayout>
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#ececec" />

</LinearLayout>

所以,第一个错误是我得到了“无法解析符号radioButton”,我不知道为什么会发生这种情况,它正在为其他2个TextView工作但是对于单选按钮我得到了这个错误。

1 个答案:

答案 0 :(得分:0)

修改您的模型类,即 Category2 添加属性

public class Category2{
    ....
    ...
    boolean isSelected  = false;
    int previousSelectedPosition = -1;
}

onBindViewHolder

category = categoryList.get(position);

holder.radioButton.setChecked(category.getIsSelected());

holder.radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                          if(previousSelectedPosition >= 0)
                                categoryList.get(previousSelectedPosition).setIsSelected(false);
                          previousSelectedPosition = position;
                          category.isSelected = !category.isSelected;
                          if(radioButton!=null)
                              radioButton.setChecked(false);
                          radioButton = holder.radioButton;
                          notifyDataSetChanged();
                    }

            }
        });