Android cardview onclick实现

时间:2017-01-04 05:47:51

标签: android checkbox android-cardview

我列出了一系列时间序列,从0:00到23:00,间隔为15分钟,即[0:00,0:15,......,22:45, 23:00]现在我想对卡片实施onClick,每当我点击一张卡片时,卡片中都会设置一个复选框,并且设置为true,当我点击另一张卡片时,之前点击的卡片复选框将被设置作为假和不可见,而新的将包含一个选中的复选框

public class SuggestTimeCardAdapter extends RecyclerView.Adapter<SuggestTimeCardAdapter.ViewHolder> {

    private Context context;

    List<SuggestTimeList> suggestTimeLists;

    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, int position) {

        SuggestTimeList suggestTimeList =  suggestTimeLists.get(position);

        holder.tvSuggestTime.setText(suggestTimeList.getSuggestTime());

        holder.suggest_time_card_view.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v){
                //implement the click
                holder.cbSuggestTime.setVisibility(View.VISIBLE);
                holder.cbSuggestTime.setChecked(true);
            }
        });


    }
    @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:00,那么将检查所有增量为2:15的卡片,以便检查2:15,4:30等等。 ,然后在滚动而不点击任何内容时,选中的卡片将突然变为未选中状态,并检查随机卡片。我在这里很困惑,请有人帮忙吗?

1 个答案:

答案 0 :(得分:0)

SuggestTimeList课程中,添加一个属性; isChecked,就像这样:

class SuggestTimeList
{
   boolean isChecked;
   //// Other attributes and methods...
   public void setChecked(boolean isChecked) {
      this.isChecked = isChecked;
   }

   public boolean isChecked() {
      return isChecked;
   }
}

然后在您的适配器中执行此操作:

@Override
public void onBindViewHolder(final SuggestTimeCardAdapter.ViewHolder holder, int position) {

    SuggestTimeList suggestTimeList =  suggestTimeLists.get(position);

    holder.tvSuggestTime.setText(suggestTimeList.getSuggestTime());
    if(suggestTimeList.isChecked()) {
            holder.cbSuggestTime.setVisibility(View.VISIBLE);
            holder.cbSuggestTime.setChecked(true);
    }else {
            holder.cbSuggestTime.setChecked(false);
            holder.cbSuggestTime.setVisibility(View.INVISIBLE);
    }  
    holder.suggest_time_card_view.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            //implement the click
            holder.cbSuggestTime.setVisibility(View.VISIBLE);
            holder.cbSuggestTime.setChecked(true);
            for(SuggestTimeList s : suggestTimeLists) {
                 s.setChecked(false);
            }
            suggestTimeList.setChecked(true);
            notifyDataSetChanged();

        }
    });


}