android_(可扩展的listview)ExpandeableListView

要实现一下效果:

1.listview默认展开(expandGroup方法)

2.group不可收回(group的点击事件设为true)

3.去掉group的导航键(android:groupIndicator=“@null”)

4.group点击无效果(编写<Selector>文件,将true和false两种效果设置为同一个背景色,然后再在group的布局中将background属性填进去)。



MainAcitivity:

<pre name="code" class="java">	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.expandlv);

		elv = (ExpandableListView) findViewById(R.id.elv);

		final String[] group = new String[] { "zhang", "wang", "li" };
		final String[][] child = new String[][] {
				{ "zhangone", "zhangtwo", "zhangsan" },
				{ "wangone", "wangtwo", "wangsan" },
				{ "lione", "litwo", "lisan" } };
		ExpandAdapter adapter = new ExpandAdapter(this, group, child);
		int size = adapter.getGroupCount();
		Log.d("size", size+"");
	
		elv.setOnGroupClickListener(new OnGroupClickListener() { // 监听group的点击事件

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				Log.d("click", "click:  " + group[groupPosition]);
				return false;   //如果返回true,则点击不会收缩
			}
		});
		elv.setOnChildClickListener(new OnChildClickListener() {
			// 监听child的点击事件

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Log.d("click", "click:  " + child[groupPosition][childPosition]);
				return false;
			}
		});
		elv.setAdapter(adapter);
		
		for (int i = 0; i < adapter.getGroupCount(); i++) {
			elv.expandGroup(i);
			Log.d("expand", elv.expandGroup(i)+"");
		}


 

ExpandedAdapter:

package com.example.itemlistener;

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

public class ExpandAdapter extends BaseExpandableListAdapter {

	private LayoutInflater mInflater;
	private String[] group;
	private String[][] child;

	public ExpandAdapter(Context context, String[] group, String[][] child) {
		mInflater = LayoutInflater.from(context);
		this.group = group;
		this.child = child;
	}

	/**
	 * 子控件
	 */
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return child[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) {
		convertView= mInflater.inflate(R.layout.item_child, null);
		TextView tv=(TextView)convertView.findViewById(R.id.tv_child);
		tv.setText(child[groupPosition][childPosition]);
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return child.length;
	}

	/**
	 * 父控件
	 */
	@Override
	public Object getGroup(int groupPosition) {
		return group[groupPosition];
	}

	@Override
	public int getGroupCount() {
		return group.length;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		convertView= mInflater.inflate(R.layout.item_header, null);
		TextView tv=(TextView)convertView.findViewById(R.id.tv_group);
		tv.setText(group[groupPosition]);
		return convertView;
	}

	@Override
	public boolean hasStableIds() {  //这个是判断刷新时是否稳定,具体什么操作我也不清楚
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		Log.d("select", child[groupPosition][childPosition]);
		return true;
	}

}

Layout_Main:

<?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="vertical" >

    <ExpandableListView
        android:id="@+id/elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ExpandableListView>

</LinearLayout>

item_header:

<?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="vertical" >

    <TextView
        android:id="@+id/tv_group"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="group" >
    </TextView>

</LinearLayout>



item_child:

<?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="vertical" >

    <TextView
        android:id="@+id/tv_child"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:paddingLeft="25dp"
        android:text="child" >
    </TextView>

</LinearLayout>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值