安卓ExpandableListView实现多组列表,类似于QQ分组

先直接上图,

1:、展开的效果图:





2:后面的是点击收缩的效果图。


3:下面就是代码部分:

我们要定义2个集合用来存储数据,一个是groupItem的数据,一个是childItem的数据

private List<Map<String, Object>> groupList = new ArrayList<Map<String, Object>>();// 分组名称
public List<List<Map<String, Object>>> childList = new ArrayList<List<Map<String, Object>>>();// 详细名称


定义一个控件:

public ExpandableListView expandablelistview; // 2层列表

将我们获取到的数据填充到里面去:

               adapter = new JingCaiZuQiuWinBeatLotteryAdapter(MainActivity.this, winBeatLotteryMain, groupList, childList);
expandablelistview.setAdapter(adapter);
expandablelistview.setGroupIndicator(null);// 去掉group控件默认的箭头


// 分组展开
expandablelistview.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
// expandablelistview.expandGroup(groupPosition);
}
});
// 分组关闭
expandablelistview.setOnGroupCollapseListener(new OnGroupCollapseListener() {
public void onGroupCollapse(int groupPosition) {
// expandablelistview.collapseGroup(groupPosition);
}
});
// 子项单击
/*
* expandablelistview.setOnChildClickListener(new OnChildClickListener()
* { public boolean onChildClick(ExpandableListView arg0, View arg1, int
* groupPosition, int childPosition, long arg4) { return false; } });
*/

//全部展开,默认
int groupCount = expandablelistview.getCount();
for (int i = 0; i < groupCount; i++) {
expandablelistview.expandGroup(i);
}


其实很简单,在这里我附上我的一个小demo源代码。里面有注释。在我的资源里面有下载 http://download.csdn.net/detail/lgx06/8291627。 




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package wjq.WidgetDemo; import android.app.ExpandableListActivity; import android.os.Bundle; import android.provider.Contacts.People; import android.util.Log; import android.view.ContextMenu; import android.view.Gravity; 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.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; import android.widget.Toast; import android.widget.ExpandableListView.ExpandableListContextMenuInfo; public class ExpandableListDemo extends ExpandableListActivity{ private ExpandableListAdapter adapter; //private MyExpandableListAdapter MyAdapter=new MyExpandableListAdapter(); /* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); adapter=new MyExpandableListAdapter(); setListAdapter(adapter); registerForContextMenu(getExpandableListView()); } /* (non-Javadoc) * @see android.app.ExpandableListActivity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo) */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("菜单"); menu.add(0, 0, 0, "Action"); } /* (non-Javadoc) * @see android.app.Activity#onContextItemSelected(android.view.MenuItem) */ @Override public boolean onContextItemSelected(MenuItem item) { boolean flag=false; // TODO Auto-generated method stub ExpandableListContextMenuInfo menuInfo=(ExpandableListContextMenuInfo)item.getMenuInfo(); String title=((TextView)menuInfo.targetView).getText().toString(); int type=ExpandableListView.getPackedPositionType(menuInfo.packedPosition); if (type==ExpandableListView.PACKED_POSITION_TYPE_CHILD) { int groupPos =ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition); int childPos =ExpandableListView.getPackedPositionChild(menuInfo.packedPosition); CharSequence str="您单击了"+title; Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); Log.i("tag", "Run Hereing..."); flag= true; } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { int groupPos = ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition); CharSequence cs="您单击了"+title; Toast.makeText(this, cs, Toast.LENGTH_SHORT).show(); Log.i("tag", "Run Here..."); flag= true; } return flag; } public class MyExpandableListAdapter extends BaseExpandableListAdapter { // Sample data set. children[i] contains the children (String[]) for groups[i]. public String[] groups = { "我的好友", "新疆同学", "亲戚", "同事" }; public String[][] children = { { "胡算林", "张俊峰", "王志军", "二人" }, { "李秀婷", "蔡乔", "别高", "余音" }, { "摊派新", "张爱明" }, { "马超", "司道光" } }; public Object getChild(int groupPosition, int childPosition) { return children[groupPosition][childPosition]; } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public int getChildrenCount(int groupPosition) { return children[groupPosition].length; } public TextView getGenericView() { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textView = new TextView(ExpandableListDemo.this); textView.setLayoutParams(lp); // Center the text vertically textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position textView.setPadding(36, 0, 0, 0); return textView; } public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getGenericView(); textView.setText(getChild(groupPosition, childPosition).toString()); return textView; } public Object getGroup(int groupPosition) { return groups[groupPosition]; } public int getGroupCount() { return groups.length; } public long getGroupId(int groupPosition) { return groupPosition; } public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textView = getGenericView(); textView.setText(getGroup(groupPosition).toString()); return textView; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } public boolean hasStableIds() { return true; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值