我想让我的CardView CheckBox表现得像一个RadioGroup,这样每当我检查另一张卡时,前一个将取消选中。我现在已成功创建带复选框的CardView,但我不确定如何让它们像我想要的那样。
cardview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_margin="@dimen/activity_horizontal_margin"
android:background="?android:selectableItemBackground"
android:id="@+id/suggest_time_card_view"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/activity_horizontal_margin"
android:background="@drawable/back"
android:gravity="center"
android:padding="@dimen/activity_horizontal_margin"
android:id="@+id/tvSuggestTime"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cbSuggestTime"
android:visibility="invisible"
android:checked="false"
android:padding="@dimen/activity_horizontal_margin"
android:layout_alignBottom="@+id/tvSuggestTime"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
CardViewAdapter:
public class SuggestTimeCardAdapter extends RecyclerView.Adapter<SuggestTimeCardAdapter.ViewHolder> {
private Context context;
private List<SuggestTimeList> suggestTimeLists;
private Integer selected_position = -1;
public SuggestTimeCardAdapter(List<SuggestTimeList> suggestTimeLists, Context context){
super();
this.suggestTimeLists = suggestTimeLists;
this.context = context;
}
@Override
public SuggestTimeCardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.suggest_time_card, parent, false);
SuggestTimeCardAdapter.ViewHolder viewHolder = new SuggestTimeCardAdapter.ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final SuggestTimeCardAdapter.ViewHolder holder, final int position) {
final SuggestTimeList suggestTimeList = suggestTimeLists.get(position);
holder.tvSuggestTime.setText(suggestTimeList.getSuggestTime());
holder.cbSuggestTime.setChecked(position==selected_position);
holder.cbSuggestTime.setVisibility(View.VISIBLE);
holder.suggest_time_card_view.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//implement the click
Boolean CheckTime = suggestTimeList.getCheckTime();
String reservationTime = holder.tvSuggestTime.getText().toString();
Integer ItemId = suggestTimeList.getTimeId();
holder.cbSuggestTime.setChecked(true);
selected_position = position;
for(SuggestTimeList s : suggestTimeLists) {
s.setChecked(false);
}
suggestTimeList.setChecked(true);
//notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return suggestTimeLists.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvSuggestTime;
public CheckBox cbSuggestTime;
public CardView suggest_time_card_view;
public ViewHolder(View itemView) {
super(itemView);
tvSuggestTime = (TextView) itemView.findViewById(R.id.tvSuggestTime);
cbSuggestTime = (CheckBox) itemView.findViewById(R.id.cbSuggestTime);
suggest_time_card_view = (CardView) itemView.findViewById(R.id.suggest_time_card_view);
}
}
}
答案 0 :(得分:1)
CardView中的复选框是否像RadioGroup一样?
您可以通过添加字段(存储复选框状态)来修改模型,例如
boolean isSelected;
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
在onBindViewHolder
中,您可以使用此字段设置复选框。像这样:
// suggestTimeList is an item of list.
holder.cbSuggestTime.setCheck(suggestTimeList.isSelected());
holder.suggest_time_card_view.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// your code ....
// UnSelected all
for (int i = 0; i < suggestTimeLists.size(); i++) {
suggestTimeLists.get(i).setSelected(false);
}
// Only select card you have clicked
suggestTimeLists.get(position).setSelected(true);
notifyDataSetChanged();
}
});