码迷,mamicode.com
首页 > 移动开发 > 详细

Android_Expandablelistview

时间:2016-08-06 11:08:29      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

layout.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ExpandableListView 
        android:id="@+id/expandable_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ExpandableListView>

</RelativeLayout>

expandable_childe_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/child_text"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>
    
</LinearLayout>

expandable_group_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/group_text"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>
    
</LinearLayout>

main.java

package com.example.day09_expandablelistview;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;

public class MainActivity extends Activity {
    private int lastGroupPosition = -1;
    private ExpandableListView expandable_lv;
    private String groupData[] = {"同事","老师","朋友"};
    private String childData[][] = {{"小小","小明","饭饭","流浪"},{"李老师","张老师","吴老师","肖老师","柳老师"},{"雯雯","哔哔","嘻嘻"}};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandable_lv = (ExpandableListView) findViewById(R.id.expandable_lv);
        //给ExpandableListAdapter设置适配器---自定义适配器需要继承BaseExpandableListAdapter()实现其中的方法
        MyExpandableListAdapter myExpandableListAdapter = new MyExpandableListAdapter();
        //设置适配器
        expandable_lv.setAdapter(myExpandableListAdapter);
        //去掉group默认的箭头
        expandable_lv.setGroupIndicator(null);
        //设置组可拉伸的监听器,拉伸时会调用其中的onGroupExpand()方法
        expandable_lv.setOnGroupExpandListener(new OnGroupExpandListener() {
            
            @Override
            public void onGroupExpand(int groupPosition) {
                /**
                 * 实现打开只能打开一个组的功能,打开一个组,已将打开的组会自动收缩
                 */
                if(lastGroupPosition != groupPosition){
                    expandable_lv.collapseGroup(lastGroupPosition);
                }
                lastGroupPosition  = groupPosition;
            }
        });
        //设置组收缩的监听器,收缩时会调用其中的onGroupCollapse()方法
        expandable_lv.setOnGroupCollapseListener(new OnGroupCollapseListener() {
            
            @Override
            public void onGroupCollapse(int groupPosition) {
                
            }
        });
    }
    /**
     * 自定义实现类继承BaseExpandandableListAdapter
     * @author my
     *
     */
    class MyExpandableListAdapter extends BaseExpandableListAdapter{
        /**
         * 得到组的数量
         */
        @Override
        public int getGroupCount() {
            return groupData.length;
        }
        /**
         * 得到每个组的元素的数量
         */
        @Override
        public int getChildrenCount(int groupPosition) {
            return childData[groupPosition].length;
        }
        /**
         * 获得组的对象
         */
        @Override
        public Object getGroup(int groupPosition) {
            return groupData[groupPosition];
        }
        /**
         * 获得子对象
         */
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childData[groupPosition][childPosition];
        }
        /**
         * 得到组id
         */
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
        /**
         * 得到子id
         */
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
        /**
         * 表示数据是否稳定,对监听事件有影响
         */
        @Override
        public boolean hasStableIds() {
            return true;
        }
        /**
         * 确定一个组的展示视图--groupPosition表示的当前需要展示的组的索引
         */
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
            LayoutInflater systemService =  (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = systemService.inflate(R.layout.enpandable_group_item, null);
            TextView group_text = (TextView) view.findViewById(R.id.group_text);
            group_text.setText(groupData[groupPosition]);
            return view;
        }
        /**
         * 确定一个组的一个子的展示视图--groupPostion表示当前组的索引,childPosition表示的是需要展示的子的索引
         */
        @Override
        public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
            LayoutInflater systemService = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = systemService.inflate(R.layout.enpandable_child_item, null);
            TextView child_text = (TextView) view.findViewById(R.id.child_text);
            child_text.setText(childData[groupPosition][childPosition]);
            return view;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
        
    }

}

 

Android_Expandablelistview

标签:

原文地址:http://www.cnblogs.com/fangg/p/5743488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!