android 中可展开控件ExpandableListView的使用


布局文件三个:

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;
	}
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值