ExpandableListView可手动添加简单分组功能

来源:互联网 发布:浏览器版本过低 知乎 编辑:程序博客网 时间:2024/04/28 14:07

该片文章是下面文章的延伸版

效果图为

 

ExpandableListView实现简单分组功能


点击打开链接

http://blog.csdn.net/xiaoke804376088/article/details/49885793

java代码

package com.xiaoke.expandablelistview;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.content.Context;import android.graphics.Color;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnGroupClickListener;import android.widget.SimpleExpandableListAdapter;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener {private final String GROUP = "group";private final String CHILD = "child";private ArrayList<HashMap<String, Object>> data;private MyExpand mExpandableListAdapter =null;private EditText etGroup=null;private EditText etChild=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);data = new ArrayList<HashMap<String, Object>>();// 创建分组的信息String[] strGroup = { "A", "B", "C", "D", "E" };for (int i = 0; i < strGroup.length; i++) {HashMap<String, Object> map = new HashMap<String, Object>();map.put(GROUP, strGroup[i]);ArrayList<String> temp = new ArrayList<String>();for (int j = 0; j < 5; j++) {temp.add("数据" + j);}map.put(CHILD, temp);data.add(map);}etGroup=(EditText) findViewById(R.id.et_group_id);etChild=(EditText) findViewById(R.id.et_child_id);Button btnGroup;btnGroup=(Button) findViewById(R.id.btn_group_id);btnGroup.setOnClickListener(this);btnGroup=(Button) findViewById(R.id.btn_child_id);btnGroup.setOnClickListener(this);// 获取ExpandableListView对象ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView);//设置箭头符号不显示elv.setGroupIndicator(null);// 加载adapter,注意参数的对应 mExpandableListAdapter = new MyExpand(this, null,0, 0, null, null, null, 0, 0, null, null);elv.setAdapter(mExpandableListAdapter);// // 演示//// // 展开0组// elv.expandGroup(0);// // 收起1组// elv.collapseGroup(1);// // 展开2组// elv.expandGroup(2);// 添加点击事件,点击打开或者关闭分组elv.setOnGroupClickListener(new OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {// TODO Auto-generated method stubreturn false;}});}@Overridepublic void onClick(View v) {switch(v.getId()){case R.id.btn_group_id:createGroup(etGroup.getText().toString());//注意,数据更新后要跟新界面,不然显示不变mExpandableListAdapter.notifyDataSetChanged();break;case R.id.btn_child_id:if(!etChild.getText().toString().equals("")){creatChild(Integer.parseInt(etChild.getText().toString()));mExpandableListAdapter.notifyDataSetChanged();}break;}}public void createGroup(String str){HashMap<String, Object> map = new HashMap<String, Object>();map.put(GROUP, str);ArrayList<String> temp = new ArrayList<String>();for (int j = 0; j < 5; j++) {temp.add("数据" + j);}map.put(CHILD, temp);data.add(map);}public void creatChild(int tempInt){ArrayList<String> listTemp= (ArrayList<String>) data.get(1).get(CHILD);listTemp.add(""+tempInt);}// /*// * 重写6个方法// * getGroup:返回对应位置的组的参数// * getGroupCount:返回分了多少组// * getGroupView:显示组// * getChild:返回每组的成员// * getChildCount:返回每组成员的个数// * getChildView : 显示每组成员// */public class MyExpand extends SimpleExpandableListAdapter {private LayoutInflater inflater = null;// 构造方法中实例化LayoutInflater对象public MyExpand(Context context,List<? extends Map<String, ?>> groupData,int expandedGroupLayout, int collapsedGroupLayout,String[] groupFrom, int[] groupTo,List<? extends List<? extends Map<String, ?>>> childData,int childLayout, int lastChildLayout, String[] childFrom,int[] childTo) {super(context, groupData, expandedGroupLayout,collapsedGroupLayout, groupFrom, groupTo, childData,childLayout, lastChildLayout, childFrom, childTo);// TODO Auto-generated constructor stub// 两个方法实例化LayoutInflater对象inflater = LayoutInflater.from(context);// inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@Overridepublic Object getChild(int groupPosition, int childPosition) {ArrayList<String> items = (ArrayList<String>) data.get(groupPosition).get(CHILD);return items.get(childPosition);}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {View view = inflater.inflate(android.R.layout.simple_list_item_1,null);TextView text = (TextView) view.findViewById(android.R.id.text1);text.setText(getChild(groupPosition, childPosition) + "");return view;}@Overridepublic int getChildrenCount(int groupPosition) {ArrayList<String> items = (ArrayList<String>) data.get(groupPosition).get(CHILD);return items.size();}@Overridepublic Object getGroup(int groupPosition) {return data.get(groupPosition).get(GROUP);}@Overridepublic int getGroupCount() {return data.size();}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {View view = inflater.inflate(android.R.layout.simple_list_item_1,null);TextView text = (TextView) view.findViewById(android.R.id.text1);text.setText(getGroup(groupPosition) + "");view.setBackgroundColor(Color.RED);return view;}}}
xml布局文件

<LinearLayout 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:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <EditText            android:id="@+id/et_group_id"            android:layout_width="100dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:singleLine="true" />        <Button            android:id="@+id/btn_group_id"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="添加组"            android:textSize="15sp" />        <EditText            android:id="@+id/et_child_id"            android:layout_width="100dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:singleLine="true" />        <Button            android:id="@+id/btn_child_id"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="添加成员"            android:textSize="15sp" />    </LinearLayout>    <ExpandableListView        android:id="@+id/expandableListView"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>



0 0
原创粉丝点击