mardi 14 février 2017

How to make checkbox in CardView behave like RadioGroup?

I want to make my CardView CheckBox behave like a RadioGroup so that whenever I check another Card the previous will be unchecked. I now have successfully create a CardView with checkboxes but I am not sure how to make them behave like I want to.

cardview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
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);
    }
  }
}




Aucun commentaire:

Enregistrer un commentaire