2017-09-05 235 views
0

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

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> 

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

+2

你为什么要传递一个'RadioButton'到您的适配器的构造?如果每个'MyViewHolder'应该有一个'RadioButton',那么'MyViewHolder'应该引用它自己的'RadioButton'(就像你拥有title,count和'TextView'类)。你得到“无法解析符号”的原因是因为你试图在MyViewHolder类中引用一个名为radioButton的字段,该字段不存在。 –

+0

感谢您的回复,我正在浏览大量代码,并在混合代码以适应我的情况时感到困惑。 –

+0

你介意帮助我将项目的TextViews的内容传递给我的主要活动吗? –

回答

0

修改模型类,即产品组别 添加属性

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

      } 
     });