欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ExpandableListView实现简单二级列表

程序员文章站 2023-11-14 14:30:22
本文实例为大家分享了expandablelistview实现简单二级列表的具体代码,供大家参考,具体内容如下 xml创建一个 expandablelistvie...

本文实例为大家分享了expandablelistview实现简单二级列表的具体代码,供大家参考,具体内容如下

xml创建一个 expandablelistview

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="${relativepackage}.${activityclass}" >
 <expandablelistview 
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/expandablelistview"> 
 </expandablelistview>
 
</relativelayout>

expandablelistview的一级列表布局

<?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="80dp"
 android:orientation="vertical" > 
 <textview 
 android:id="@+id/one_name"
 android:layout_width="wrap_content"
 android:layout_height="40dp"
 android:layout_centerinparent="true"
 android:textsize="20sp"
 android:text="我是一级列表"/>
 
</relativelayout>

expandablelistview的二级列表布局

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="60dp"
 android:orientation="horizontal" >
 
 <imageview 
 android:id="@+id/img"
 android:layout_width="60dp"
 android:layout_height="60dp"
 android:src="@drawable/ic_launcher"/>
 <textview 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/tow_name"
 android:layout_margintop="20dp"
 android:layout_marginleft="20dp"
 android:text="嘻嘻哈哈"/>
</linearlayout>

java代码

package com.example.expandablelistview;
 
import java.util.arraylist;
import java.util.list;
 
import android.r.string;
import android.app.activity;
import android.os.bundle;
import android.view.menu;
import android.view.menuitem;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseexpandablelistadapter;
import android.widget.expandablelistview;
import android.widget.textview;
 
public class mainactivity extends activity {
 private mainactivity.madapder madapder;
 private expandablelistview expandablelistview;
 private list<string> alllist;
 private list<list<person>> list;
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 //获取控件
 expandablelistview=(expandablelistview) findviewbyid(r.id.expandablelistview);
 //初始化数据
 initdata();
 //自定义适配器
 madapder=new madapder();
 expandablelistview.setadapter(madapder);
 }
 public void initdata() {
 alllist=new arraylist<string>();
 alllist.add("列表①");
 alllist.add("列表②");
 alllist.add("列表③");
 list=new arraylist<list<person>>();
 list<person> list1=new arraylist<person>();
 list1.add(new person(" 虞姬"));
 list1.add(new person(" 甄姬"));
 list1.add(new person(" 阿狸"));
 list1.add(new person(" 狐狸"));
 list<person> list2=new arraylist<person>();
 list2.add(new person(" 李白"));
 list2.add(new person(" 項羽"));
 list2.add(new person(" 荊軻"));
 list2.add(new person(" 曹操"));
 list<person> list3=new arraylist<person>();
 list3.add(new person(" 公孙离"));
 list3.add(new person(" 孙尚香"));
 list3.add(new person(" 狄仁杰"));
 list3.add(new person(" 蔡文姬"));
 list.add(list1);
 list.add(list2);
 list.add(list3);
 }
 class madapder extends baseexpandablelistadapter{
 
 @override
 public int getgroupcount() {
 // todo auto-generated method stub
 return alllist.size();
 }
 
 @override
 public int getchildrencount(int groupposition) {
 // todo auto-generated method stub
 return list.get(groupposition).size();
 }
 
 @override
 public object getgroup(int groupposition) {
 // todo auto-generated method stub
 return alllist.get(groupposition);
 }
 
 @override
 public object getchild(int groupposition, int childposition) {
 // todo auto-generated method stub
 return list.get(groupposition).get(childposition);
 }
 
 @override
 public long getgroupid(int groupposition) {
 // todo auto-generated method stub
 return groupposition;
 }
 
 @override
 public long getchildid(int groupposition, int childposition) {
 // todo auto-generated method stub
 return childposition;
 }
 
 @override
 public boolean hasstableids() {
 // todo auto-generated method stub
 return true;
 }
 
 @override
 public view getgroupview(int groupposition, boolean isexpanded,
 view convertview, viewgroup parent) {
 groupview groupview;
 if(convertview==null){
 groupview=new groupview();
 //获取一级列表的布局
 convertview=view.inflate(mainactivity.this,r.layout.item_1, null);
 //复用控件
 groupview.name=(textview) convertview.findviewbyid(r.id.one_name);
 //绑定
 convertview.settag(groupview);
 }else {
 groupview = (groupview) convertview.gettag();
 }
 //给控件设置值
 groupview.name.settext(alllist.get(groupposition));
 return convertview;
 }
 @override
 public view getchildview(int groupposition, int childposition,
 boolean islastchild, view convertview, viewgroup parent) {
 viewholder holder;
 if(convertview==null){
 holder=new viewholder();
 //获取二级列表的布局
 convertview=view.inflate(mainactivity.this,r.layout.item_2, null);
 //复用控件
 holder.text_name=(textview) convertview.findviewbyid(r.id.tow_name);
 //绑定
 convertview.settag(holder);
 }else {
 holder = (viewholder) convertview.gettag();
 }
 //给控件设置值
 holder.text_name.settext(list.get(groupposition).get(childposition).getname());
 return convertview;
 }
 
 @override
 public boolean ischildselectable(int groupposition, int childposition) {
 // todo auto-generated method stub
 return true;
 } 
 }
 class viewholder{
 textview text_name;
 }
 class groupview{
 textview name;
 }
}

java_封装类

package com.example.expandablelistview;
 
public class person {
 string name;
 public person() {
 // todo auto-generated constructor stub
 }
 public person(string name) {
 super();
 this.name = name;
 }
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
 @override
 public string tostring() {
 return "person [name=" + name + "]";
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。