android 中可展开控件ExpandableListView的使用
2015-06-02 15:32
656 查看
布局文件三个:
1.expandalistview_layout.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/main_bg" android:orientation="vertical" > <ExpandableListView android:id="@+id/expandablelist" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_below="@id/common_problem_bar" android:layout_marginBottom="50dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="10dip" android:background="@color/white" android:drawSelectorOnTop="false" > </ExpandableListView> </RelativeLayout>
2.一级视图布局
common_problem_group_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation = "horizontal" android:layout_width="match_parent" android:layout_height="40dip" android:background="@color/white"> <!-- 常见问题的ExpandableListView group 布局 --> <TextView android:id = "@+id/textView01" android:layout_width ="match_parent" android:layout_height = "match_parent" android:layout_marginTop="10dip" android:layout_marginBottom="10dip" android:layout_marginLeft="40dip" android:layout_marginRight="10dip" android:textSize = "18dip"/> </RelativeLayout>
3.二级视图 common_problem_child_item
<?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="horizontal" > <!-- 常见问题的ExpandableListView child 布局 --> <TextView android:id="@+id/childTo" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:paddingBottom="5px" android:paddingLeft="30px" android:paddingTop="10px" android:textColor="@color/gray" android:textSize="15dip" /> </LinearLayout><span style="color:#0326cc;"> </span>
</pre><pre name="code" class="html">准备数据
// 一级数据
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Monaco;"><pre name="code" class="java">private static final int[] problemSubIds = new int[] { R.string.problem_sub0, R.string.problem_sub1, R.string.problem_sub2, R.string.problem_sub3, R.string.problem_sub4 }; <pre name="code" class="html" style="font-size: 18px;">// 二级数据
private static final int[][] problemContentIds = new int[][] { { R.string.problem_content0 }, { R.string.problem_content1 }, { R.string.problem_content2 }, { R.string.problem_content3 }, { R.string.problem_content4 } };
绑定适配器
mExpandableListView = (ExpandableListView) v.findViewById(R.id.expandablelist);
mExpandableListView.setAdapter(new ExpandableAdapter(getActivity(), problemSubIds, problemContentIds));
ExpandableAdapter适配器
package com.toughegg.teclient.adapter; import android.annotation.SuppressLint; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.toughegg.teclient.R; @SuppressLint("InflateParams") public class ExpandableAdapter extends BaseExpandableListAdapter { private Context context; private int[] problemypes; private int[][] problemdetail; public ExpandableAdapter(Context context, int[] problemypes, int[][] problemdetail) { this.context = context; this.problemypes = problemypes; this.problemdetail = problemdetail; } @Override public Object getChild(int groupPosition, int childPosition) { return problemdetail[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { int text = problemdetail[groupPosition][childPosition]; LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.te_common_problem_child, null); TextView tv = (TextView) linearLayout.findViewById(R.id.childTo); tv.setText(text); return linearLayout; } @Override public int getChildrenCount(int groupPosition) { return problemdetail[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return problemypes[groupPosition]; } @Override public int getGroupCount() { return problemypes.length; } @Override public long getGroupId(int groupPosition) { return groupPosition; } // 获取一级列表View对象 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { int text = problemypes[groupPosition]; LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); RelativeLayout linearLayout = (RelativeLayout) layoutInflater.inflate(R.layout.te_common_problem_group_xml, null); TextView textView = (TextView) linearLayout.findViewById(R.id.textView01); textView.setText(text); return linearLayout; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }
相关文章推荐
- flex 控件的重要属性
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- MFC中动态创建控件以及事件响应实现方法
- WinForm自定义函数FindControl实现按名称查找控件
- WinForm拖拽控件生成副本的解决方法
- ASP.NET的HtmlForm控件学习及Post与Get的区别概述
- WinForm实现移除控件某个事件的方法
- C#分屏控件用法实例
- jQuery操作表单常用控件方法小结
- C#的winform控件命名规范
- Jquery给基本控件的取值、赋值示例
- ASP.net 动态加载控件时一些问题的总结
- Android ExpandableListView长按事件的完美解决办法
- ASP.NET 4中的可扩展输出缓存(可以缓存页面/控件等)
- .NET中TextBox控件设置ReadOnly=true后台取不到值三种解决方法
- asp.net DataGrid控件中弹出详细信息窗口
- asp.net Repeater控件的说明及详细介绍及使用方法