Android开发之ExpandableListView的使用(一目了然版)

 

  首先,通过ExpandableListView这个名字,我们就可以看得出这是一个可扩展的ListView,对于ListView我们就很清楚,不用太多的解释了!

  由于本人测试的时候全部都是真机测试的,所以就不上图了,代码尽量详细些。。。

  (如果大家想测试的话,直接可以把下面的两张图片复制粘贴到你的项目中,其它的代码直接用就ok了)

  直接看代码讲解吧:

  

imagetodown.png:

imagetoright.png:

 

MainActivity.java:

package com.example.expandable_test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ExpandableListView listView=null;
	private My_Adapter adapter=null;//自定义的适配器
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		listView=(ExpandableListView)findViewById(R.id.expandablelistview);
		adapter=new My_Adapter(this);
		listView.setAdapter(adapter);
		
		listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Toast.makeText(MainActivity.this,"你点击了:"+adapter.getChild(groupPosition, childPosition).toString(), Toast.LENGTH_LONG).show();
				return false;
			}
		});
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

 

然后是自己重写的适配器My_Adapter:

package com.example.expandable_test;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class My_Adapter extends BaseExpandableListAdapter {

    private Context context;
    private LayoutInflater father_Inflater=null;
    private LayoutInflater son_Inflater=null;
    
    private ArrayList<String> father_array;//父层
    private ArrayList<List<String>> son_array;//子层
    
    public My_Adapter(Context context)
    {
        this.context=context;
        father_Inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        son_Inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Init_data();
    }

  //重写的方法,用于获取子层的内容,这里获取子层的显示字符串 @Override
public Object getChild(int groupPosition, int childPosition) { return son_array.get(groupPosition).get(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) { Son_ViewHolder son_ViewHolder=null; if(convertView==null) { convertView=son_Inflater.inflate(R.layout.son, null); son_ViewHolder=new Son_ViewHolder(); son_ViewHolder.son_TextView=(TextView)convertView.findViewById(R.id.son_textview); convertView.setTag(son_ViewHolder); } else { son_ViewHolder=(Son_ViewHolder)convertView.getTag(); } son_ViewHolder.son_TextView.setText(son_array.get(groupPosition).get(childPosition)); return convertView; }
  //重写的方法,用于获取父层中其中一层的子数目 @Override
public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return son_array.get(groupPosition).size(); }
  //重写的方法,用于获取父层中的一项,返回的是父层的字符串类型 @Override
public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return father_array.get(groupPosition); }
  //重写的方法,用于获取父层的大小 @Override
public int getGroupCount() { // TODO Auto-generated method stub return father_array.size(); }
  //重写的方法,用于获取父层的位置 @Override
public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; }
  //重写的方法,用于获取父层的视图 @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Father_ViewHolder father_ViewHolder=null; if(convertView==null) { convertView=father_Inflater.inflate(R.layout.father, null); father_ViewHolder=new Father_ViewHolder(); father_ViewHolder.father_TextView=(TextView)convertView.findViewById(R.id.father_textview); father_ViewHolder.image_view=(ImageView)convertView.findViewById(R.id.father_imageview); convertView.setTag(father_ViewHolder); } else { father_ViewHolder=(Father_ViewHolder)convertView.getTag(); } father_ViewHolder.father_TextView.setText(father_array.get(groupPosition)); if(isExpanded) { father_ViewHolder.image_view.setImageDrawable(context.getResources().getDrawable(R.drawable.imagetodown)); } else { father_ViewHolder.image_view.setImageDrawable(context.getResources().getDrawable(R.drawable.imagetoright)); } return convertView; }   
  //.... @Override
public boolean hasStableIds() { // TODO Auto-generated method stub return true; }   
  //.... @Override
public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; }
  //初始化数据,主要是父层和子层数据的初始化
public void Init_data() { father_array=new ArrayList<String>(); son_array=new ArrayList<List<String>>(); father_array.add("房产凭证"); father_array.add("二手汽车"); father_array.add("二手物品"); father_array.add("宠物及用品"); father_array.add("求职简介"); father_array.add("征婚交友"); List<String> one1=new ArrayList<String>(); one1.add("郊区"); one1.add("市区"); one1.add("农村"); one1.add("山区"); one1.add("海湾"); one1.add("岛屿"); one1.add("城镇"); son_array.add(one1); List<String> two1=new ArrayList<String>(); two1.add("大众"); two1.add("奔驰"); two1.add("QQ"); two1.add("西门子"); two1.add("东风"); son_array.add(two1); List<String> one2=new ArrayList<String>(); one2.add("手机"); one2.add("电脑"); one2.add("电视"); one2.add("冰箱"); one2.add("沙发"); one2.add("水杯"); son_array.add(one2); List<String> two2=new ArrayList<String>(); two2.add("妞妞"); two2.add("贝贝"); two2.add("朵朵"); two2.add("霍霍"); two2.add("花花"); two2.add("美美"); son_array.add(two2); List<String> one3=new ArrayList<String>(); one3.add("教师"); one3.add("会计"); one3.add("工程师"); one3.add("测试员"); son_array.add(one3); List<String> two3=new ArrayList<String>(); two3.add("兴趣交友"); two3.add("婚庆交友"); son_array.add(two3); } public final class Father_ViewHolder { private TextView father_TextView; private ImageView image_view; } public final class Son_ViewHolder { private TextView son_TextView; } }

看到适配器的内容,我们就应该很熟悉了,只要是方法实现部分前面有“@Override”的,就都是适配器自身含有的方法,我们所做的不过是重写这些方法进行实现而已!

 

接着,三个布局文件:

1.main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ExpandableListView 
        android:id="@+id/expandablelistview"
        android:layout_width="fill_parent"
         android:drawSelectorOnTop="false"
        android:layout_height="fill_parent"
        android:background="#ffffff"
        android:groupIndicator="@null" 
        android:cacheColorHint="#00000000"
        android:listSelector="#00000000" //这个写出来主要是去除单击单项时点击处变黄一下的效果
        ></ExpandableListView>

</RelativeLayout>

2.father.xml

<?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="wrap_content"
    android:id="@+id/father"
    android:orientation="vertical"
    > 
    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        >
        <TextView 
            android:layout_alignParentLeft="true"
            android:textSize="25dp"
            android:id="@+id/father_textview"
       android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:layout_alignParentRight="true" android:layout_centerVertical="true" android:id="@+id/father_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="15dip" android:src="@drawable/imagetoright" /> </RelativeLayout> </LinearLayout>

3.son.xml

<?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="wrap_content"
    android:id="@+id/son"
    android:orientation="vertical" > 
    <TextView 
		    android:id="@+id/son_textview"
		     android:textSize="25dp"
		    android:layout_width="fill_parent"
		    android:layout_height="50dp"
		    />
</LinearLayout>

  

posted @ 2013-04-22 09:17  崔晓东  阅读(3526)  评论(1编辑  收藏  举报