本篇文章為大家展示了使用ExpandableListView怎么實(shí)現(xiàn)一個二級列表,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),鷹潭企業(yè)網(wǎng)站建設(shè),鷹潭品牌網(wǎng)站建設(shè),網(wǎng)站定制,鷹潭網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,鷹潭網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
xml創(chuàng)建一個 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); //初始化數(shù)據(jù) 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); //復(fù)用控件 groupView.name=(TextView) convertView.findViewById(R.id.one_name); //綁定 convertView.setTag(groupView); }else { groupView = (GroupView) convertView.getTag(); } //給控件設(shè)置值 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); //復(fù)用控件 Holder.text_name=(TextView) convertView.findViewById(R.id.tow_name); //綁定 convertView.setTag(Holder); }else { Holder = (ViewHolder) convertView.getTag(); } //給控件設(shè)置值 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 + "]"; } }
上述內(nèi)容就是使用ExpandableListView怎么實(shí)現(xiàn)一個二級列表,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。