Android ExpandableListView 进行二级扩展 (BaseExpandableListAdapter)
2016-04-02 19:52
561 查看
首先,我的这个这个加载界面是在Fragment里面添加的,可能和activity有点区别,但不是很大。
主要布局文件stream.xml,添加ExpandableListView控件
第一个自定义菜单布局文件groups.xml
第二级自定义菜单布局childs.xml
Fragment里面的文件和响应
这篇文章讲的比较清晰
http://blog.csdn.net/eyu8874521/article/details/7873811
主要布局文件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
相关文章推荐
- android_handler(三)
- android的 deeplink与服务路由的思考
- Android开发:GPU过度绘制的优化
- android重新启动应用程序和重新启动系统
- android利用zbar二维码扫描-(解决中文乱码及扫描区域定义)
- Android客户端性能优化(魅族资深工程师毫无保留奉献)
- Android Activity经常被自动回收
- Android获取内存(RAM)大小信息
- Android Scroller使用详解
- Android 自定义Listview 如何绑定Sqlite数据库数据
- Android开发:解决android:gravity不能居中问题
- Android开发中的一个小功能 清空搜索框的文字
- Android开发:setAlpha()方法
- Mac下载安装Android Studio教程
- Android 电子邮件发送成功与失败的提示
- parcelable的使用与seraliezd的使用比较
- Android之获取数据库路径
- Android和“时间”有关的一些常用函数
- mac配置git环境
- Android开发实用技巧之三:定制自己的日志工具LogUtil.java