`
dowhathowtodo
  • 浏览: 798807 次
文章分类
社区版块
存档分类
最新评论

ExpandableListView使用

 
阅读更多

1.自定义ExpandableListView

<ExpandableListView  
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="@android:color/transparent"
    android:listSelector="@android:color/transparent">
</ExpandableListView>

2.在Activity中使用ExpandableListView


ExpandableListView list = (ExpandableListView) findViewById(android.R.id.list);

3.定义group.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:background="@android:color/transparent"
    android:descendantFocusability="blocksDescendants">
	
    <ImageButton
        android:id="@+id/group_imagebutton"
        android:layout_width="50dp"
        android:layout_height="50dp" />
	<LinearLayout 
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_marginLeft="20dp"
	    android:orientation="vertical">
	    <TextView
	        android:id="@+id/group_name"
	        android:layout_width="160dp"
	        android:layout_height="20dp"
	        android:textSize="16sp"/>
	    <TextView
	        android:id="@+id/group_dance"
	        android:layout_width="160dp"
	        android:layout_height="30dp"
	        android:textSize="14sp"/>
	</LinearLayout>>
   

</LinearLayout>

4.定义child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="@android:color/transparent"
    <TextView
        android:id="@+id/child_nation"
        android:layout_width="fill_parent"
        android:layout_height="20dp"
        android:textSize="16sp"/>
    <TextView
        android:id="@+id/child_intro"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"/>
   

</LinearLayout>

5.创建adapter


import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class TeacherListAdapter extends BaseExpandableListAdapter {
	private Context mContext = null;
	private ArrayList<TeacherBean> teacherContentList = null;

	public TeacherListAdapter(Context context) {
		this.mContext = context;
	}

	public Object getChild(int groupPosition, int childPosition) {
		return teacherContentList.get(groupPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		TextView nation = null;
		TextView intro = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.child, null);
		}
		nation = (TextView) convertView.findViewById(R.id.child_nation);
		
		intro = (TextView) convertView.findViewById(R.id.child_intro);
		TeacherBean context = teacherContentList.get(groupPosition);
		nation.setText(context.getNation());
		intro.setText(context.getIntro());
		return convertView;
	}

	public Object getGroup(int groupPosition) {
		return teacherContentList.get(groupPosition);
	}

	public int getGroupCount() {
		return teacherContentList.size();
	}

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

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ImageView image = null;
		TextView name = null;
		TextView dance = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.group, null);
		}
		image = (ImageView) convertView.findViewById(R.id.group_imagebutton);
		name = (TextView) convertView.findViewById(R.id.group_name);
		dance = (TextView) convertView.findViewById(R.id.group_dance);
		TeacherBean context = teacherContentList.get(groupPosition);
		name.setText(context.getName());
		dance.setText(context.getDance());
		image.setImageDrawable(context.getImage());		
		return convertView;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return true;
	}

}


分享到:
评论

相关推荐

    ExpandableListView 使用 包含通用Adapter ViewHolde listview分组显示

    ExpandableListView 使用 包含通用Adapter ViewHolder ,使用时只需在创建Adapter 的时候传入对应的分组布局及明细布局,然后在activity中设置绑定即可。因此Adapter是通用的,只需要跟据不同的需求去改变布局和在...

    ExpandableListView使用示例

    在这个"ExpandableListView使用示例"中,我们将深入探讨如何有效地利用`ExpandableListView`。 首先,了解`ExpandableListView`的基本概念。它是一个特殊的`ListView`,能够显示可扩展的组(Group)和每个组内的...

    Android中ExpandableListView使用

    `ExpandableListView`的使用虽然比普通的`ListView`稍微复杂一些,但其强大的功能和灵活性使其成为处理层次数据的理想选择。通过熟练掌握`ExpandableListView`,开发者可以创建更直观、交互性强的Android应用,提升...

    多级列表ExpandableListView使用实例代码

    本实例代码提供了使用`ExpandableListView`的基本步骤和关键知识点,让我们深入探讨一下。 1. **`ExpandableListView`简介** `ExpandableListView`是Android提供的一个可扩展的列表视图,它扩展了`ListView`的功能...

    ExpandableListView使用详解

    本文将深入解析`ExpandableListView`的使用方法,包括其基本原理、设置数据源、适配器、事件监听以及一些实用技巧。 1. **基本原理** `ExpandableListView`继承自`ListView`,它通过增加子组(Group)和子项...

    Android中ExpandableListView使用示例详解

    "Android中ExpandableListView使用示例详解" Android中ExpandableListView是Android中常用的控件之一,它可以显示层次结构的数据,例如树形结构的菜单等。本文将详细介绍Android中ExpandableListView的使用示例,...

    ExpandableListView 解决子级背景色混乱 和父级展开项唯一效果实现

    ExpandableListView使用了Adapter的视图复用机制来提高性能,当一个条目滚动出屏幕再滚动回来时,它的背景可能会被错误地设置为之前显示的条目的颜色。为了解决这个问题,我们需要在getView()方法中正确地设置每个...

    android Expandablelistview使用

    本教程将深入探讨如何在Android应用中使用ExpandableListView,以及如何实现其核心功能。 首先,了解ExpandableListView的基本结构。这个控件由两层数据构成:父组(Group)和子项(Child)。每个父组可以包含多个...

    ExpandableListView的使用实例

    下面将详细介绍`ExpandableListView`的使用方法和相关知识点。 1. **基本概念** `ExpandableListView`是`ListView`的一个扩展,继承自`AdapterView`。与`ListView`不同的是,`ExpandableListView`支持分组和子项的...

    Andriod中ExpandableListView的使用

    综上所述,`Android中ExpandableListView的使用`涉及到数据模型设计、JSON解析、自定义适配器、事件监听以及性能优化等多个方面,是一个典型的Android UI编程示例。通过实践,开发者可以掌握处理层次结构数据的方法...

    模仿QQ的扩展型很好的ExpandableListView.zip

    3. **视图复用**:为了提高性能,ExpandableListView使用了视图复用机制,通过`getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)`和`getChildView(int groupPosition, int...

    Android源码——基于自定义适配器的ExpandableListView源码.zip

    `1_120820204340_1.png`可能是对ExpandableListView使用过程中的截图,展示了如何在实际应用中显示和操作这些条目。 在自定义适配器时,你还需要关注以下几点: - 数据结构:为了配合ExpandableListView,你需要...

    Android中级联菜单ExpandableListview的使用

    本篇将详细介绍如何在Android应用中使用`ExpandableListView`来创建这种功能。 ### 一、`ExpandableListView`的基本概念 `ExpandableListView`是`ListView`的扩展,它增加了子项(child items)和父项(group ...

    Android之ExpandableListView控件的使用

    本篇文章将深入探讨如何在Android应用中有效地使用`ExpandableListView`。 ### 一、`ExpandableListView`的基本概念 `ExpandableListView`是`ListView`的一个扩展,增加了可折叠和可展开的功能。与`ListView`相比...

    android ExpandableListView三级菜单的使用

    在这个“android ExpandableListView三级菜单的使用”示例中,我们将深入探讨如何创建并操作一个支持三级菜单的`ExpandableListView`。 首先,`ExpandableListView`是`ListView`的扩展,它提供了更丰富的功能,可以...

    ExpandableListView,recyclerview的使用

    使用`ExpandableListView`时,你需要创建适配器,继承自`ExpandableListAdapter`,并实现其中的方法,如`getGroupCount()`、`getChildCount()`、`getGroupView()`和`getChildView()`等,以便为各个组和子项提供视图...

    自定义ExpandableListView结合Sqlite

    为了在`Fragment`中使用`ExpandableListView`,你需要创建一个新的`Fragment`类,然后在`onCreateView()`方法中初始化并设置`ExpandableListView`。 注意,在处理数据时,应考虑数据刷新和异步加载,可以使用`...

Global site tag (gtag.js) - Google Analytics