您的位置:首页 > 移动开发 > Android开发

Android ExpandableListView 进行二级扩展 (BaseExpandableListAdapter)

2016-04-02 19:52 561 查看
首先,我的这个这个加载界面是在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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: