Android ExpandableListView 進行二級擴展 (BaseExpandableListAdapter)

首先,我的這個這個加載界面是在Fragment裏面添加的,可能和activity有點區別,但不是很大。

主要佈局文件stream.xml,添加ExpandableListView控件

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

第一個自定義菜單佈局文件groups.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageView
        android:layout_width="50dp"
        android:layout_height="80dp"
        android:id="@+id/ImageGroup"
        />
    <TextView
        android:id="@+id/textGroup"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:paddingLeft="40px"
        android:paddingTop="6px"
        android:paddingBottom="6px"
        android:textSize="25sp"
        android:text="No data"
        android:layout_marginLeft="70dp"/>
</LinearLayout>

第二級自定義菜單佈局childs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageView
        android:id="@+id/imageChild"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_gravity="left"/>
    <TextView
        android:id="@+id/textChild"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:paddingLeft="60px"
        android:paddingTop="10px"
        android:paddingBottom="10px"
        android:textSize="20sp"
        android:text="No Data"
        android:layout_marginLeft="0dp"/>

</LinearLayout>

Fragment裏面的文件和響應

package com.bank;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainTab02 extends Fragment {

        List<Map<String, String>> gruops = new ArrayList<Map<String, String>>();

        List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
        ImageView loggroup, logchild;
        TextView textgroup, textchild;
        private ExpandableListView ep;

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View messageLayout = inflater.inflate(R.layout.stream, container, false);


                final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {


                        //設置組視圖的圖片
                        int[] logos = new int[]{R.drawable.cloth, R.drawable.zhu, R.drawable.getmoney};
                        //設置組視圖的顯示文字
                        private String[] category = new String[]{"衣", "食", "住"};
                        //子視圖顯示文字
                        private String[][] subcategory = new String[][]{
                                {"一月", "一月", "一月", "一月", "一月", "一月"},
                                {"一月", "一月", "一月", "一月", "一月", "一月"},
                                {"一月", "一月", "一月", "一月", "一月", "一月"}

                        };

                        //子視圖圖片
                        public int[][] sublogos = new int[][]{
                                {R.drawable.cloth, R.drawable.cloth,
                                        R.drawable.cloth, R.drawable.cloth,
                                        R.drawable.cloth, R.drawable.cloth},
                                {R.drawable.cloth, R.drawable.cloth,
                                        R.drawable.cloth, R.drawable.cloth,
                                        R.drawable.cloth, R.drawable.cloth},
                                {R.drawable.cloth, R.drawable.cloth,
                                        R.drawable.cloth, R.drawable.cloth,
                                        R.drawable.cloth, R.drawable.cloth}};

                        @Override
                        public int getGroupCount() {
                                return category.length;
                        }

                        @Override
                        public int getChildrenCount(int i) {
                                return subcategory[i].length;
                        }

                        @Override
                        public Object getGroup(int i) {
                                return category[i];
                        }

                        @Override
                        public Object getChild(int i, int i1) {
                                return subcategory[i][i1];
                        }

                        @Override
                        public long getGroupId(int i) {
                                return i;
                        }

                        @Override
                        public long getChildId(int i, int i1) {
                                return i1;
                        }

                        @Override
                        public boolean hasStableIds() {
                                return true;
                        }

                        @Override
                        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
                                if (view == null) {
                                        view = getActivity().getLayoutInflater().inflate(R.layout.groups, viewGroup, false);
                                }

                                //創建一級菜單視圖
//                    LinearLayout glayout = (LinearLayout) LayoutInflater.from(getActivity().getBaseContext()).inflate(R.layout.groups, null);
                                //獲得ImageView視圖
                                loggroup = (ImageView) view.findViewById(R.id.ImageGroup);
                                loggroup.setImageResource(logos[i]);
                                // 獲得TextView視圖
                                textgroup = (TextView) view.findViewById(R.id.textGroup);
                                textgroup.setText(category[i]);
                                return view;
                        }

                        @Override
                        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
                                //需要獲得佈局文件的對象
                                if (view == null) {
                                        view = getActivity().getLayoutInflater().inflate(R.layout.childs, viewGroup, false);
                                }

                                //創建一級菜單視圖
                                //   LinearLayout glayout = (LinearLayout) LayoutInflater.from(getActivity().getBaseContext()).inflate(R.layout.childs, null);
                                //獲得ImageView視圖
                                logchild = (ImageView) view.findViewById(R.id.imageChild);
                                logchild.setImageResource(sublogos[i][i1]);
                                // 獲得TextView視圖
                                textchild = (TextView) view.findViewById(R.id.textChild);
                                textchild.setText(subcategory[i][i1]);


                                return view;
                        }

                        @Override
                        public boolean isChildSelectable(int i, int i1) {
                                return true;
                        }
                };

                ep = (ExpandableListView) messageLayout.findViewById(R.id.expandable);

                ep.setAdapter(adapter);

                //爲ExpandableListView的子列表單擊事件設置監聽器
                ep.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                        @Override
                        public boolean onChildClick(ExpandableListView parent, View v,
                                                    int groupPosition, int childPosition, long id) {
                                // TODO Auto-generated method stub
                                Toast.makeText(getActivity(), "你單擊了:"
                                        + adapter.getChild(groupPosition, childPosition), Toast.LENGTH_LONG).show();
                                return true;
                        }
                });

                //    setListData();
                return messageLayout;
        }

這篇文章講的比較清晰

http://blog.csdn.net/eyu8874521/article/details/7873811

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章