日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android UI控件----ExpandableListView的基本用法

發布時間:2025/3/18 Android 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android UI控件----ExpandableListView的基本用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ExpandableListView介紹

ExpandableListView的引入

ExpandableListView可以顯示一個視圖垂直滾動顯示兩級列表中的條目,這不同于列表視圖(ListView)。ExpandableListView允許有兩個層次:一級列表中有二級列表。
比如在手機設置中,對于分類,有很好的效果。手機版QQ也是這樣的效果。

使用ExpandableListView的整體思路

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

(2)數據源,就是此處的適配器類ExpandableAdapter,此方法繼承了BaseExpandableListAdapter,需要重寫里面的10個方法。
數據源中,用到了自定義的View布局,此時根據自己的需求,來設置組和子項的布局樣式。
getChildView()和getGroupView()方法設置自定義布局。

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

ExpandableListView的完整代碼實現

(1)activity_main.xml:在里面放置一個ExpandableListView控件

<?xml version="1.0" encoding="utf-8"?> <RelativeLayoutxmlns: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"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.smyhvae.expandablelistviewdemo.MainActivity"><ExpandableListViewandroid:id="@+id/expandableListView"android:layout_width="match_parent"android:layout_height="wrap_content"/></RelativeLayout>

(2)item_group.xml:一級列表的item的布局

<?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="wrap_content"android:background="#cccccc"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_group"android:layout_width="wrap_content"android:layout_height="30dp"android:gravity="center"android:text="group text"android:textColor="#000000"/></LinearLayout>

(3)item_child.xml:二級列表的item的布局

<?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="wrap_content"android:gravity="center"android:orientation="horizontal"><ImageViewandroid:id="@+id/iv_child"android:layout_width="30dp"android:layout_height="30dp"android:src="@mipmap/ic_launcher"/><TextViewandroid:id="@+id/tv_child"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="item text"android:textColor="#000000"/></LinearLayout>

(4)MainActivity.java:

import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.TextView;public class MainActivity extends Activity {//Viewprivate ExpandableListView expandableListView;//Model:定義的數據private String[] groups = {"A", "B", "C"};//注意,字符數組不要寫成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4,B5"}, {"C1,C2,C3,C4"}}private String[][] childs = {{"A1", "A2", "A3", "A4"}, {"A1", "A2", "A3", "B4"}, {"A1", "A2", "A3", "C4"}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);expandableListView.setAdapter(new MyExpandableListView());}//為ExpandableListView自定義適配器class MyExpandableListView extends BaseExpandableListAdapter {//返回一級列表的個數@Overridepublic int getGroupCount() {return groups.length;}//返回每個二級列表的個數@Overridepublic int getChildrenCount(int groupPosition) { //參數groupPosition表示第幾個一級列表Log.d("smyhvae", "-->" + groupPosition);return childs[groupPosition].length;}//返回一級列表的單個item(返回的是對象)@Overridepublic Object getGroup(int groupPosition) {return groups[groupPosition];}//返回二級列表中的單個item(返回的是對象)@Overridepublic Object getChild(int groupPosition, int childPosition) {return childs[groupPosition][childPosition]; //不要誤寫成groups[groupPosition][childPosition]}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}//每個item的id是否是固定?一般為true@Overridepublic boolean hasStableIds() {return true;}//【重要】填充一級列表@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {if (convertView == null) {convertView = getLayoutInflater().inflate(R.layout.item_group, null);} else {}TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group);tv_group.setText(groups[groupPosition]);return convertView;}//【重要】填充二級列表@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {if (convertView == null) {convertView = getLayoutInflater().inflate(R.layout.item_child, null);}ImageView iv_child = (ImageView) convertView.findViewById(R.id.iv_child);TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child);//iv_child.setImageResource(resId);tv_child.setText(childs[groupPosition][childPosition]);return convertView;}//二級列表中的item是否能夠被選中?可以改為true@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}}

注:請自行完成ConvertView和ViewHolder的優化。

工程文件:(Android Studio 2.1,API 23)http://download.csdn.net/detail/smyhvae/9623708

總結

以上是生活随笔為你收集整理的Android UI控件----ExpandableListView的基本用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。