使用ExpandableListView怎么实现一个二级列表
本篇文章为大家展示了使用ExpandableListView怎么实现一个二级列表,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联公司是一家集网站建设,鹰潭企业网站建设,鹰潭品牌网站建设,网站定制,鹰潭网站建设报价,网络营销,网络优化,鹰潭网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
xml创建一个 ExpandableListView
ExpandableListView的一级列表布局
ExpandableListView的二级列表布局
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 ListallList; private List > 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
(); allList.add("列表①"); allList.add("列表②"); allList.add("列表③"); list=new ArrayList >(); List
list1=new ArrayList (); list1.add(new Person(" 虞姬")); list1.add(new Person(" 甄姬")); list1.add(new Person(" 阿狸")); list1.add(new Person(" 狐狸")); List list2=new ArrayList (); list2.add(new Person(" 李白")); list2.add(new Person(" 項羽")); list2.add(new Person(" 荊軻")); list2.add(new Person(" 曹操")); List list3=new ArrayList (); 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 + "]"; } }
上述内容就是使用ExpandableListView怎么实现一个二级列表,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
本文题目:使用ExpandableListView怎么实现一个二级列表
转载源于:http://ybzwz.com/article/jchpjh.html