【问题标题】:How to select only one radio button in CardView in Android?如何在 Android 的 CardView 中只选择一个单选按钮?
【发布时间】:2017-04-26 00:38:50
【问题描述】:

我试图从 cardView 生成的列表中一次只选择一个单选按钮。我尝试了很多答案,包括this one。但这些似乎都不起作用。这是我的代码 group_list_item.xml

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view= "http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:elevation="1dp"
card_view:cardCornerRadius="1dp">
<RelativeLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:padding="10dp"
    android:background="?android:selectableItemBackground">
    <TextView
        android:text="Description"
        android:id="@+id/owner"
        android:textStyle="italic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="84dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />
    <TextView
        android:text="Title"
        android:textSize="25dp"
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/username" />
    <RadioButton
        android:text="Select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="23dp"
        android:id="@+id/radioButton" />
</RelativeLayout>

CustomGroupAdapter.java

public class CustomGroupAdapter extends RecyclerView.Adapter<CustomGroupAdapter.MyVH>{
private int selectedPosition = -1;
View view;
Context context;
private List<GroupDataModel> dataModels;
private LayoutInflater inflater;
public CustomGroupAdapter(Context context, List<GroupDataModel> data){
    this.context = context;
    this.dataModels = data;
    this.inflater = LayoutInflater.from(context);
}
@Override
public MyVH onCreateViewHolder(ViewGroup parent, int viewType) {
    view = inflater.inflate(R.layout.group_list_item, parent, false);
    MyVH myVH = new MyVH(view);
    return myVH;
}
@Override
public int getItemCount() {
    return dataModels.size();
}

@Override
public void onBindViewHolder(final MyVH holder, final int position) {

    GroupDataModel dataModel = dataModels.get(position);
    holder.setData(dataModel, position);
    final RadioButton radioButton = (RadioButton) holder.itemView.findViewById(R.id.radioButton);
    holder.itemView.findViewById(R.id.radioButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (selectedPosition == -1) {
                radioButton.setChecked(position == getItemCount() - 1);
            } else {
                radioButton.setChecked(selectedPosition == position);
            }
        }
    });
}
class MyVH extends RecyclerView.ViewHolder{
    TextView name;
    TextView owner;
    int postion;
    GroupDataModel dataModel;
    public MyVH(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.name);
        owner = (TextView) itemView.findViewById(R.id.owner);
    }
    public void setData(GroupDataModel dataModel, int position) {
        this.name.setText(dataModel.getName());
        this.owner.setText(dataModel.getOwner());
        this.postion = position;
        this.dataModel = dataModel;
    }
  }
}

【问题讨论】:

  • RadioButtons 通常在RadioGroup 中使用。如果只有一个RadioButton,你最好使用CheckBox
  • 你必须检查每个卡片视图,如果有其他卡片收音机被选中,那么删除那个并检查当前。在两张卡的通知项之后
  • @Veneed Reddy。单个项目中只有一个单选按钮。但是因为有不止一项。我不能使用 CheckBox,因为我一次只需要选择一项。
  • @DivyeshPatel 我正在尝试做同样的事情,但无法做到。你可以在代码中看到我的尝试。
  • 我明天解决你的问题,到那时再试试

标签: android


【解决方案1】:

您必须在模型类中添加布尔值并检查无线电检查更改列表器并在列表中设置位置明智标志,然后通知适配器

【讨论】:

    【解决方案2】:

    我已经通过更改和测试代码解决了这个问题,我得到了一个有效的。它实际上来自this answer. 但我已经将它用于cardView。

    public class CustomGroupAdapter extends RecyclerView.Adapter<CustomGroupAdapter.MyVH>{
    RadioButton radioButton;
    View view;
    Context context;
    private List<GroupDataModel> dataModels;
    private LayoutInflater inflater;
    public CustomGroupAdapter(Context context, List<GroupDataModel> data){
        this.context = context;
        this.dataModels = data;
        this.inflater = LayoutInflater.from(context);
    }
    @Override
    public MyVH onCreateViewHolder(ViewGroup parent, int viewType) {
        view = inflater.inflate(R.layout.group_list_item, parent, false);
        MyVH myVH = new MyVH(view);
        return myVH;
    }
    @Override
    public int getItemCount() {
        return dataModels.size();
    }
    
    @Override
    public void onBindViewHolder(final MyVH holder, final int position) {
    
        GroupDataModel dataModel = dataModels.get(position);
        holder.setData(dataModel, position);
        holder.itemView.findViewById(R.id.radioButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(radioButton == null){
                    radioButton = (RadioButton) v;
                radioButton.setChecked(true);
            }
            if(radioButton== v)
                    return;
            radioButton.setChecked(false);
            ((RadioButton)v).setChecked(true);
            radioButton = (RadioButton)v;
        }
        });
    }
    class MyVH extends RecyclerView.ViewHolder{
        TextView name;
        TextView owner;
        int postion;
        GroupDataModel dataModel;
        public MyVH(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            owner = (TextView) itemView.findViewById(R.id.owner);
        }
        public void setData(GroupDataModel dataModel, int position) {
            this.name.setText(dataModel.getName());
            this.owner.setText(dataModel.getOwner());
            this.postion = position;
            this.dataModel = dataModel;
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 2016-06-27
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      相关资源
      最近更新 更多