我想從CardView生成的列表中每次只選擇一個單選按鈕。我已經嘗試了很多答案,包括this one。但這些似乎都沒有奏效。這裏是我的代碼 group_list_item.xml如何在Android的CardView中只選擇一個單選按鈕?
<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;
}
}
}
'RadioButton's通常一個'RadioGroup'內部使用。如果只有一個「RadioButton」,您最好使用「CheckBox」 –
,您必須檢查每個卡片視圖,如果選擇了其他卡片收音機,則將其移除並檢查當前情況。在那之後,通知兩張牌 –
@Veneed Reddy。單個項目中只有一個單選按鈕。但是因爲有多個項目。我無法使用CheckBox,因爲我一次只需選擇一個項目。 –