ExpandableListView控件的使用

      类似QQ手机,点击一个选项展现一个列表。Android中控件ExpandableListView可实现类似于此功能。


主界面布局文件:

<RelativeLayout xmlns: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:background="#fff" >

    <ExpandableListView
        android:id="@+id/id_expandablelv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

类似于ListView控件,直接获取该控件id,设置其适配器即可。

package com.example.expandablelistviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {

	private ExpandableListView mExpandableListView;
	private ExpandableAdapter mExpandableAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mExpandableListView = (ExpandableListView) findViewById(R.id.id_expandablelv);
		mExpandableAdapter = new ExpandableAdapter(this);
		mExpandableListView.setAdapter(mExpandableAdapter);
	}
}

适配器文件:

package com.example.expandablelistviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableAdapter extends BaseExpandableListAdapter {

	int[] logos = new int[]{
            R.drawable.word,
            R.drawable.excel,
            R.drawable.email,
            R.drawable.ppt
    };
    private String[] armTypes = new String[]{
            "WORD", "EXCEL", "EMAIL", "PPT"
    };
    private String[][] arms = new String[][]{
            {"文档编辑", "文档排版", "文档处理", "文档打印"},
            {"表格编辑", "表格排版", "表格处理", "表格打印"},
            {"收发邮件", "管理邮箱", "登录登出", "注册绑定"},
            {"演示编辑", "演示排版", "演示处理", "演示打印"},
    };
    
    LayoutInflater mLayoutInflater;
    
    public ExpandableAdapter(Context mContext) {
		mLayoutInflater = LayoutInflater.from(mContext);
	}
    
	@Override
	public int getGroupCount() {
		//返回类别长度
		return armTypes.length;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return arms[groupPosition].length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return armTypes[groupPosition];
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return arms[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) {
		convertView = mLayoutInflater.inflate(R.layout.item_group, null);
		ImageView logo = (ImageView) convertView.findViewById(R.id.id_group_logo);
		logo.setImageResource(logos[groupPosition]);
		
		TextView text = (TextView) convertView.findViewById(R.id.id_group_tv);
		text.setText(getGroup(groupPosition).toString());
		
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
		convertView = mLayoutInflater.inflate(R.layout.item_sub, null);
		TextView tv = (TextView) convertView.findViewById(R.id.id_sub_tv);
		tv.setText(getChild(groupPosition, childPosition).toString());
        return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}


自定义的group的xml文件

<?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="50dp"
    android:orientation="horizontal" >
    
    <ImageView 
        android:id="@+id/id_group_logo"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="30dp"
        android:layout_gravity="center_vertical"
        />
    
    <TextView 
        android:id="@+id/id_group_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:paddingLeft="5dp"
        />

</LinearLayout>

自定义每个列表选项布局文件

<?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="45dp"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/id_sub_tv"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal|left"
        android:gravity="center_vertical|left"
        android:layout_marginLeft="30dp"
        android:textSize="20sp"
        />

</LinearLayout>

使用还是挺简单的。

源码下载:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值