【发布时间】: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