Android 中使用ExpandableListView 實現(xiàn)分組的實例
Android 中使用ExpandableListView 實現(xiàn)分組
一個視圖顯示垂直滾動兩級列表中的條目。這不同于列表視圖,允許兩個層次,類似于QQ的好友分組。要實現(xiàn)這個效果的整體思路為:
1.要給ExpandableListView 設(shè)置適配器,那么必須先設(shè)置數(shù)據(jù)源。
2.數(shù)據(jù)源,就是此處的適配器類,此方法繼承了BaseExpandableListAdapter,它是ExpandableListView的一個子類。需要重寫里面的多個方法。方法的意思,代碼中都有詳細(xì)的注釋。數(shù)據(jù)源中,用到了自定義的View布局,此時根據(jù)自己的需求,來設(shè)置組和子項的布局樣式。getChildView()和getGroupView()方法設(shè)置自定義布局。
3.數(shù)據(jù)源設(shè)置好,直接給ExpandableListView.setAdapter()即可實現(xiàn)此收縮功能。
下面是我自己簡單做的一個顯示效果:

layout中主視圖expandable_layout.xml(ExpandableListView布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/el">
</ExpandableListView>
</LinearLayout>
Layout中g(shù)roup_layout.xml(分組組名展示布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:id="@+id/iv_group" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_group" />
</LinearLayout>
Layout中child_layout.xml(子菜單item布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:id="@+id/iv_item" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_item" />
</LinearLayout>
Layout中alertdialog_layout.xml(AlertDialog自定義顯示布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et"
android:hint="請輸入想對他說的話"/>
</LinearLayout>
Activity中Java實現(xiàn)代碼(ExpandableListViewDemo.java):
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by panchengjia on 2016/12/2.
*/
public class ExpandableListViewDemo extends AppCompatActivity {
ExpandableListView el;
//定義分組名以及對應(yīng)的圖片數(shù)組,需一一對應(yīng)
String[] country={"魏國","蜀國","吳國"};
int[] icon={R.mipmap.wei,R.mipmap.shu,R.mipmap.wu};
//使用二維定義組內(nèi)成員以及對應(yīng)頭像,同樣需要以一一對應(yīng)
String[][] heros={{"司馬懿","郭嘉","夏侯惇","甄姬"},{"劉備","趙云","張飛"},{"孫權(quán)","周瑜"}};
int[][] icons={{R.mipmap.simayi,R.mipmap.guojia,R.mipmap.xiahoudun,R.mipmap.zhenji},
{R.mipmap.liubei,R.mipmap.zhaoyun,R.mipmap.zhangfei},{R.mipmap.sunquan,R.mipmap.zhouyu}};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_layout);
el= (ExpandableListView) findViewById(R.id.el);
//設(shè)置點擊下拉子菜單的監(jiān)聽事件
el.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//獲取分組成員的姓名
TextView tv = (TextView) v.findViewById(R.id.tv_item);
String name = tv.getText().toString();
//調(diào)用show方法(自定義AlertDialog)
show(v,name);
return false;
}
});
el.setAdapter(new MyAdapter());//設(shè)置自定義適配器
}
//定義show方法調(diào)出AlertDialog(不再贅述,詳情請看前期相關(guān)博文,文章末尾有鏈接)
public void show(View v,String name){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(name);//設(shè)置標(biāo)題名為傳入的字符串(分組內(nèi)點擊對應(yīng)的人物名)
View msg = getLayoutInflater().inflate(R.layout.altertdialog_layout,null);
builder.setView(msg);
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(ExpandableListViewDemo.this, "已發(fā)送", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(ExpandableListViewDemo.this, "不想說了", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
//設(shè)置自定義適配器
class MyAdapter extends BaseExpandableListAdapter{
//獲取國家個數(shù)
@Override
public int getGroupCount() {
return country.length;
}
//獲取每個國家對應(yīng)的人數(shù)
@Override
public int getChildrenCount(int groupPosition) {
return heros[groupPosition].length;
}
//獲取對應(yīng)國家名
@Override
public Object getGroup(int groupPosition) {
return country[groupPosition];
}
//獲取對應(yīng)國家對應(yīng)的任務(wù)
@Override
public Object getChild(int groupPosition, int childPosition) {
return heros[groupPosition][childPosition];
}
//獲取選擇的國家對應(yīng)數(shù)組下標(biāo)
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//獲取選擇的任務(wù)對應(yīng)數(shù)組下標(biāo)
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//表示人物和國家ID是否穩(wěn)定的更改底層數(shù)據(jù)
@Override
public boolean hasStableIds() {
return true;
}
//獲取一個視圖顯示國家名以及對應(yīng)的圖標(biāo)(ListView適配器優(yōu)化)
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder vh;
if(convertView==null){
convertView=getLayoutInflater().inflate(R.layout.group_layout,null);
vh=new ViewHolder();
vh.tv= (TextView) convertView.findViewById(R.id.tv_group);
vh.iv= (ImageView) convertView.findViewById(R.id.iv_group);
convertView.setTag(vh);
}
vh= (ViewHolder) convertView.getTag();
vh.tv.setText(country[groupPosition]);
vh.iv.setImageResource(icon[groupPosition]);
return convertView;
}
//獲取一個視圖顯示國家對應(yīng)人物以及對應(yīng)的圖標(biāo)(ListView適配器優(yōu)化)
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder vh;
if(convertView==null){
convertView=getLayoutInflater().inflate(R.layout.child_layout,null);
vh=new ViewHolder();
vh.tv= (TextView) convertView.findViewById(R.id.tv_item);
vh.iv= (ImageView) convertView.findViewById(R.id.iv_item);
convertView.setTag(vh);
}
vh= (ViewHolder) convertView.getTag();
vh.tv.setText(heros[groupPosition][childPosition]);
vh.iv.setImageResource(icons[groupPosition][childPosition]);
return convertView;
}
class ViewHolder{
ImageView iv;
TextView tv;
}
//設(shè)置子選項(對應(yīng)人物)是可選的
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
補(bǔ)充說明:
java實現(xiàn)代碼中用到自定義適配器ListView的優(yōu)化,優(yōu)化步驟如下:
(1)使用固定寬高(match_parent)的ListView,有助于在填充item(ListView中每行的布局)時避免重復(fù)渲染ListView組件,導(dǎo)致重復(fù)多次調(diào)用getView方法。
(2)Convertview用來重復(fù)使用已被隱藏的item對象,從而避免重復(fù)創(chuàng)建每個item的view對象。
(3)使用ViewHolder優(yōu)化提高容器中查找組件的效率。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android ExpandableListView雙層嵌套實現(xiàn)三級樹形菜單
- 完美實現(xiàn)ExpandableListView二級分欄效果
- ExpandableListView實現(xiàn)二級列表購物車
- ExpandableListView實現(xiàn)簡單二級列表
- Android中使用Expandablelistview實現(xiàn)微信通訊錄界面
- Android 關(guān)于ExpandableListView刷新問題的解決方法
- Android 關(guān)于ExpandableListView去掉里頭分割線的方法
- Android UI控件ExpandableListView基本用法詳解
- Android使用ExpandableListView實現(xiàn)三層嵌套折疊菜單
相關(guān)文章
Android自定義控件開發(fā)實戰(zhàn)之實現(xiàn)ListView下拉刷新實例代碼
這篇文章主要介紹了Android自定義控件開發(fā)實戰(zhàn)之實現(xiàn)ListView下拉刷新實例代碼的相關(guān)資料,需要的朋友可以參考下2016-04-04
在Android設(shè)備上搭建Web服務(wù)器的方法
本篇文章主要介紹了在Android設(shè)備上搭建Web服務(wù)器的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Android編程實現(xiàn)簡單設(shè)置按鈕顏色的方法
這篇文章主要介紹了Android編程實現(xiàn)簡單設(shè)置按鈕顏色的方法,涉及Android控件布局與屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
第1個Android應(yīng)用程序 Android制作簡單單頁導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了第1個Android應(yīng)用程序PhonewordApp:Android制作簡單單頁導(dǎo)航,感興趣的小伙伴們可以參考一下2016-06-06
Android手勢滑動實現(xiàn)ImageView縮放圖片大小
這篇文章主要為大家詳細(xì)介紹了Android手勢滑動實現(xiàn)ImageView縮放圖片大小的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-02-02
Android開發(fā)中Activity創(chuàng)建跳轉(zhuǎn)及傳值的方法
這篇文章主要介紹了Android開發(fā)中Activity創(chuàng)建跳轉(zhuǎn)及傳值的方法的相關(guān)資料,需要的朋友可以參考下2016-05-05
Android編程實現(xiàn)ImageView圖片拋物線動畫效果的方法
這篇文章主要介紹了Android編程實現(xiàn)ImageView圖片拋物線動畫效果的方法,實例分析了Android實現(xiàn)拋物線運(yùn)動的算法原理與相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10

