ExpandableListView實(shí)現(xiàn)簡(jiǎn)單二級(jí)列表
本文實(shí)例為大家分享了ExpandableListView實(shí)現(xiàn)簡(jiǎn)單二級(jí)列表的具體代碼,供大家參考,具體內(nèi)容如下
xml創(chuàng)建一個(gè) 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的一級(jí)列表布局
<?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="我是一級(jí)列表"/> </RelativeLayout>
ExpandableListView的二級(jí)列表布局
<?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(" 項(xiàng)羽"));
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();
//獲取一級(jí)列表的布局
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();
//獲取二級(jí)列表的布局
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_封裝類(lèi)
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 + "]";
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android ExpandableListView雙層嵌套實(shí)現(xiàn)三級(jí)樹(shù)形菜單
- 完美實(shí)現(xiàn)ExpandableListView二級(jí)分欄效果
- ExpandableListView實(shí)現(xiàn)二級(jí)列表購(gòu)物車(chē)
- Android中使用Expandablelistview實(shí)現(xiàn)微信通訊錄界面
- Android 關(guān)于ExpandableListView刷新問(wèn)題的解決方法
- Android 關(guān)于ExpandableListView去掉里頭分割線(xiàn)的方法
- Android 中使用ExpandableListView 實(shí)現(xiàn)分組的實(shí)例
- Android UI控件ExpandableListView基本用法詳解
- Android使用ExpandableListView實(shí)現(xiàn)三層嵌套折疊菜單
相關(guān)文章
Android實(shí)現(xiàn)幾種推送方式解決方案
推送功能在手機(jī)開(kāi)發(fā)中應(yīng)用的場(chǎng)景是越來(lái)起來(lái)了,本篇文章主要介紹了Android實(shí)現(xiàn)幾種推送方式解決方案 ,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
Android 個(gè)人理財(cái)工具一:項(xiàng)目概述與啟動(dòng)界面的實(shí)現(xiàn)
本文主要介紹Android 開(kāi)發(fā)個(gè)人理財(cái)工具項(xiàng)目概述與啟動(dòng)界面的實(shí)現(xiàn),這里主要對(duì)實(shí)現(xiàn)項(xiàng)目的流程做了詳細(xì)概述,并對(duì)啟動(dòng)界面簡(jiǎn)單實(shí)現(xiàn),有需要的小伙伴可以參考下2016-08-08
Android項(xiàng)目中使用HTTPS配置的步驟詳解
這篇文章主要給大家介紹了關(guān)于Android項(xiàng)目中使用HTTPS配置步驟的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06
Android編程基于Contacts讀取聯(lián)系人的方法(附demo源碼)
這篇文章主要介紹了Android編程基于Contacts讀取聯(lián)系人的方法,實(shí)例分析了Contacts讀取的實(shí)現(xiàn)方法及權(quán)限設(shè)置方法,并附帶了完整實(shí)例供讀者下載參考,需要的朋友可以參考下2015-12-12
Android IPC機(jī)制ACtivity綁定Service通信代碼實(shí)例
這篇文章主要介紹了Android IPC機(jī)制ACtivity綁定Service通信代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
android自定義toast(widget開(kāi)發(fā))示例
這篇文章主要介紹了android自定義toast(widget開(kāi)發(fā))示例,需要的朋友可以參考下2014-03-03
Android手勢(shì)ImageView三部曲 第二部
這篇文章主要為大家詳細(xì)介紹了Android手勢(shì)ImageView三部曲的第二部,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

