博文開始之前,首先要感谢大牛:(lmj623565791),本博文是在其博文http://blog.csdn.net/lmj623565791/article/details/40212367基础上进一步的改动而来。

本博文主要是利用ListView实现多级树控件,并通过CheckBox来对各节点的全选与反选的功能,首先来看一下效果:

Android  实现自己定义多级树控件和全选与反选的效果


对于多级树的显示事实上就是通过数据中各个节点的关系,通过不同的缩进来达到树的效果。而数据中主要要把握id,父节点pId。name的关系,来显示其效果。

代码实现例如以下:

一. 布局xml文件

1.主界面activity_main.xml,简单的ListView和一个控制CheckBox切换的Button


2.树节点布局list_view.xml. 树节点关闭和打开的Image,旋转框CheckBox,内容TextView

> <RelativeLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="wrap_content" android:minHeight="40dip" > <ImageView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:src="@drawable/tree_econpand" /> <CheckBox android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/id_treenode_icon" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/check_box_bg" android:button="@null" android:focusable="false" /> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/id_treeNode_check" android:text="@string/app_name" android:textSize="18sp" /> </RelativeLayout></span>


二.java代码实现

1.实体类MyNodeBean,内容能够依据详细情况增加


2.主函数MainActivity.java


3.ListView实现适配器MyTreeListViewAdapter.java

该适配器继承TreeListViewAdapter<T>,在该适配器中仅仅实现方法

其它。父类中载入


三、树节点实现的方法

1.树节点实体类Node.java

2.自己定义继承于BaseAdapter的适配器TreeListViewAdapter<T>


3.Node节点实现帮助类TreeHelper.java


以上就是本文的全部内容。

源代码地址:http://download.csdn.net/detail/a123demi/8109643




相关文章:

  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-22
  • 2022-12-23
  • 2021-08-09
  • 2022-12-23
相关资源
相似解决方案