Android 使用RecyclerView中的单选按钮仅允许1个选择
我尝试在RecyclerView中使用单选按钮,我只希望用户能够选择一个项目。因此,如果用户选择了其中一个,则需要取消选择先前选择的。我试图在onBindView方法中处理这个问题。这是我的适配器中的代码:Android 使用RecyclerView中的单选按钮仅允许1个选择,android,android-recyclerview,radio-button,Android,Android Recyclerview,Radio Button,我尝试在RecyclerView中使用单选按钮,我只希望用户能够选择一个项目。因此,如果用户选择了其中一个,则需要取消选择先前选择的。我试图在onBindView方法中处理这个问题。这是我的适配器中的代码: public class SelectCategoryAdapter extends RecyclerView.Adapter<SelectCategoryAdapter.MyViewHolder> { private Context mContext; pri
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();
}
}
public类SelectCategoryAdapter扩展了RecyclerView.Adapter{
私有上下文;
私有列表分类列表;
私人类别2;
专用单选按钮单选按钮;
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
公共文本视图标题、计数、名称类别;
公共MyViewHolder(视图){
超级(视图);
nameCategory=(TextView)view.findViewById(R.id.edittext\u category);
count=(TextView)view.findViewById(R.id.count);
radioButton=(radioButton)视图.findViewById(R.id.radio1);
//溢出=(ImageView)view.findViewById(R.id.overflow);
}
}
public SelectCategoryAdapter(上下文mContext、列表类别列表、RadioButton RadioButton){
this.mContext=mContext;
this.categoryList=categoryList;
this.radioButton=radioButton;
}
@凌驾
public SelectCategoryAdapter.MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
View itemView=LayoutInflater.from(parent.getContext())
.充气(R.layout.adapter\u select\u category,parent,false);
返回新的SelectCategoryAdapter.MyViewHolder(itemView);
}
@凌驾
BindViewHolder上的公共无效(最终SelectCategoryAdapter.MyViewHolder,最终int位置){
category=categoryList.get(位置);
holder.nameCategory.setText(category.getName());
if(category.getTasks()<10){
holder.count.setText(“+Integer.toString(category.getTasks())+”);
}否则{
holder.count.setText(Integer.toString(category.getTasks());
}
holder.radioButton.setOnCheckedChangeListener(空);
支架.radioButton.setChecked(位置==radioButton);
holder.radioButton.setOnCheckedChangeListener(新的CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
无线电按钮=位置;
notifyDataSetChanged();
}
});
}
@凌驾
public int getItemCount(){
返回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个文本视图,但对于单选按钮,我得到这个错误 修改模型类别,即类别2 添加属性
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();
}
}
});
修改模型类别,即类别2 添加属性
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();
}
}
});
为什么要将
单选按钮
传递给适配器的构造函数?如果每个MyViewHolder
都应该有一个RadioButton
,那么MyViewHolder
应该有一个对其自己的RadioButton
的引用(就像你如何拥有标题、计数和类别TextView
一样)。您获得“无法解析符号”的原因是您试图引用MyViewHolder
类上名为radioButton的字段,该字段不存在。感谢您的回复,我浏览了很多代码,在混合代码以适应我的情况时,我感到困惑。您介意帮我现在将项目文本视图的内容传递到我的主要活动吗?为什么要将单选按钮传递到适配器的构造函数中?如果每个MyViewHolder
都应该有一个RadioButton
,那么MyViewHolder
应该有一个对其自己的RadioButton
的引用(就像你如何拥有标题、计数和类别TextView
一样)。您获得“无法解析符号”的原因是您试图引用MyViewHolder
类上名为radioButton的字段,该字段不存在。感谢您的回复,我浏览了很多代码,在混合代码以适应我的情况时,我感到困惑。您介意帮我将项目文本视图的内容传递到我的主要活动吗?