• Android简单的BaseExpandableList使用


    1.Activity

     1 package com.example.administrator.mystudent.ExpandableListView;
     2 
     3 import android.app.ExpandableListActivity;
     4 import android.os.Bundle;
     5 
     6 
     7 public class ExpandableListViewActivity extends ExpandableListActivity {
     8 
     9     @Override
    10     protected void onCreate(Bundle savedInstanceState) {
    11         super.onCreate(savedInstanceState);
    12         setTitle("二级列表的使用");
    13         //设置适配器
    14         setListAdapter(new MyExpandableListAdapter(this));
    15     }
    16 }

    2.adapter适配器

      1 package com.example.administrator.mystudent.ExpandableListView;
      2 
      3 import android.content.Context;
      4 import android.view.View;
      5 import android.view.ViewGroup;
      6 import android.widget.BaseExpandableListAdapter;
      7 import android.widget.TextView;
      8 
      9 import java.util.ArrayList;
     10 import java.util.List;
     11 
     12 /**
     13  * Created by hyang on 2017/3/1
     14  * email 390315032@qq.com.
     15  * 可收缩列表适配器
     16  */
     17 
     18 public class MyExpandableListAdapter extends BaseExpandableListAdapter {
     19 
     20     private Context context;
     21     private String [] group={"衣服","食品","水果"};
     22     private String [] cloths={"衬衫","西裤","内衣"};
     23     private String [] food={"面包","蛋糕","可乐","橙汁"};
     24     private String [] fruits={"苹果","香蕉","草莓"};
     25 
     26     private List<String> groupList = null;
     27     private List<List<String>> itemList = null;
     28 
     29     protected MyExpandableListAdapter(Context context){
     30         this.context=context;
     31         groupList=new ArrayList<String>();
     32         itemList=new ArrayList<List<String>>();
     33 
     34         setListData();
     35     }
     36 
     37     /**
     38      * 向list中添加数据
     39      */
     40     private void setListData() {
     41         for(String s:group){
     42             groupList.add(s);
     43         }
     44         List clothsList=new ArrayList();
     45         List foodList=new ArrayList();
     46         List fruitsList=new ArrayList();
     47 
     48         for(String s:cloths){
     49             clothsList.add(s);
     50         }
     51         for(String s:food){
     52             foodList.add(s);
     53         }
     54         for(String s:fruits){
     55             fruitsList.add(s);
     56         }
     57 
     58         itemList.add(clothsList);
     59         itemList.add(foodList);
     60         itemList.add(fruitsList);
     61     }
     62 
     63     @Override
     64     public int getGroupCount() {
     65         //返回组数量
     66         return itemList.size();
     67     }
     68 
     69     @Override
     70     public int getChildrenCount(int groupPosition) {
     71         //返回当前组的节点数量
     72         return itemList.get(groupPosition).size();
     73     }
     74 
     75     @Override
     76     public Object getGroup(int groupPosition) {
     77         //返回分组对象,用于一些数据传递,在事件处理时可直接取得和分组相关的数据
     78         return groupList.get(groupPosition);
     79     }
     80 
     81     @Override
     82     public Object getChild(int groupPosition, int childPosition) {
     83         //设置子节点数据
     84         return itemList.get(groupPosition).get(childPosition);
     85     }
     86 
     87     @Override
     88     public long getGroupId(int groupPosition) {
     89         return groupPosition;
     90     }
     91 
     92     @Override
     93     public long getChildId(int groupPosition, int childPosition) {
     94         return childPosition;
     95     }
     96 
     97     @Override
     98     public boolean hasStableIds() {
     99         return false;
    100     }
    101 
    102     @Override
    103     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    104         //设置组显示View,这里可是设置布局文件引用
    105         TextView view=null;
    106         if(convertView==null){
    107             view=new TextView(context);
    108         }else{
    109             view= (TextView) convertView;
    110         }
    111         //设置textView的显示样式
    112         view.setTextSize(30);
    113         view.setPadding(50,5,0,5);
    114         //向textView设置数据
    115         String s=groupList.get(groupPosition);
    116         view.setText(s);
    117         return view;
    118     }
    119 
    120     @Override
    121     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    122         //设置节点显示View,这里可是设置布局文件引用
    123         TextView view=null;
    124         if(convertView==null){
    125             view=new TextView(context);
    126         }else{
    127             view= (TextView) convertView;
    128         }
    129         view.setTextSize(20);
    130         view.setPadding(30,5,0,5);
    131         //向textView设置数据
    132         String s=itemList.get(groupPosition).get(childPosition);
    133         view.setText(s);
    134         return view;
    135     }
    136 
    137     @Override
    138     public boolean isChildSelectable(int groupPosition, int childPosition) {
    139         return true;
    140     }
    141 }
  • 相关阅读:
    《剑指offer》Q01-12 (牛客10.11)
    北京,我来了
    pods的好处
    iOS那些你从未使用过的属性方法
    重构一个运行超过10年的老项目
    C#泛型
    认真思考创建对象的几种方式
    2014年终总结
    前端优化:AMDclean去除requirejs
    webstorm常用快捷键及插件
  • 原文地址:https://www.cnblogs.com/yoyohong/p/6483291.html
Copyright © 2020-2023  润新知