2017-04-25 116 views
1

我想从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; 
    } 
    } 
} 
+0

'RadioButton's通常一个'RadioGroup'内部使用。如果只有一个“RadioButton”,您最好使用“CheckBox” –

+0

,您必须检查每个卡片视图,如果选择了其他卡片收音机,则将其移除并检查当前情况。在那之后,通知两张牌 –

+0

@Veneed Reddy。单个项目中只有一个单选按钮。但是因为有多个项目。我无法使用CheckBox,因为我一次只需选择一个项目。 –

回答

0

,你必须在你的模型类添加布尔和检查无线电支票变化听者和在你的列表中设置位置明智的标志,然后通知适配器

+0

你能修改我贴的代码吗? –

0

我已经通过更改和测试代码解决了问题,我得到了一个可行的代码。它实际上是从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; 
    } 
} 

}

相关问题