• QQ分组实现,可收缩---ExpandableListView


    activity:

      1 package com.zzw.qqgroup;
      2 
      3 import java.util.ArrayList;
      4 import java.util.HashMap;
      5 import java.util.List;
      6 import java.util.Map;
      7 import java.util.Random;
      8 
      9 import android.app.Activity;
     10 import android.content.Context;
     11 import android.graphics.Color;
     12 import android.os.Bundle;
     13 import android.view.LayoutInflater;
     14 import android.view.View;
     15 import android.view.View.OnClickListener;
     16 import android.view.ViewGroup;
     17 import android.widget.EditText;
     18 import android.widget.ExpandableListView;
     19 import android.widget.ExpandableListView.OnGroupClickListener;
     20 import android.widget.SimpleExpandableListAdapter;
     21 import android.widget.TextView;
     22 
     23 public class MainActivity extends Activity {
     24 
     25     private final String GROUP = "group";
     26     private final String CHILD = "child";
     27 
     28     private EditText editText;
     29     private MyExpandableListAdapter mExpandableListAdapter;
     30 
     31     private ArrayList<HashMap<String, Object>> data;
     32 
     33     @Override
     34     protected void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         setContentView(R.layout.activity_main);
     37 
     38         data = new ArrayList<HashMap<String, Object>>();
     39 
     40         editText = (EditText) findViewById(R.id.editText);
     41 
     42         rawData();
     43 
     44         ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView);
     45         elv.setGroupIndicator(null);// 使收缩箭头消失
     46 
     47         mExpandableListAdapter = new MyExpandableListAdapter(this, null, 0, 0, null, null, null, 0, null, null);
     48 
     49         elv.setAdapter(mExpandableListAdapter);
     50 
     51         /*
     52          * 下面是演示
     53          */
     54         // elv.expandGroup(0);// 展开0组
     55         // elv.collapseGroup(1);// 收起1组
     56 
     57         elv.setOnGroupClickListener(new OnGroupClickListener() {
     58 
     59             @Override
     60             public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
     61                 /*
     62                  * 安卓默认是返回false 如果返回true,则不管是点击已展开的分组还是未展开的分组都不会相应展开或者收缩的
     63                  */
     64                 return false;
     65             }
     66         });
     67 
     68         findViewById(R.id.addGroup).setOnClickListener(new OnClickListener() {
     69             @Override
     70             public void onClick(View v) {
     71                 addGroup(2);
     72             }
     73         });
     74 
     75         findViewById(R.id.addChild).setOnClickListener(new OnClickListener() {
     76             @Override
     77             public void onClick(View v) {
     78                 addChild(2);
     79             }
     80         });
     81     }
     82 
     83     // 在指定位置添加组
     84     private void addGroup(int pos) {
     85         String str = editText.getText() + "";
     86         if (!str.trim().equals("")) {
     87             HashMap<String, Object> map = new HashMap<String, Object>();
     88             map.put(GROUP, str);
     89             ArrayList<String> childs = new ArrayList<String>();
     90             map.put(CHILD, childs);
     91             data.add(pos, map);
     92             editText.setText("");
     93             mExpandableListAdapter.notifyDataSetChanged();
     94         }
     95     }
     96 
     97     // 在尾部增加组
     98     private void addGroup() {
     99         String str = editText.getText() + "";
    100         if (!str.trim().equals("")) {
    101             HashMap<String, Object> map = new HashMap<String, Object>();
    102             map.put(GROUP, str);
    103             ArrayList<String> childs = new ArrayList<String>();
    104             map.put(CHILD, childs);
    105             data.add(map);
    106             editText.setText("");
    107             mExpandableListAdapter.notifyDataSetChanged();
    108         }
    109     }
    110 
    111     // 指定组中指定位置添加child数据
    112     private void addChild(int groupPos, int childPos) {
    113         String str = editText.getText() + "";
    114         if (!str.trim().equals("") && groupPos < data.size()) {
    115             HashMap<String, Object> map = data.get(groupPos);
    116             ArrayList<String> childs = (ArrayList<String>) map.get(CHILD);
    117             if (childPos < childs.size()) {
    118                 childs.add(childPos, str);
    119                 editText.setText("");
    120                 mExpandableListAdapter.notifyDataSetChanged();
    121             }
    122         }
    123     }
    124 
    125     // 指定组中尾部位置增加child元素
    126     private void addChild(int groupPos) {
    127         String str = editText.getText() + "";
    128         if (!str.trim().equals("") && groupPos < data.size()) {
    129             HashMap<String, Object> map = data.get(groupPos);
    130             ArrayList<String> childs = (ArrayList<String>) map.get(CHILD);
    131             childs.add(str);
    132             editText.setText("");
    133             mExpandableListAdapter.notifyDataSetChanged();
    134 
    135         }
    136     }
    137 
    138     // 初始化增加数据
    139     private void rawData() {
    140         // 设置分组
    141         String[] g = { "我的好友", "朋友", "同学", "同事" };
    142         String[] c = { "张三", "李四", "王二", "麻子", "钱五" };
    143 
    144         Random rand = new Random();
    145         for (int i = 0; i < g.length; i++) {
    146             int count = 0;
    147             HashMap<String, Object> map = new HashMap<String, Object>();
    148             map.put(GROUP, g[i]);
    149 
    150             ArrayList<String> child = new ArrayList<String>();
    151             int r = rand.nextInt(10);
    152             for (String ch : c) {
    153                 child.add("-------" + ch + count++);
    154             }
    155             map.put(CHILD, child);
    156 
    157             data.add(map);
    158         }
    159     }
    160 
    161     private class MyExpandableListAdapter extends SimpleExpandableListAdapter {
    162         LayoutInflater inflater;
    163 
    164         public MyExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData,
    165                 int expandedGroupLayout, int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
    166                 List<? extends List<? extends Map<String, ?>>> childData, int childLayout, String[] childFrom,
    167                 int[] childTo) {
    168             super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom, groupTo, childData,
    169                     childLayout, childFrom, childTo);
    170             inflater = LayoutInflater.from(context);
    171         }
    172 
    173         @Override
    174         public Object getChild(int groupPosition, int childPosition) {
    175             ArrayList<String> items = (ArrayList<String>) data.get(groupPosition).get(CHILD);
    176 
    177             return items.get(childPosition);
    178         }
    179 
    180         @Override
    181         public int getChildrenCount(int groupPosition) {
    182 
    183             ArrayList<String> items = (ArrayList<String>) data.get(groupPosition).get(CHILD);
    184             return items.size();
    185         }
    186 
    187         @Override
    188         public Object getGroup(int groupPosition) {
    189 
    190             return data.get(groupPosition).get(GROUP);
    191         }
    192 
    193         @Override
    194         public int getGroupCount() {
    195             return data.size();
    196         }
    197 
    198         @Override
    199         public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    200             if (convertView == null) {
    201                 convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
    202             }
    203             TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
    204             textView.setText(getGroup(groupPosition) + "");
    205             textView.setTextColor(Color.RED);
    206             return convertView;
    207         }
    208 
    209         @Override
    210         public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
    211                 ViewGroup parent) {
    212             if (convertView == null) {
    213                 convertView = inflater.inflate(R.layout.item, null);
    214             }
    215             TextView textView = (TextView) convertView.findViewById(R.id.textView1);
    216             textView.setText(getChild(groupPosition, childPosition) + "");
    217             return convertView;
    218         }
    219     }
    220 }

    xml:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="wrap_content"
     5     android:orientation="vertical"
     6     tools:context="com.zzw.qqgroup.MainActivity" >
     7 
     8 
     9     <ExpandableListView
    10         android:id="@+id/expandableListView"
    11         android:layout_width="match_parent"
    12         android:layout_height="match_parent"
    13         android:layout_weight="1" >
    14     </ExpandableListView>
    15 
    16     <RelativeLayout
    17         android:layout_width="match_parent"
    18         android:layout_height="wrap_content" >
    19 
    20         <Button
    21             android:id="@+id/addGroup"
    22             android:layout_width="wrap_content"
    23             android:layout_height="wrap_content"
    24             android:layout_alignParentLeft="true"
    25             android:text="增加分组" />
    26 
    27         <Button
    28             android:id="@+id/addChild"
    29             android:layout_width="wrap_content"
    30             android:layout_height="wrap_content"
    31             android:layout_alignParentRight="true"
    32             android:text="增加联系人" />
    33 
    34         <EditText
    35             android:id="@+id/editText"
    36             android:layout_width="wrap_content"
    37             android:layout_height="wrap_content"
    38             android:layout_alignBaseline="@+id/addGroup"
    39             android:layout_alignBottom="@+id/addGroup"
    40             android:layout_toLeftOf="@+id/addChild"
    41             android:layout_toRightOf="@+id/addGroup"
    42             android:ems="10"
    43             android:hint="请输入" >
    44 
    45             <requestFocus />
    46         </EditText>
    47     </RelativeLayout>
    48 
    49 </LinearLayout>
    activity_main.xml
  • 相关阅读:
    tomcat 启动超时
    读书笔记-String
    读书笔记-集合
    读书笔记-算法
    多变量梯度下降
    多变量线性回归
    梯度下降算法在线性回归中的运用
    梯度下降
    线性回归——代价函数
    线性回归
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4972094.html
Copyright © 2020-2023  润新知