类似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;
}
}
<?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>
使用还是挺简单的。
源码下载: