android 中可展开控件ExpandableListView的使用

来源:互联网 发布:人工智能学术会议 编辑:程序博客网 时间:2024/05/16 05:30


布局文件三个:

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"?><RelativeLayoutxmlns: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 布局 --><TextViewandroid: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;}@Overridepublic Object getChild(int groupPosition, int childPosition) {return problemdetail[groupPosition][childPosition];}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic 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;}@Overridepublic int getChildrenCount(int groupPosition) {return problemdetail[groupPosition].length;}@Overridepublic Object getGroup(int groupPosition) {return problemypes[groupPosition];}@Overridepublic int getGroupCount() {return problemypes.length;}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}// 获取一级列表View对象@Overridepublic 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;}@Overridepublic boolean hasStableIds() {return true;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}







0 0