android的ExpandableListView

activity_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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.expandablelistview.MainActivity" >

    <ExpandableListView 
        android:id="@+id/ExpandableListView1_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       android:groupIndicator="@null"
        
                >
        
    </ExpandableListView>

</RelativeLayout>
<!--  android:groupIndicator="@null"去掉自带的箭头图标 -->

group_item.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:orientation="horizontal" >
    
    <ImageView
        android:id="@+id/imageViewgroup_1" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        />
    
	<TextView 
	    android:id="@+id/textViewgroup_1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	  
	    />
</LinearLayout>

child_item.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:orientation="horizontal" >
    
    
     <ImageView
        android:id="@+id/imageViewchild_1" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:padding="10dp"
        />
    
	<TextView 
	    android:id="@+id/textViewchild_1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="sdfds"
	    android:padding="10dp"
	  
		/>
</LinearLayout>

MainActivity

package com.example.expandablelistview;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	private ExpandableListView expandableListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		expandableListView=(ExpandableListView) findViewById(R.id.ExpandableListView1_1);
		expandableListView.setAdapter(new MyExpandableListAdapter());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	class MyExpandableListAdapter extends BaseExpandableListAdapter{
		private String[] skills = new String[]{
	            "WORD", "EXCEL", "EMAIL", "PPT"
	    };
		private String[][] groups = new String[][]{
		            {"文档编辑", "文档排版", "文档处理", "文档打印"},
		            {"表格编辑", "表格排版", "表格处理", "表格打印"},
		            {"收发邮件", "管理邮箱", "登录登出", "注册绑定"},
		            {"演示编辑", "演示排版", "演示处理", "演示打印"},
		    };
		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return skills.length;
		}
		//二级列表的数量
		@Override
		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generated method stub
			return groups[groupPosition].length;
		}
		//返回每一组的对象
		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return skills[groupPosition];
		}
		//返回每组中的列表项
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return groups[groupPosition][childPosition];
		}

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

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

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

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(convertView==null){
				convertView=getLayoutInflater().inflate(R.layout.group_item, null);

			}
			ImageView imageView=(ImageView) convertView.findViewById(R.id.imageViewgroup_1);
			TextView textView=(TextView) convertView.findViewById(R.id.textViewgroup_1);
			imageView.setImageResource(R.drawable.ic_launcher);
			textView.setText(skills[groupPosition]);
			return convertView;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(convertView==null){
				convertView=getLayoutInflater().inflate(R.layout.child_item, null);
				ImageView imageView=(ImageView) convertView.findViewById(R.id.imageViewchild_1);
				TextView textView=(TextView) convertView.findViewById(R.id.textViewchild_1);
				imageView.setImageResource(R.drawable.ic_launcher);
				textView.setText(groups[groupPosition][childPosition]);
			}
			return convertView;
		}

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

	}
}

时间: 2024-08-02 23:07:33

android的ExpandableListView的相关文章

22.Android之ExpandableListView树形列表学习

Android经常用到树形菜单,一般ExpandableListView可以满足这个需要,今天学习下. XML代码: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent

Android中ExpandableListView控件基本使用

本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 main.xml 文件源代码例如以下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a

Android在ExpandableListView控制的基本使用

在本文中,Demo为了展示Android在ExpandableListView用途管制.如该组/儿子ListView绑定数据源. 直接上代码例如以下: 程序结构图: layout文件夹下的 main.xml 文件源代码例如以下: <? xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an

Android中ExpandableListView中嵌套ListView

最近项目挺紧张,一直没有时间总结学习,今天把最近遇到的一个奇葩的设计,做一下总结.其他的好多的APP中做的通讯录都类似微信通讯录这样,但是有这样一个需求的设计. 就是分为两个组,第一个组不需要A-Z的索引,第二组需要A-Z的索引.以下是我的实现思路. 1.实现思路 1.写布局文件,将ExpandableListView添加到布局文件. 2.写MyExpandableListViewAdapter的实现 3.ListView设置为不可滑动. 4.再第一组中,添加一个ListView,第二个组中,添

Android 利用ExpandableListView显示和查询仿QQ分组列表用户信息

在我们的项目开发过程中,经常会对用户的信息进行分组,即通过组来显示用户的信息,同时通过一定的查询条件来显示查询后的相关用户信息,并且通过颜色选择器来设置列表信息的背景颜色. 其中借鉴xiaanming:http://blog.csdn.net/xiaanming/article/details/12684155 下面来看看项目运行后的效果图以及代码结构图: 下面通过代码来实现整个效果. 1.主界面布局activity_main.xml <span style="font-size:18px

Android的ExpandableListView的学习

最近在公司做项目,有一个小模块,需要用到ExpandableListView这个控件,所以在工作中边学习边工作.根据需求,在进入ExpandableLisView的时候默认展开第一组,需要用到这个属性,expandableListView.expandGroup(0); 在ExpandableListView中,当点击一个组展开时,其他展开的组是不自动关上的,这需要在监听事件中处理一下: @Override public void onGroupExpand(int groupPosition)

Android中ExpandableListView的使用

ExpandableListView是Android中可以实现下拉list的一个控件,具体的实现方法如下: 首先:在layout的xml文件中定义一个ExpandableListView view plain copy to clipboard print ? < LinearLayout android:id ="@+id/linearLayout" android:layout_width ="fill_parent" android:layout_hei

android原生ExpandableListView

android原生可扩展ExpandableListView就是可以伸缩的listView,一条标题下面有多条内容. 这个list的adapter对的数据要求与普通ListView的数据要求也有一些差别,这个list需要有两个数据源 一半需要 List<String> groups 作为group 和一个 Map<String , List<String>> children 作为children,他们最好意义对应 ExpandableListView使用起来和普通的l

Android的ExpandableListView的动画展开效果和使用traceview的性能优化

Android的原生提供和展开分组的ListView:ExpandableListView,然而相比于iOS上原生提供的UITableView,其UI能力不足,比如没有原生的动画展开和收起效果支持. 然而性能的优化是没有止境的,当分组内的子view(childView)变得复杂,或者ListView的parent结构复杂,例如内嵌与其它 LinearLayout, FrameLayout或者ScrollView之中,并且parent的使用自定义的重写的onMeasure()方法时,生成child