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

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息