2017-02-15 55 views
0

我想讓我的CardView CheckBox像RadioGroup一樣工作,以便每當我檢查另一張卡片時,前一個將被取消選中。我現在已經成功創建了一個帶複選框的CardView,但我不確定如何使它們像我想要的那樣工作。如何使CardView中的複選框與RadioGroup類似?

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:在CardView

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); 
    } 
    } 
} 

Previous checkbox is still checked need it to be unchecked after checking another card

+0

請畫出你的效果圖! – Fang

+0

您可以嘗試將RadioGroup中的複選框放入您的xml中。 – Android

+0

@Jason對不起,我忘了補充一點,請參閱更新 – JerryKo

回答

1

製作複選框表現得像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(); 
     } 
    });