如果希望展示的列表可以收縮和展開,就像QQ好友列表一樣,我們可以使用ExpandableListView。
一、設計界面
1、布局文件
打開activity_main.xml文件。
輸入以下代碼:
[html] view plain copy
- <?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="match_parent"
- android:orientation="vertical" >
- <ExpandableListView
- android:id="@+id/province"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#EFEFEF">
- </ExpandableListView>
- </LinearLayout>
二、程序文件
打開“src/com.genwoxue.expandable/MainActivity.java”文件。
然後輸入以下代碼:
[java] view plain copy
- package com.genwoxue.expandablelistview;
-
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.ExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.ExpandableListView.OnChildClickListener;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
-
- public class MainActivity extends Activity {
-
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
- //設置組視圖的顯示文字
- private String[] province = new String[] { "河南省", "河北省", "山東省","山西省" };
- //子視圖顯示文字
- private String[][] city = new String[][] {
- { "鄭州市", "開封市", "新鄉市", "安陽市", "南陽市"},
- { "石家莊市", "邯鄲市", "保定市", "廊坊市"},
- { "濟南市", "青島市", "日照市", "煙台市", "威海市" },
- { "太原市", "大同市", "晉城市", "呂梁市", "長治市" }
- };
-
- //自己定義一個獲得文字信息的方法
- TextView getTextView() {
- AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
- ViewGroup.LayoutParams.MATCH_PARENT, 64);
- TextView textView = new TextView(MainActivity.this);
- textView.setLayoutParams(lp);
- textView.setGravity(Gravity.CENTER_VERTICAL);
- textView.setPadding(36, 0, 0, 0);
- textView.setTextSize(20);
- textView.setTextColor(Color.BLACK);
- return textView;
- }
-
-
- //重寫ExpandableListAdapter中的各個方法
- @Override
- public int getGroupCount() {
- return province.length;
- }
-
- @Override
- public Object getGroup(int groupPosition) {
- return province[groupPosition];
- }
-
- @Override
- public long getGroupId(int groupPosition) {
- return groupPosition;
- }
-
- @Override
- public int getChildrenCount(int groupPosition) {
- return city[groupPosition].length;
- }
-
- @Override
- public Object getChild(int groupPosition, int childPosition) {
- return city[groupPosition][childPosition];
- }
-
- @Override
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
-
- @Override
- public boolean hasStableIds() {
- return true;
- }
-
- @Override
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- LinearLayout ll = new LinearLayout(MainActivity.this);
- ll.setOrientation(0);
- TextView textView = getTextView();
- textView.setTextColor(Color.BLACK);
- textView.setText(getGroup(groupPosition).toString());
- ll.addView(textView);
-
- return ll;
- }
-
- @Override
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- LinearLayout ll = new LinearLayout(MainActivity.this);
- ll.setOrientation(0);
- TextView textView = getTextView();
- textView.setText(getChild(groupPosition, childPosition).toString());
- ll.addView(textView);
- return ll;
- }
-
- @Override
- public boolean isChildSelectable(int groupPosition,int childPosition) {
- return true;
- }
-
- };
-
- ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.province);
- expandableListView.setAdapter(adapter);
-
- //設置item點擊的監聽器
- expandableListView.setOnChildClickListener(new OnChildClickListener() {
-
- @Override
- public boolean onChildClick(ExpandableListView parent, View v,
- int groupPosition, int childPosition, long id) {
-
- Toast.makeText(
- MainActivity.this,
- adapter.getChild(groupPosition, childPosition).toString(),
- Toast.LENGTH_SHORT).show();
-
- return false;
- }
- });
- }
- }
三、運行結果