热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AndroidListView自定义CheckBox实现列表项多选功能详解

本文详细介绍了在Android开发中如何在ListView的每一行添加CheckBox,以实现列表项的多选功能。用户不仅可以通过点击复选框来选择项目,还可以通过点击列表的任意一行来完成选中操作,提升了用户体验和操作便捷性。同时,文章还探讨了相关的事件处理机制和布局优化技巧,帮助开发者更好地实现这一功能。

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

3f6158a754f1220cfe9715840cfe43ac.png

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

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

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

public class Person implements Serializable{

/**

*

*/

private static final long serialVersionUID = 1L;

private String userCode;

private String userName;

private boolean checked;//保存复选框的状态

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

实现ListView的Adapter类代码如下:

/**

*

*

*

DispatchSelectUserAdapter.java

*

Description: 选择用户界面Adapter类

*

Copyright: Copyright (C) 2011

*

CreateDate: 2011-10-26

*

*

* @author ZhanHua

*/

public class DispatchSelectUserAdapter extends BaseAdapter {

private Context mContext;

private List mPersonList;

private int mResource;

private LayoutInflater mInflater;

public DispatchSelectUserAdapter(Context context, List personList,

int resource) {

mContext = context;

mPersonList = personList;

mResource = resource;

mInflater = LayoutInflater.from(mContext);

}

@Override

public int getCount() {

return mPersonList.size();

}

@Override

public Object getItem(int position) {

return mPersonList.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

if (convertView == null) {

convertView = mInflater.inflate(mResource, parent, false);

}

TextView tvUserName = (TextView) convertView.findViewById(

R.id.dispatch_item_select_user_name);

final CheckBox ckbItem = (CheckBox) convertView.findViewById(

R.id.dispatch_item_select_user_ckb);

Person person = mPersonList.get(position);

tvUserName.setText(person.getUserName());

System.out.println(person.getUserName());

ckbItem.setChecked(person.isChecked());

ckbItem.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mPersonList.get(position).setChecked(ckbItem.isChecked());//保存checkbox状态至位置对应的列表对象Person中

}

});

person = null;

return convertView;

}

}在主界面初始化数据:

/**

*

* Desc:初始化列表数据

* @param personList

*/

private void initListData(List personList) {

DispatchSelectUserAdapter adapter = new DispatchSelectUserAdapter(

DispatchSelectUserActivity.this, personList,

R.layout.dispatch_select_user_item);

getListView().setAdapter(adapter);

}ListView中列表点击事件:

/**

* 列表点击

*/

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

super.onListItemClick(l, v, position, id);

CheckBox checkbox = (CheckBox) v.findViewById(R.id.dispatch_item_select_user_ckb);

checkbox.setChecked(!checkbox.isChecked());

mPersonList.get(position).setChecked(checkbox.isChecked());

}

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

主界面的xml:

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:background="@drawable/common_background"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

layout="@layout/common_title"

android:layout_alignParentTop="true"/>

android:id="@+id/section_title_layout"

android:background="@drawable/bg_sencend_title"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="28dip" android:weightSum="10"

android:layout_below="@id/dispatch_select_user_title">

android:id="@+id/dispatch_section_user_name"

android:layout_marginLeft="10dip"

android:text="@string/dispatch_section_user_name"

android:layout_weight="2"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:textColor="@drawable/color_black"

android:clickable="false" android:enabled="false" android:gravity="center_vertical">

android:id="@+id/dispatch_lineLayout02"

android:background="#6d6d6e"

android:layout_width="1dip"

android:layout_height="fill_parent"

>

android:id="@+id/dispatch_section_select"

android:text="@string/dispatch_section_select"

android:layout_weight="8"

android:gravity="center"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:textColor="@drawable/color_black"

android:background="@drawable/bg_sencend_title" android:clickable="false" android:enabled="false">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_below="@id/section_title_layout">

android:id="@+id/android:list"

android:layout_height="wrap_content"

android:cacheColorHint="#00000000"

android:layout_width="fill_parent">

ListView中单个Item的xml:

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:gravity="center"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="40dip" android:weightSum="10">

android:id="@+id/dispatch_item_select_user_name"

android:textColor="#000000"

android:layout_marginLeft="10dip"

android:layout_weight="2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

android:gravity="center"

android:layout_width="fill_parent"

android:layout_weight="8"

android:layout_height="wrap_content">

style="@style/CustomCheckBox"

android:id="@+id/dispatch_item_select_user_ckb"

android:focusable="false"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

这样就可以实现上图中的选择列表效果了,也可以在title区域添加全选复选框,点击后,实现全选效果。



推荐阅读
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Android 九宫格布局详解及实现:人人网应用示例
    本文深入探讨了人人网Android应用中独特的九宫格布局设计,解析其背后的GridView实现原理,并提供详细的代码示例。这种布局方式不仅美观大方,而且在现代Android应用中较为少见,值得开发者借鉴。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
author-avatar
离开一场爱的风雪
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有