我试图从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;
}
}
}
答案 0 :(得分:0)
你必须在模型类中添加布尔值并检查无线电检查更改列表器并在列表中设置位置明智标志,然后通知适配器
答案 1 :(得分:0)