当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程之listView中checkbox用法实例分析

Android编程之listView中checkbox用法实例分析

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android编程之listview中checkbox用法。分享给大家供大家参考,具体如下:

我们经常会用到在listview中使用checkbox的情况。直接不回应用后会发现,listview中的onitemclicklistener事件会和checkbox中的选择事件发生冲突,这个怎么处理呢。直接上代码。

list_item.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/color_while">
  <textview
    android:id="@+id/txt_add_note_tag_list_name"
    android:layout_height="50dp"
    android:layout_width="fill_parent"
    android:gravity="center_vertical"
    android:textcolor="@color/color_black"
    android:layout_marginleft="8dp"
    />
  <checkbox
    android:id="@+id/chk_add_note_tag_list_chk"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_alignparentright="true"
    android:layout_marginright="8dp"
    android:focusable="false"   <!--这个是必须加上的,不然会出现冲突-->
    android:clickable="false"   <!--这个是必须加上的,不然会出现冲突-->
    />
</relativelayout>

baseadapter.java代码:

package cg.guangda.androidnote;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import cg.guangda.androidnote.model.notetag;
import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.checkbox;
import android.widget.textview;
public class add_note_tag_list_baseadapter extends baseadapter {
  private layoutinflater inflater;
  private list<notetag> list_notetag_date;
  //定义多选框是否被选中
  public static map<integer, boolean> isselected;
  public add_note_tag_list_baseadapter(context context,list<notetag> list_notetag_date)
  {
    this.inflater = layoutinflater.from(context);
    this.list_notetag_date = list_notetag_date;
    //这儿定义isselected这个map是记录每个listitem的状态,初始状态全部为false。
    isselected = new hashmap<integer, boolean>();
    for (int i = 0; i < list_notetag_date.size(); i++) {
      isselected.put(i, false);
    }
  }
  @override
  public int getcount() {
    // todo auto-generated method stub
    return list_notetag_date.size();
  }
  @override
  public object getitem(int position) {
    // todo auto-generated method stub
    return list_notetag_date.get(position);
  }
  @override
  public long getitemid(int position) {
    // todo auto-generated method stub
    return position;
  }
  @override
  public view getview(int position, view convertview, viewgroup parent) {
    add_note_notetag notetag = null;
    if(convertview==null)
    {
      convertview = inflater.inflate(r.layout.add_note_tag_list_item, null);
      notetag = new add_note_notetag();
      notetag.txt_add_note_tag_list_name = (textview)convertview.findviewbyid(r.id.txt_add_note_tag_list_name);
      notetag.chk_add_note_tag_list_chk = (checkbox)convertview.findviewbyid(r.id.chk_add_note_tag_list_chk);
      convertview.settag(notetag);
    }
    else {
      notetag = (add_note_notetag)convertview.gettag();
    }
    notetag.txt_add_note_tag_list_name.settext(list_notetag_date.get(position).get_tagname());
    notetag.chk_add_note_tag_list_chk.setchecked(isselected.get(position));
    return convertview;
  }
  public class add_note_notetag
  {
    textview txt_add_note_tag_list_name;
    checkbox chk_add_note_tag_list_chk;
  }
}

应用页面:

list_popwin_note_tag.setadapter(new add_note_tag_list_baseadapter(this, list_notetag_date));
list_popwin_note_tag.setonitemclicklistener(new notetaglistitemonclicklistener());

/**
* 点击列表项事件
* @author cg
*
*/
class notetaglistitemonclicklistener implements onitemclicklistener{
    @override
    public void onitemclick(adapterview<?> arg0, view view, int position,
        long arg3) {
      // todo auto-generated method stub
      add_note_notetag vhollder = (add_note_notetag) view.gettag();
      //在每次获取点击的item时将对于的checkbox状态改变,同时修改map的值。
      vhollder.chk_add_note_tag_list_chk.setchecked(vhollder.chk_add_note_tag_list_chk.ischecked()==true ? false : true);
      boolean check = vhollder.chk_add_note_tag_list_chk.ischecked();
      add_note_tag_list_baseadapter.isselected.put(position, check);
      log.v("notetaglistitemonclicklistener", list_notetag_date.get(position).get_tagname() + check);
    }
}

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网