国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

ExpandableListView實現(xiàn)簡單二級列表

2019-10-21 21:35:43
字體:
供稿:網(wǎng)友

本文實例為大家分享了ExpandableListView實現(xiàn)簡單二級列表的具體代碼,供大家參考,具體內(nèi)容如下

xml創(chuàng)建一個 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); //初始化數(shù)據(jù) 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(); } //給控件設(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); //復用控件 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)容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 寻甸| 门源| 荔波县| 抚远县| 翁源县| 新营市| 巴彦淖尔市| 平乐县| 融水| 当涂县| 昭通市| 福贡县| 高台县| 突泉县| 中江县| 讷河市| 武平县| 广平县| 宜兰县| 吐鲁番市| 平凉市| 广德县| 砚山县| 临西县| 兰溪市| 太和县| 西林县| 宣武区| 海兴县| 彰化县| 财经| 晋城| 防城港市| 元氏县| 乌兰浩特市| 浪卡子县| 吉木乃县| 松原市| 沁阳市| 林甸县| 贺州市|