扩展下拉菜单的实现方式

扩展下拉菜单的实现方式

扩展下拉菜单的实现方式下面将演示两种。方法一是通过继承BaseExpandableListAdapter,实现父类的方法,特别是通过getGroupView()getChildView()方法实现下拉列别的样式。方法二是直接实现SimpleExpandableListAdapter类,通过传递groupList对象和group的布局样式,以及childList对象和child夫人布局样式。

一、继承BaseExpandableListAdapter

(1)   声明一个类继承BaseExpandableListAdapter类,给类为ExpandableListAdapter

(2)   实现带有三个参数的构造函数,一个Activity对象,一个List<>对象,一个List<List<>>对象。

(3)   分别覆写BaseExpandableListAdapter方法。

(4)   实现一个方法getGenericView(String,string)方法,该方法被getGroupView()getChildView()调用。

ExpandableListAdapter对象:

package com.example.expandablelistdemo;

import java.util.List;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableAdapter extends BaseExpandableListAdapter {

	private List<String> groupArray;
	private List<List<String>> childArray;
	private Activity activity;
	public ExpandableAdapter(Activity activity,List<String> groupArray,List<List<String>> childArray) {
		// TODO Auto-generated constructor stub
		this.activity = activity;
		this.groupArray = groupArray;
		this.childArray = childArray;
	}
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childArray.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		String string = (String) getChild(groupPosition, childPosition);
		return getGenericView(string);
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return childArray.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groupArray.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groupArray.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
		String string = (String) getGroup(groupPosition);
		return getGenericView(string);
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}

	
	public TextView getGenericView(String string) {
		AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 60);
		TextView textView = new TextView(activity);
		textView.setLayoutParams(layoutParams);
		//Center the text vertically
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		//Set the text starting postion
		textView.setPadding(60, 0, 0, 0);
		textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.brid,0,0,0);
		
		textView.setText(string);
		return textView;
	}
}


 

(5)   Activity类中,生成一个ExpandableListActivity对象

(6)   生成一个ExpandableListAdapter对象,传递三个参数

(7)   利用ExpandableListActivity对象的setListAdapter方法把Adapter对象传给该对象。

继承Activity的类:

package com.example.expandablelistdemo;

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

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.ExpandableListView;

public class Activity3 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity3);
        List<String> groupArray = new ArrayList<String>();
        groupArray.add("第一章");
        groupArray.add("第二章");
        List<String> child1Array = new ArrayList<String>();
        child1Array.add("第1.1章");
        child1Array.add("第1.2章");
        child1Array.add("第1.3章");
        List<String> child2Array = new ArrayList<String>();
        child2Array.add("第2.1章");
        child2Array.add("第2.2章");
        List<List<String>> childArray = new ArrayList<List<String>>();
        childArray.add(child1Array);
        childArray.add(child2Array);
        //声明一个ExpandableListVie对象
        ExpandableListView expandableListView = new ExpandableListView(Activity3.this);
        //声明一个ExpandableAdapter对象
        ExpandableAdapter expandableAdapter = new ExpandableAdapter(this, groupArray, childArray);
        expandableListView.setAdapter(expandableAdapter);
        addContentView(expandableListView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity3, menu);
        return true;
    }

    
}


二、通过SimpleExpandableListAdapter实现

(1)   生成一个Activity对象继承成ExpandableListActivity

(2)   创建一级目录的List对象,接着创建二级目录的List<List<>>对象,其中每一个List<>对应着一个一级目录

(3)   创建一级目录对应的Layout布局,创建二级目录对应的Layout布局

(4)   声明一个SimpleExpandableListAdapter对象,把以上实现的参数传递过去。

 实现ExpandableListActivity的类:

package com.example.expandablelistdemo1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter;

public class Activity4 extends ExpandableListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity4);
		//声明一级目录
		List<Map<String, String>> groups = new ArrayList<Map<String,String>>();
		Map<String, String> group1 = new HashMap<String, String>();
		group1.put("group", "第一章");
		Map<String, String> group2 = new HashMap<String, String>();
		group2.put("group", "第二章");
		groups.add(group1);
		groups.add(group2);
		//声明二级子目录,对应的一级子目录为group1
		List<Map<String, String>> child1 = new ArrayList<Map<String,String>>();
		Map<String, String> child1Data1 = new HashMap<String, String>();
		child1Data1.put("child", "第1.1章");
		Map<String, String> child1Data2 = new HashMap<String, String>();
		child1Data2.put("child", "第1.2章");
		child1.add(child1Data1);
		child1.add(child1Data2);
		//声明二级子目录,对应一级子目录为group2
		List<Map<String, String>> child2 = new ArrayList<Map<String,String>>();
		Map<String, String> child2Data1 = new HashMap<String, String>();
		child2Data1.put("child", "第2.1章");
		Map<String, String> child2Data2 = new HashMap<String, String>();
		child2Data2.put("child", "第2.2章");
		child2.add(child2Data1);
		child2.add(child2Data2);
		List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();
		childs.add(child1);
		childs.add(child2);
		
		//声明Adapter类
		SimpleExpandableListAdapter expandableListAdapter = new SimpleExpandableListAdapter(this, 
															groups, R.layout.group, new String[]{"group"}, new int[]{R.id.group}, 
															childs, R.layout.child, new String[]{"child"}, new int[]{R.id.child});
		setListAdapter(expandableListAdapter);
		
	}
	
}


Activity的布局文件:

<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" >

    <ExpandableListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:drawSelectorOnTop="true" >

    </ExpandableListView>

    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="@string/textView" />

</RelativeLayout>


 

group.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/bird"
    android:gravity="center_vertical"
    android:paddingBottom="5dp"
    android:paddingLeft="50dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:textSize="25sp" >

</TextView>

child.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/child"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:paddingLeft="80dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:textSize="18sp" >

</TextView>



第一个工程资源:http://download.csdn.net/detail/zhaoshiqing7/4490513

第二个工程资源:http://download.csdn.net/detail/zhaoshiqing7/4490518

一个额外的工程实现qq组展示:http://download.csdn.net/detail/zhaoshiqing7/4496683

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值