Android(安卓)ExpandableListView分组效果
16lz
2021-01-26
效果如图:
package com.example.expandablelistview;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Random;import org.apache.http.MethodNotSupportedException;import android.app.Activity;import android.content.Context;import android.graphics.Color;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnGroupClickListener;import android.widget.TextView;public class MainActivity extends Activity {private MyExpandableListAdapter mExpandableListAdapter;private List<HashMap<String,Object>> list;List<String> list_child;private final String GROUP = "group";private final String CHILD = "child";private EditText edit;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);list = new ArrayList<HashMap<String,Object>>();//分组的标签String[] groups = {"A","B","C","D","E","F"};Random rand = new Random();for(int i=0;i<groups.length;i++){HashMap<String,Object> map = new HashMap<String, Object>();map.put(GROUP, groups[i]);list_child = new ArrayList<String>();for(int j=0;j<5;j++){list_child.add("联系人"+j);}map.put(CHILD, list_child);list.add(map);}edit = (EditText) findViewById(R.id.edit);ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandablelistview);Button btn = (Button) findViewById(R.id.btn);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {addChild(2,2);}});mExpandableListAdapter = new MyExpandableListAdapter(this);elv.setAdapter(mExpandableListAdapter); // 演示 // 展开0组 elv.expandGroup(0); // 收起1组 elv.collapseGroup(1); // 展开2组 elv.expandGroup(2); elv.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView arg0, View arg1, int arg2, long arg3) { // Android默认是返回false。 // 如果返回true,那么,不管是点击已展开的分组还是未展开的分组,都不会相应展开或者收缩的,也就是说这个ExpandableListView将成为一个‘死’的ListView return false; } });}private void addChild(int pos,int index){HashMap<String, Object> map = list.get(pos);List<String> list = (List<String>) map.get(CHILD);list.add(index, "xlg");}public class MyExpandableListAdapter extends BaseExpandableListAdapter{private Context mcontext;private LayoutInflater inflater; public MyExpandableListAdapter(Context context) {this.mcontext = context;inflater = LayoutInflater.from(context);}@Overridepublic int getGroupCount() {return list.size();}@Overridepublic int getChildrenCount(int groupPosition) {List<String> child = (List<String>) list.get(groupPosition).get(CHILD);return child.size();}@Overridepublic Object getGroup(int groupPosition) {return list.get(groupPosition).get(GROUP);}@Overridepublic Object getChild(int groupPosition, int childPosition) {List<String> child = (List<String>) list.get(groupPosition).get(CHILD);return child.get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn childPosition;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);TextView text = (TextView) convertView.findViewById(android.R.id.text1);text.setText(getGroup(groupPosition)+"");text.setBackgroundColor(Color.RED);return convertView;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,ViewGroup parent) {convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);TextView text = (TextView) convertView.findViewById(android.R.id.text1);text.setText(getChild(groupPosition, childPosition)+"");text.setBackgroundColor(Color.GRAY);return convertView;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn false;}}}
需要的布局文件:
<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" tools:context="com.example.expandablelistview.MainActivity" > <ExpandableListView android:id="@+id/expandablelistview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"> </ExpandableListView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#e0e0e0"> <EditText android:id="@+id/edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送" android:background="@android:color/holo_red_dark"/> </LinearLayout></LinearLayout>
更多相关文章
- ViewPage 翻页滑动
- Android(安卓)《第一行代码》 第二章练习代码 ActivityTest
- Android(安卓)PopupWindow增加半透明蒙层
- Android(安卓)ExpandableListView使用小结(一)
- AndroidStudio调用摄像头
- Activity销毁时返回数据
- Android(安卓)手机端与服务器端通过http交换数据 Json
- Android(安卓)App静默安装的解决方案
- Android:dkplayer非全屏时候显示返回按钮