在Android开发的过程中,大家希望能实现可折叠列表视图
1.list.xml 源码
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/background_color"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:id="@+id/expandable_relative_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<TextView
android:id="@+id/list_title_text"
android:textSize="22.0sp"
android:textColor="@color/line_black"
android:text="可扩展LISTVIEW学习"
android:ellipsize="marquee"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
>
</TextView>
</RelativeLayout>
<ExpandableListView
android:id="@+id/expandable_list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#00000000"
android:dividerHeight="1.0dip"
android:layout_below="@+id/expandable_relative_layout"
>
</ExpandableListView
>
</RelativeLayout>
注:去除GroupView自带折叠打开图片
添加android:groupIndicator="@null"
<ExpandableListView
android:groupIndicator="@null"
android:id="@+id/expandable_list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#00000000"
android:dividerHeight="1.0dip"
android:layout_below="@+id/expandable_relative_layout"
>
2.源代码
package com.android.expand;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
importandroid.widget.ExpandableListView.ExpandableListContextMenuInfo;
public class ExpandableList1 extends Activity {
public final static String tag ="ExpandableList1";
public final static String NAME = "姓名:";
public final static String PHONE = "电话:";
public final static String SEX = "性别:";
private ExpandableListView listView;
private TextView mTitle;
publicList<String> group;
publicList<List<String>>child;
public ExpandInfoAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
findView();
initListView();
setListViewOnChildClickListener();
// 注册长按选项弹出莱单
registerForContextMenu(listView);
}
public void findView() {
listView = (ExpandableListView)findViewById(R.id.expandable_list_view);
mTitle = (TextView)this.findViewById(R.id.list_title_text);
}
public void initListView() {
initialOther();
addItemByValue("张三","051782214", "男");
addItemByValue("李四", "110","男");
addItemByValue("王二", "132","女");
addItemByValue("麻子","13321234562", "女");
adapter = newExpandInfoAdapter(this);
listView.setAdapter(adapter);
}
public void setListViewOnChildClickListener(){
listView
.setOnChildClickListener(newExpandableListView.OnChildClickListener() {
publicboolean onChildClick(ExpandableListView parent,
Viewv, int groupPosition, int childPosition,
longid) {
Logs.i(tag,"groupPosition:" + groupPosition);
Logs.i(tag,"childPosition:" + childPosition);
Logs.i(tag,"id:" + id);
returntrue;
}
});
}
public void initialOther() {
group = newArrayList<String>();
child = newArrayList<List<String>>();
}
public void addItemByValue(String n, Stringc1, String c2) {
group.add(n);
List<String>item = new ArrayList<String>();
item.add(NAME + n);
item.add(PHONE + c1);
item.add(SEX + c2);
child.add(item);
}
public class ExpandInfoAdapter extendsBaseExpandableListAdapter {
LayoutInflater mInflater;
Bitmap mIcon1;
Activity activity;
publicExpandInfoAdapter(Activity a) {
activity =a;
mInflater =LayoutInflater.from(activity);
mIcon1 =BitmapFactory.decodeResource(activity.getResources(),
R.drawable.icon48x48_1);
}
//++++++++++++++++++++++++++++++++++++++++++++
// child's stub
@Override
public Object getChild(intgroupPosition, int childPosition) {
returnchild.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(intgroupPosition, int childPosition) {
returnchildPosition;
}
@Override
public int getChildrenCount(intgroupPosition) {
returnchild.get(groupPosition).size();
}
@Override
public View getChildView(intgroupPosition, int childPosition,
booleanisLastChild, View convertView, ViewGroup parent) {
//return
//getChildViewStub(child.get(groupPosition).get(childPosition)
//.toString());
returngetView(groupPosition, childPosition, convertView, parent);
}
public TextViewgetChildViewStub(String s) {
// Layoutparameters for the ExpandableListView
AbsListView.LayoutParamslp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,64);
TextViewtext = new TextView(activity);
text.setLayoutParams(lp);
text.setTextSize(20);
text.setGravity(Gravity.CENTER_VERTICAL| Gravity.LEFT);
text.setPadding(36,0, 0, 0);
text.setText(s);
returntext;
}
public View getView(intgroupPosition, int childPosition,
ViewconvertView, ViewGroup parent) {
ViewHolderholder;
if(convertView == null) {
convertView= mInflater.inflate(R.layout.list_item_icon_text,
null);
holder= new ViewHolder();
holder.text= (TextView) convertView.findViewById(R.id.text);
holder.icon= (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else{
holder= (ViewHolder) convertView.getTag();
}
String name =child.get(groupPosition).get(childPosition);
holder.text.setText(name);
holder.icon.setImageBitmap(mIcon1);
returnconvertView;
}
//++++++++++++++++++++++++++++++++++++++++++++
// group's stub
@Override
public Object getGroup(intgroupPosition) {
returngroup.get(groupPosition);
}
@Override
public int getGroupCount(){
returngroup.size();
}
@Override
public long getGroupId(intgroupPosition) {
returngroupPosition;
}
@Override
public View getGroupView(intgroupPosition, boolean isExpanded,
ViewconvertView, ViewGroup parent) {
returngetGroupViewStub(getGroup(groupPosition).toString());
}
public TextViewgetGroupViewStub(String s) {
// Layoutparameters for the ExpandableListView
AbsListView.LayoutParamslp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,64);
TextViewtext = new TextView(activity);
text.setLayoutParams(lp);
text.setTextSize(20);
text.setGravity(Gravity.CENTER_VERTICAL| Gravity.LEFT);
text.setPadding(36,0, 0, 0);
text.setText(s);
returntext;
}
// Indicate whether Group isExpanded or Collapsed
public void onGroupExpanded(intgroupPosition) {
}
public voidonGroupCollapsed(int groupPosition) {
}
@Override
public booleanisChildSelectable(int groupPosition, int childPosition) {
returntrue;
}
@Override
public boolean hasStableIds(){
returnfalse;
}
}
static class ViewHolder {
TextView text;
ImageView icon;
}
@Override
public void onCreateContextMenu(ContextMenu menu,View v,
ContextMenuInfomenuInfo) {
menu.setHeaderTitle("Samplemenu");
menu.add(0, 0, 0, "menu1order0");
menu.add(0, 0, 1, "menu2order1");
menu.add(0, 1, 1, "menu3item1");
menu.add(1, 1, 1, "menu4group1");
}
@Override
public boolean onContextItemSelected(MenuItemitem) {
Logs.i(tag, "GroupID" +item.getGroupId() + ", itemId :"
+item.getItemId() + " order :" + item.getOrder());
ExpandableListContextMenuInfoinfo = (ExpandableListContextMenuInfo) item
.getMenuInfo();
int type =ExpandableListView
.getPackedPositionType(info.packedPosition);
if (type ==ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
String title= ((ViewHolder) info.targetView.getTag()).text
.getText().toString();
int groupPos= ExpandableListView
.getPackedPositionGroup(info.packedPosition);
int childPos= ExpandableListView
.getPackedPositionChild(info.packedPosition);
Toast.makeText(
this,
title+ ": Child " + childPos + " clicked in group"
+groupPos, Toast.LENGTH_SHORT).show();
returntrue;
} else if (type ==ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
String titles= ((TextView) info.targetView).getText().toString();
int groupPos= ExpandableListView
.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this,titles + ": Group " + groupPos + " clicked",
Toast.LENGTH_SHORT).show();
returntrue;
}
return false;
}
}
注:控制点击groupview不展开childView方法
expandListView
.setOnGroupClickListener(newExpandableListView.OnGroupClickListener() {
@Override
publicboolean onGroupClick(ExpandableListView parent,
Viewv, int groupPosition, long childPosition) {
Toast.makeText(
EmailExpandListActivity.this,
"[GroupClick]:" + groupPosition + " isEmpty :"
+child.get(groupPosition).isEmpty(),
Toast.LENGTH_SHORT).show();
if(groupPosition == 0) {
return true;
}else if (child.get(groupPosition).isEmpty()) {//isEmpty没有
returntrue;
}else {
returnfalse;
}
}
});