天天看點

Android開發進階元件--ExpandableListView(可伸展的清單元件)

[size=medium][color=gray]1、在Android開發中,有時候希望對清單項可以分組管理并實作收縮功能,例如QQ在使用時,有“我的好友”、“家人”、“同學”等分組,單擊其中一項會展開,再單擊一次又縮回去。要實作這種功能,就得使用到我們今天的主角ExpandableListView元件了。

2、該元件層次結構關系如下:

java.lang.Object

android.view.View

android.view.ViewGroup

android widget.AdapterView<T extends android.widget.Adapter>

android.widget.AbsListView

android.widget.ListView

android.widget.ExpandableListView

3、每一個可擴充項旁邊都有一個提示符(箭頭等)用來說明該清單項目前的狀态,可以使用方法:setChildIndicator(Drawable)和setGroupIndicator(Drawable)(或相應的XML檔案的屬性)去設定這些提示符的樣式。注意:在XML布局檔案中,一般不對ExpandableListView的android:layout_height屬性使用wrap_content,否則可能會報錯。

與ListView一樣ExpandableListView也需要一個擴充卡做橋梁來提供資料,ExpandableListView是一個垂直滾動顯示兩級清單項,它可以有兩層,每層都能夠獨立的展開并顯示其子項。BaseExpandableListAdapter是一個用在ExpandableListView元件的擴充卡。

4、建立布局檔案

<?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">

<TextView

android:textSize="24sp"

android:gravity="center"

android:text="花名冊"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

<ExpandableListView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/list"

android:background="#abcdef"/>

</LinearLayout>

5、修改ExpandableActivity.java檔案

package xiao.fuyan.testapp;

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.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

public class ExpandableActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.expandable_xml);

//

ExpandableListAdapter adapter = new BaseExpandableListAdapter() {

int[] logos = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};

private String[] generalsTypes = new String[]{"1", "2", "3"};

private String[][] generals = new String[][]{

{"a", "b", "c", "d"},

{"e", "f", "g", "h"},

{"i", "j", "k", "l"}

};

private int[][] generalsLogos = new int[][]{

{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher},

{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher},

{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}

};

TextView getTextView(){

AbsListView.LayoutParams lp = new AbsListView.LayoutParams(

ViewGroup.LayoutParams.FILL_PARENT, 64

);

TextView textView = new TextView(ExpandableActivity.this);

textView.setLayoutParams(lp);

textView.setGravity(Gravity.CENTER_VERTICAL);

textView.setPadding(36, 0, 0, 0);

textView.setTextSize(16);

textView.setTextColor(Color.BLACK);

return textView;

}

@Override

public int getGroupCount() {

return generalsTypes.length;

}

@Override

public int getChildrenCount(int groupPosition) {

return generals[groupPosition].length;

}

@Override

public Object getGroup(int groupPosition) {

return generalsTypes[groupPosition];

}

@Override

public Object getChild(int groupPosition, int childPosition) {

return generals[groupPosition][childPosition];

}

@Override

public long getGroupId(int groupPosition) {

return groupPosition;

}

@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(ExpandableActivity.this);

ll.setOrientation(LinearLayout.HORIZONTAL);

ImageView logo = new ImageView(ExpandableActivity.this);

logo.setImageResource(logos[groupPosition]);

logo.setPadding(20, 0, 0, 0);

ll.addView(logo);

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(ExpandableActivity.this);

ll.setOrientation(LinearLayout.HORIZONTAL);

ImageView generalLogo = new ImageView(ExpandableActivity.this);

generalLogo.setImageResource(generalsLogos[groupPosition][childPosition]);

ll.addView(generalLogo);

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.list);

expandableListView.setAdapter(adapter);

}

}

[/color][/size]