zoukankan      html  css  js  c++  java
  • Android组件学习之ExpandableListView

    一个简单的小例子:

    可以展开的ListView,和Listview差不多,只是设置的Adapter不同。常用的Adapter有BaseExpandableListAdapter、SimpleExpandableListAdapter、SimpleCursorTreeAdapter

    布局如下:(布局中我设置了android:groupIndicator,不知道为什么不起作用。另外,android:dividerHeight这个属性是组对象和子节点共用的。如果要定义比较复杂的组视图及子节点视图,还是单独设置一个layout文件,再在Adapter中设置相应的返回View视图)

     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     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context="com.example.testandroid.ui.MainActivity" >
    10 
    11     <ExpandableListView
    12         android:id="@+id/main_elv1"
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         android:layout_alignParentLeft="true"
    16         android:layout_alignParentTop="true"
    17         android:childDivider="@color/white"
    18         android:divider="@color/gray"
    19         android:dividerHeight="1px"
    20         android:groupIndicator="@drawable/arrow_triangle_down"
    21         android:indicatorLeft="4dp"
    22         android:indicatorRight="4dp" >
    23     </ExpandableListView>
    24 
    25 </RelativeLayout>

    主Activity如下:

     1 package com.example.testandroid.ui;
     2 
     3 import android.os.Bundle;
     4 import android.support.v7.app.ActionBarActivity;
     5 import android.view.Menu;
     6 import android.view.MenuItem;
     7 import android.widget.ExpandableListView;
     8 
     9 import com.example.testandroid.R;
    10 import com.example.testandroid.adapter.MainActivityExLVAdapter;
    11 
    12 public class MainActivity extends ActionBarActivity {
    13 
    14     private ExpandableListView mExpandableListView = null;
    15     private MainActivityExLVAdapter mMainActivityExLVAdapter = null;
    16 
    17     @Override
    18     protected void onCreate(Bundle savedInstanceState) {
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.activity_main);
    21         init();
    22     }
    23 
    24     private void init() {
    25         mExpandableListView = (ExpandableListView) findViewById(R.id.main_elv1);
    26         mMainActivityExLVAdapter = new MainActivityExLVAdapter(this);
    27         mExpandableListView.setAdapter(mMainActivityExLVAdapter);
    28     }
    29 
    30     @Override
    31     public boolean onCreateOptionsMenu(Menu menu) {
    32         getMenuInflater().inflate(R.menu.main, menu);
    33         return true;
    34     }
    35 
    36     @Override
    37     public boolean onOptionsItemSelected(MenuItem item) {
    38         int id = item.getItemId();
    39         if (id == R.id.action_settings) {
    40             return true;
    41         }
    42         return super.onOptionsItemSelected(item);
    43     }
    44 }

    Adapter我写到另外一个类文件中:

      1 package com.example.testandroid.adapter;
      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 public class MainActivityExLVAdapter extends BaseExpandableListAdapter {
     10 
     11     private Context mContext = null;
     12 
     13     private String[] mGroup = { "FirstOne", "SecondTwo", "ThirdThress" };
     14     private String[][] mChildren = {
     15             { "2014-year-01-month-01", "2014-year-01-month-02",
     16                     "2014-year-01-month-03", "2014-year-01-month-04",
     17                     "2014-year-01-month-05" },
     18             { "2014-year-02-month-01", "2014-year-02-month-02",
     19                     "2014-year-02-month-03", "2014-year-02-month-04" },
     20             { "2014-year-03-month-01", "2014-year-03-month-02", } };
     21 
     22     public MainActivityExLVAdapter(Context mContext) {
     23         this.mContext = mContext;
     24     }
     25 
     26     /**
     27      * 获取组的数量
     28      */
     29     @Override
     30     public int getGroupCount() {
     31         return mGroup.length;
     32     }
     33 
     34     /**
     35      * 获取组下的节点数量
     36      */
     37     @Override
     38     public int getChildrenCount(int groupPosition) {
     39         return mChildren[groupPosition].length;
     40     }
     41 
     42     /**
     43      * 获取组对象(这个方法可以返回任意对象,或者null都行,给自己调用的)
     44      */
     45     @Override
     46     public Object getGroup(int groupPosition) {
     47         return mGroup[groupPosition];
     48     }
     49 
     50     /**
     51      * 获取子节点对象(这个方法可以返回任意对象,或者null都行,给自己调用的)
     52      */
     53     @Override
     54     public Object getChild(int groupPosition, int childPosition) {
     55         return mChildren[groupPosition][childPosition];
     56     }
     57 
     58     /**
     59      * 给自己调用的
     60      */
     61     @Override
     62     public long getGroupId(int groupPosition) {
     63         return groupPosition;
     64     }
     65 
     66     /**
     67      * 给自己调用的
     68      */
     69     @Override
     70     public long getChildId(int groupPosition, int childPosition) {
     71         return childPosition;
     72     }
     73 
     74     /**
     75      * TODO 不知道有什么用
     76      */
     77     @Override
     78     public boolean hasStableIds() {
     79         return true;
     80     }
     81 
     82     /**
     83      * 返回组对象的视图
     84      */
     85     @Override
     86     public View getGroupView(int groupPosition, boolean isExpanded,
     87             View convertView, ViewGroup parent) {
     88         TextView mTextView = new TextView(mContext);
     89         mTextView.setText(getGroup(groupPosition).toString());
     90         return mTextView;
     91     }
     92 
     93     /**
     94      * 返回子节点的视图
     95      */
     96     @Override
     97     public View getChildView(int groupPosition, int childPosition,
     98             boolean isLastChild, View convertView, ViewGroup parent) {
     99         TextView mTextView = new TextView(mContext);
    100         mTextView.setText(getChild(groupPosition, childPosition).toString());
    101         return mTextView;
    102     }
    103 
    104     @Override
    105     public boolean isChildSelectable(int groupPosition, int childPosition) {
    106         return false;
    107     }
    108 
    109 }
  • 相关阅读:
    「SAP技术」SAP MM MB5M报表不显示特殊库存数据
    python-day3-条件判断与循环
    python-day2-运算符
    Mysql数据库意外崩溃导致表数据文件损坏无法启动的问题解决
    图书管理系统(Servlet+Jsp+Java+Mysql,附下载演示地址)
    求解最长递增子序列(LIS) | 动态规划(DP)+ 二分法
    HTML+CSS+JavaScript实现植物大战僵尸(附演示地址)
    面试官:如何在Integer类型的ArrayList中同时添加String、Character、Boolean等类型的数据? | Java反射高级应用
    面试官:手撕十大排序算法,你会几种?
    用x种方式求第n项斐波那契数,99%的人只会第一种
  • 原文地址:https://www.cnblogs.com/creasylai19/p/3903255.html
Copyright © 2011-2022 走看看