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

首頁 > 系統 > Android > 正文

Android 中使用ExpandableListView 實現分組的實例

2019-12-12 04:26:37
字體:
來源:轉載
供稿:網友

 Android 中使用ExpandableListView 實現分組

一個視圖顯示垂直滾動兩級列表中的條目。這不同于列表視圖,允許兩個層次,類似于QQ的好友分組。要實現這個效果的整體思路為:

1.要給ExpandableListView 設置適配器,那么必須先設置數據源。

2.數據源,就是此處的適配器類,此方法繼承了BaseExpandableListAdapter,它是ExpandableListView的一個子類。需要重寫里面的多個方法。方法的意思,代碼中都有詳細的注釋。數據源中,用到了自定義的View布局,此時根據自己的需求,來設置組和子項的布局樣式。getChildView()和getGroupView()方法設置自定義布局。

3.數據源設置好,直接給ExpandableListView.setAdapter()即可實現此收縮功能。

下面是我自己簡單做的一個顯示效果:

 

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中group_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實現代碼(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;  //定義分組名以及對應的圖片數組,需一一對應  String[] country={"魏國","蜀國","吳國"};  int[] icon={R.mipmap.wei,R.mipmap.shu,R.mipmap.wu};  //使用二維定義組內成員以及對應頭像,同樣需要以一一對應  String[][] heros={{"司馬懿","郭嘉","夏侯

主站蜘蛛池模板:
旺苍县|
宿松县|
泉州市|
丰县|
南宁市|
淳安县|
沧源|
伊宁县|
重庆市|
浮山县|
潢川县|
民勤县|
凤山市|
江孜县|
桑植县|
鄂伦春自治旗|
陈巴尔虎旗|
新田县|
伊春市|
乌兰察布市|
南溪县|
中西区|
望江县|
于都县|
沈丘县|
济源市|
烟台市|
桂林市|
金乡县|
和龙市|
建阳市|
昌图县|
吴忠市|
武定县|
石城县|
吉木萨尔县|
全南县|
盱眙县|
宁乡县|
夏河县|
秦皇岛市|