zoukankan      html  css  js  c++  java
  • ExpandableListView的用法

    ExpandableListView组件是android中一个比较常用的组件,当点击一个父item的时候可以将它的子item显示出来,像手机QQ中的好友列表就是实现的类型效果。使用ExpandableListView组件的关键就是设置它的adapter,这个adapter必须继承BaseExpandbaleListAdapter类,所以实现运用ExpandableListView的核心就是学会继承这个BaseExpanableListAdapter类。

     下面是一个小demo:

    activvity_main.xml:

     1 <RelativeLayout 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="match_parent"
     5     tools:context=".MainActivity" >
     6 
     7     <ExpandableListView
     8         android:id="@+id/main_expandablelistview"
     9         android:layout_width="match_parent"
    10         android:layout_height="wrap_content" />
    11 
    12 </RelativeLayout>

    layout_parent.xml:(父item运用的样式)

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <TextView
     8         android:id="@+id/parent_textview"
     9         android:layout_width="match_parent"
    10         android:layout_height="50dp"
    11         android:layout_gravity="center"
    12         android:gravity="center" />
    13 
    14 </LinearLayout>

    layout_children.xml:(子item运用的样式)

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <TextView
     8         android:id="@+id/second_textview"
     9         android:layout_width="match_parent"
    10         android:layout_height="40dp"
    11         android:layout_gravity="center"
    12         android:gravity="center" />
    13 
    14 </LinearLayout>

    MainActivity.java:

      1 package com.example.android_expandablelistview;
      2 
      3 import java.util.ArrayList;
      4 import java.util.HashMap;
      5 import java.util.List;
      6 import java.util.Map;
      7 
      8 import android.app.Activity;
      9 import android.content.Context;
     10 import android.os.Bundle;
     11 import android.view.LayoutInflater;
     12 import android.view.View;
     13 import android.view.ViewGroup;
     14 import android.widget.BaseExpandableListAdapter;
     15 import android.widget.ExpandableListView;
     16 import android.widget.TextView;
     17 
     18 public class MainActivity extends Activity {
     19 
     20     ExpandableListView mainlistview = null;
     21     List<String> parent = null;
     22     Map<String, List<String>> map = null;
     23 
     24     @Override
     25     protected void onCreate(Bundle savedInstanceState) {
     26         super.onCreate(savedInstanceState);
     27         setContentView(R.layout.activity_main);
     28         mainlistview = (ExpandableListView) this
     29                 .findViewById(R.id.main_expandablelistview);
     30 
     31         initData();
     32         mainlistview.setAdapter(new MyAdapter());
     33     }
     34 
     35     // 初始化数据
     36     public void initData() {
     37         parent = new ArrayList<String>();
     38         parent.add("parent1");
     39         parent.add("parent2");
     40         parent.add("parent3");
     41 
     42         map = new HashMap<String, List<String>>();
     43 
     44         List<String> list1 = new ArrayList<String>();
     45         list1.add("child1-1");
     46         list1.add("child1-2");
     47         list1.add("child1-3");
     48         map.put("parent1", list1);
     49 
     50         List<String> list2 = new ArrayList<String>();
     51         list2.add("child2-1");
     52         list2.add("child2-2");
     53         list2.add("child2-3");
     54         map.put("parent2", list2);
     55 
     56         List<String> list3 = new ArrayList<String>();
     57         list3.add("child3-1");
     58         list3.add("child3-2");
     59         list3.add("child3-3");
     60         map.put("parent3", list3);
     61 
     62     }
     63 
     64     class MyAdapter extends BaseExpandableListAdapter {
     65       
     66         //得到子item需要关联的数据
     67         @Override
     68         public Object getChild(int groupPosition, int childPosition) {
     69             String key = parent.get(groupPosition);
     70             return (map.get(key).get(childPosition));
     71         }
     72 
     73         //得到子item的ID
     74         @Override
     75         public long getChildId(int groupPosition, int childPosition) {
     76             return childPosition;
     77         }
     78 
     79         //设置子item的组件
     80         @Override
     81         public View getChildView(int groupPosition, int childPosition,
     82                 boolean isLastChild, View convertView, ViewGroup parent) {
     83             String key = MainActivity.this.parent.get(groupPosition);
     84             String info = map.get(key).get(childPosition);
     85             if (convertView == null) {
     86                 LayoutInflater inflater = (LayoutInflater) MainActivity.this
     87                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     88                 convertView = inflater.inflate(R.layout.layout_children, null);
     89             }
     90             TextView tv = (TextView) convertView
     91                     .findViewById(R.id.second_textview);
     92             tv.setText(info);
     93             return tv;
     94         }
     95 
     96         //获取当前父item下的子item的个数
     97         @Override
     98         public int getChildrenCount(int groupPosition) {
     99             String key = parent.get(groupPosition);
    100             int size=map.get(key).size();
    101             return size;
    102         }
    103       //获取当前父item的数据
    104         @Override
    105         public Object getGroup(int groupPosition) {
    106             return parent.get(groupPosition);
    107         }
    108 
    109         @Override
    110         public int getGroupCount() {
    111             return parent.size();
    112         }
    113 
    114         @Override
    115         public long getGroupId(int groupPosition) {
    116             return groupPosition;
    117         }
    118        //设置父item组件
    119         @Override
    120         public View getGroupView(int groupPosition, boolean isExpanded,
    121                 View convertView, ViewGroup parent) {
    122             if (convertView == null) {
    123                 LayoutInflater inflater = (LayoutInflater) MainActivity.this
    124                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    125                 convertView = inflater.inflate(R.layout.layout_parent, null);
    126             }
    127             TextView tv = (TextView) convertView
    128                     .findViewById(R.id.parent_textview);
    129             tv.setText(MainActivity.this.parent.get(groupPosition));
    130             return tv;
    131         }
    132 
    133         @Override
    134         public boolean hasStableIds() {
    135             return true;
    136         }
    137 
    138         @Override
    139         public boolean isChildSelectable(int groupPosition, int childPosition) {
    140             return true;
    141         }
    142 
    143     }
    144 }

    最后的实现结果:

  • 相关阅读:
    有关数据库锁表
    order by 排序的数字异常
    索引建议
    有关文件在浏览器中打开window.open
    vscode 常用快捷键
    jQuery中preventDefault()、stopPropagation()、return false 之间的区别
    理解Linux系统负荷(WDCP系统后台参数之一)
    JavaScript toString() 方法
    1-4:CSS3课程入门之文本新增属性
    1-3:CSS3课程入门之伪类和伪元素
  • 原文地址:https://www.cnblogs.com/huolongluo/p/5582030.html
Copyright © 2011-2022 走看看