Android(Java):Android ListView checkbox 全选效果

先在适配器的getView方法加入红色代码

@Override
 public View getView(final int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  ViewHolder viewHolder = null;
  if(convertView == null) {
   viewHolder = new ViewHolder();
   convertView = mLayoutInflater.inflate(R.layout.item_record_list, null);
   viewHolder.arrow = convertView.findViewById(R.id.riv_arraw);
   viewHolder.tvName = (TextView) convertView.findViewById(R.id.rtv_name);
   viewHolder.tvRecord = (TextView) convertView.findViewById(R.id.rtv_record);
   viewHolder.btnDelete = (Button) convertView.findViewById(R.id.recorddel);
   viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.cb_check);
   //设置行高度
   AbsListView.LayoutParams params=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LayoutManager.dip2px(mContext, LIST_HEIGHT));
   convertView.setLayoutParams(params);
   convertView.setTag(viewHolder);
  }else {
   viewHolder = (ViewHolder) convertView.getTag();
  }
  convertView.setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    view = v;
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
                    v.setBackgroundResource(R.color.dongao_orange);
                }else{
                 v.setBackgroundResource(0);
                }
    return gd.onTouchEvent(event);
   }
   
  });
  final StudyLog record = records.get(position);
  viewHolder.tvName.setText(record.getName());
  viewHolder.tvRecord.setText(record.getLog());
  viewHolder.btnDelete.setVisibility(record.getStatus());
  viewHolder.checkBox.setVisibility(visibility);//是否编辑状态 编辑状态显示checkbox
  viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {//先注册选中事件
   
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    record.setCheck(buttonView.isChecked());//记录选中状态
    handler.sendEmptyMessage(0);//从Activity传过来的handler
   }
   
  });
  viewHolder.checkBox.setChecked(record.isCheck());//设置选中状态
  RelativeLayout.LayoutParams margin = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
  if(visibility == View.VISIBLE){
   viewHolder.arrow.setVisibility(View.GONE);
   margin.setMargins(45, 8, 0, 0);
  }else{
   viewHolder.arrow.setVisibility(View.VISIBLE);
   margin.setMargins(35, 8, 0, 0);
  }
  viewHolder.tvName.setLayoutParams(margin);
  convertView.setId(position);
  return convertView;
 }

 

activity里面的handler:

private Handler handler=new Handler(){
  public void handleMessage(Message msg) {
   num = 0;
   datas = mRecordAdapter.getRecords();
   for(StudyLog log : datas) {//适配器是懒加载模式,当超过一页的时候会出现计数不准
    if(log.isCheck()){
     num ++;
    }
   }
   if(num == datas.size()){
    mBtnCheckall.setText(R.string.cancelcheckall);
   }else{
    mBtnCheckall.setText(R.string.checkall);
   }
   mBtnDelete.setText(String.format(getString(R.string.delete), num));
  };
 };

 

         由于checkbox比较小很难点击,很有可能引起其他事件,所以我想在长按事件实现其选中功能,但是用户体验性不好,用户不容易发现。最终在单击时判断是不是编辑状态,如果是就选中,否则就执行其他事件。

 

@Override
  public boolean onSingleTapUp(MotionEvent e) {
   // TODO Auto-generated method stub
   if(visibility == View.GONE){
    playVideo(view.getId());
    return false;
   }
   ViewHolder vh = (ViewHolder) view.getTag();
   boolean check = !vh.checkBox.isChecked();
   vh.checkBox.setChecked(check);
   return false;
  }

你可能感兴趣的:(android,ListView,checkbox,全选)