Android开发之ListView,加入CheckBox(复选框),实现选择列表

简介:

http://blog.csdn.net/ahutzh/article/details/6911095


Android ListView没行加入CheckBox,实现选择列表,既可点击复选框进行选中,也可以点击list一行进行选中,效果图如下:


下面贴下主要代码的实现:

对于列表中复选框,我们需要在复选框的状态发生变化时,保存复选框的状态,不然在拖动列表过程中,会丢失复选框的状态。

在这里我们采用下面方式保存:

[java]  view plain copy
  1. public class Person implements Serializable{  
  2.   
  3.     /** 
  4.      *  
  5.      */  
  6.     private static final long serialVersionUID = 1L;  
  7.       
  8.     private String userCode;  
  9.     private String userName;  
  10.     private boolean checked;    //保存复选框的状态  

此对象保存的数据对象列表中的每一列。

实现ListView的Adapter类代码如下:

[java]  view plain copy
  1. /** 
  2.  *  
  3.  * <dl> 
  4.  * <dt>DispatchSelectUserAdapter.java</dt> 
  5.  * <dd>Description: 选择用户界面Adapter类</dd> 
  6.  * <dd>Copyright: Copyright (C) 2011</dd> 
  7.  * <dd>CreateDate: 2011-10-26</dd> 
  8.  * </dl> 
  9.  *  
  10.  * @author ZhanHua 
  11.  */  
  12. public class DispatchSelectUserAdapter extends BaseAdapter {  
  13.   
  14.     private Context mContext;  
  15.     private List<Person> mPersonList;  
  16.     private int mResource;  
  17.     private LayoutInflater mInflater;  
  18.   
  19.     public DispatchSelectUserAdapter(Context context, List<Person> personList,  
  20.             int resource) {  
  21.         mContext = context;  
  22.         mPersonList = personList;  
  23.         mResource = resource;  
  24.         mInflater = LayoutInflater.from(mContext);  
  25.     }  
  26.   
  27.     @Override  
  28.     public int getCount() {  
  29.         return mPersonList.size();  
  30.     }  
  31.   
  32.     @Override  
  33.     public Object getItem(int position) {  
  34.         return mPersonList.get(position);  
  35.     }  
  36.   
  37.     @Override  
  38.     public long getItemId(int position) {  
  39.         return position;  
  40.     }  
  41.   
  42.     @Override  
  43.     public View getView(final int position, View convertView, ViewGroup parent) {  
  44.         if (convertView == null) {  
  45.             convertView = mInflater.inflate(mResource, parent, false);  
  46.         }  
  47.         TextView tvUserName = (TextView) convertView.findViewById(  
  48.                 R.id.dispatch_item_select_user_name);  
  49.         final CheckBox ckbItem = (CheckBox) convertView.findViewById(  
  50.                 R.id.dispatch_item_select_user_ckb);  
  51.         Person person = mPersonList.get(position);  
  52.         tvUserName.setText(person.getUserName());  
  53.         System.out.println(person.getUserName());  
  54.         ckbItem.setChecked(person.isChecked());  
  55.         ckbItem.setOnClickListener(new View.OnClickListener() {  
  56.               
  57.             @Override  
  58.             public void onClick(View v) {  
  59.                 mPersonList.get(position).setChecked(ckbItem.isChecked());//保存checkbox状态至位置对应的列表对象Person中  
  60.                   
  61.             }  
  62.         });  
  63.         person = null;  
  64.         return convertView;  
  65.     }  
  66.   
  67. }  
在主界面初始化数据:

[java]  view plain copy
  1. /** 
  2.      *  
  3.      * Desc:初始化列表数据 
  4.      * @param personList 
  5.      */  
  6.     private void initListData(List<Person> personList) {  
  7.         DispatchSelectUserAdapter adapter = new DispatchSelectUserAdapter(  
  8.                 DispatchSelectUserActivity.this, personList,  
  9.                 R.layout.dispatch_select_user_item);  
  10.         getListView().setAdapter(adapter);  
  11.   
  12.     }  
ListView中列表点击事件:

[java]  view plain copy
  1. /** 
  2.      * 列表点击 
  3.      */  
  4.     @Override  
  5.     protected void onListItemClick(ListView l, View v, int position, long id) {  
  6.         super.onListItemClick(l, v, position, id);  
  7.         CheckBox checkbox = (CheckBox) v.findViewById(R.id.dispatch_item_select_user_ckb);  
  8.         checkbox.setChecked(!checkbox.isChecked());  
  9.         mPersonList.get(position).setChecked(checkbox.isChecked());  
  10.     }  

下面贴下界面设计的XML代码:

主界面的xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:background="@drawable/common_background"  
  6.   android:layout_width="fill_parent"  
  7.   android:layout_height="fill_parent">  
  8.             <include android:id="@+id/dispatch_select_user_title"   
  9.             layout="@layout/common_title"   
  10.             android:layout_alignParentTop="true"/>  
  11.             <LinearLayout   
  12.                 android:id="@+id/section_title_layout"  
  13.                 android:background="@drawable/bg_sencend_title"  
  14.                 android:orientation="horizontal"  
  15.                 android:layout_width="fill_parent"  
  16.                 android:layout_height="28dip" android:weightSum="10"  
  17.                 android:layout_below="@id/dispatch_select_user_title">  
  18.                 <TextView   
  19.                     android:id="@+id/dispatch_section_user_name"   
  20.                     android:layout_marginLeft="10dip"  
  21.                     android:text="@string/dispatch_section_user_name"  
  22.                     android:layout_weight="2"  
  23.                     android:layout_width="fill_parent"  
  24.                     android:layout_height="fill_parent"   
  25.                     android:textColor="@drawable/color_black"  
  26.                     android:clickable="false" android:enabled="false" android:gravity="center_vertical">  
  27.                     </TextView>  
  28.                     <LinearLayout   
  29.                         android:id="@+id/dispatch_lineLayout02"  
  30.                         android:background="#6d6d6e"  
  31.                         android:layout_width="1dip"  
  32.                         android:layout_height="fill_parent"  
  33.                     ></LinearLayout>  
  34.                 <TextView   
  35.                     android:id="@+id/dispatch_section_select"   
  36.                     android:text="@string/dispatch_section_select"  
  37.                     android:layout_weight="8"  
  38.                     android:gravity="center"  
  39.                     android:layout_width="fill_parent"   
  40.                     android:layout_height="fill_parent"  
  41.                     android:textColor="@drawable/color_black"  
  42.                     android:background="@drawable/bg_sencend_title" android:clickable="false" android:enabled="false">  
  43.                     </TextView>  
  44.                 </LinearLayout>  
  45.             <LinearLayout   
  46.                 android:layout_width="fill_parent"  
  47.                 android:layout_height="fill_parent"  
  48.                 android:layout_below="@id/section_title_layout">  
  49.                 <ListView   
  50.                     android:id="@+id/android:list"  
  51.                     android:layout_height="wrap_content"  
  52.                     android:cacheColorHint="#00000000"  
  53.                     android:layout_width="fill_parent"></ListView>  
  54.             </LinearLayout>  
  55. </RelativeLayout>  

ListView中单个Item的xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent">  
  7.     <LinearLayout   
  8.                 android:gravity="center"  
  9.                 android:orientation="horizontal"  
  10.                 android:layout_width="fill_parent"  
  11.                 android:layout_height="40dip" android:weightSum="10">  
  12.                 <TextView   
  13.                     android:id="@+id/dispatch_item_select_user_name"  
  14.                     android:textColor="#000000"  
  15.                     android:layout_marginLeft="10dip"  
  16.                     android:layout_weight="2"   
  17.                     android:layout_width="fill_parent"   
  18.                     android:layout_height="wrap_content"  
  19.                     >  
  20.                     </TextView>  
  21.                 <LinearLayout   
  22.                     android:gravity="center"  
  23.                     android:layout_width="fill_parent"   
  24.                     android:layout_weight="8"   
  25.                     android:layout_height="wrap_content">  
  26.                     <CheckBox   
  27.                     style="@style/CustomCheckBox"  
  28.                     android:id="@+id/dispatch_item_select_user_ckb"   
  29.                     android:focusable="false"  
  30.                     android:layout_width="wrap_content"   
  31.                     android:layout_height="wrap_content">  
  32.                     </CheckBox>  
  33.                 </LinearLayout>  
  34.     </LinearLayout>  
  35. </LinearLayout>  

这样就可以实现上图中的选择列表效果了,也可以在title区域添加全选复选框,点击后,实现全选效果。
相关文章
|
3天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
26天前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
12 0
|
17天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
20天前
|
监控 算法 Android开发
安卓应用开发:打造高效启动流程
【4月更文挑战第5天】 在移动应用的世界中,用户的第一印象至关重要。特别是对于安卓应用而言,启动时间是用户体验的关键指标之一。本文将深入探讨如何优化安卓应用的启动流程,从而减少启动时间,提升用户满意度。我们将从分析应用启动流程的各个阶段入手,提出一系列实用的技术策略,包括代码层面的优化、资源加载的管理以及异步初始化等,帮助开发者构建快速响应的安卓应用。
|
20天前
|
Java Android开发
Android开发之使用OpenGL实现翻书动画
本文讲述了如何使用OpenGL实现更平滑、逼真的电子书翻页动画,以解决传统贝塞尔曲线方法存在的卡顿和阴影问题。作者分享了一个改造后的外国代码示例,提供了从前往后和从后往前的翻页效果动图。文章附带了`GlTurnActivity`的Java代码片段,展示如何加载和显示书籍图片。完整工程代码可在作者的GitHub找到:https://github.com/aqi00/note/tree/master/ExmOpenGL。
21 1
Android开发之使用OpenGL实现翻书动画
|
20天前
|
Android开发 开发者
Android开发之OpenGL的画笔工具GL10
这篇文章简述了OpenGL通过GL10进行三维图形绘制,强调颜色取值范围为0.0到1.0,背景和画笔颜色设置方法;介绍了三维坐标系及与之相关的旋转、平移和缩放操作;最后探讨了坐标矩阵变换,包括设置绘图区域、调整镜头参数和改变观测方位。示例代码展示了如何使用这些方法创建简单的三维立方体。
15 1
Android开发之OpenGL的画笔工具GL10
|
26天前
|
Android开发
Android开发小技巧:怎样在 textview 前面加上一个小图标。
Android开发小技巧:怎样在 textview 前面加上一个小图标。
12 0
|
26天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
28天前
|
缓存 Java Android开发
安卓应用开发中的内存优化策略
在移动应用开发领域,性能一直是衡量应用质量的重要指标之一。特别是对于安卓平台,由于设备的硬件配置多样化,内存管理成为开发者面临的重大挑战。本文将深入探讨针对安卓平台的内存优化技巧,包括内存泄漏的预防、合理使用数据结构和算法、以及高效的资源释放机制。通过这些方法,开发者可以显著提升应用的性能和用户体验。
|
1月前
|
Java Android开发
Android开发系列全套课程
本系列课程面向有java基础,想进入企业从事android开发的计算机专业者。学习搭配实战案例,高效掌握岗位知识。
18 1