android 的 ExpandableListView Example Tutorial
https://www.journaldev.com/9942/android-expandablelistview-example-tutorial
Welcome to Android ExpandableListView Example Tutorial. In this tutorial we’ll implement an ExpandableListView which is used to group list data by categories. It’s sort of menu and submenus in a Android ListView.
Android ExpandableListView
Android ExpandableListView is a view that shows items in a vertically scrolling two-level list. It differs from a ListView by allowing two levels which are groups that can be easily expanded and collapsed by touching to view and their respective children items.
ExpandableListViewAdapter in android loads the data into the items associated with this view.
Following are some important methods that are used by this class :
- setChildIndicator(Drawable)
: This is used to show an indicator besides each item representing the
current state. If the child is the last child for a group, the statestate_last
will be set - setGroupIndicator(Drawable) : An indicator is drawn besides the group representing its state i.e. expanded or collapsed. If the group is empty, the state
state_empty
will be set. If the group is expanded, the statestate_expanded
will be set - getGroupView() : It returns view for the list group header
- getChildView() : It returns view for list child item
The notable interfaces that are implemented by this class are given below :
- ExpandableListView.OnChildClickListener : This is overridden to implement the callback method that’s invoked when a child in the expanded list is clicked
- ExpandableListView.OnGroupClickListener : This is overridden to implement the callback method that’s invoked when a group header in the expanded list is clicked
- ExpandableListView.OnGroupCollapseListener : It is used for notifying when a group is collapsed
- ExpandableListView.OnGroupExpandListener : It is used to notify when a group is expanded
Android ExpandableListView Project Structure
This project consists of three classes.
- A MainActivity that shows the layout with the ExpandableListView
- An ExpandableListDataPump which represents a random data in a List and maps the child item data to the respective group headers using a HashMap
- A CustomExpandableListAdapter which provides the MainActivity with the data from the ExpandableListDataPump class/li>
Android ExpandableList
The activity_main.xml layout consists of an ExpandableListView in a RelativeLayout as shown below :
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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MainActivity">
- <ExpandableListView
- android:id="@+id/expandableListView"
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
- android:divider="@android:color/darker_gray"
- android:dividerHeight="0.5dp" />
- </RelativeLayout>
The android:indicatorLeft is the left bound for an items indicator.
Note : We cannot use the value wrap_content for the android:layout_height attribute of the ExpandableListView
in XML unless the parent’s size is strictly specified
The layout of the group header of each individual list is given below :
list_group.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/listTitle"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
- android:textColor="@android:color/black"
- android:paddingTop="10dp"
- android:paddingBottom="10dp" />
- </LinearLayout>
The layout row of the child items is given below :
list_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/expandedListItem"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
- android:paddingTop="10dp"
- android:paddingBottom="10dp" />
- </LinearLayout>
The ExpandableListDataPump
class is defined as below:
- package com.journaldev.expandablelistview;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- public class ExpandableListDataPump {
- public static HashMap<String, List<String>> getData() {
- HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
- List<String> cricket = new ArrayList<String>();
- cricket.add("India");
- cricket.add("Pakistan");
- cricket.add("Australia");
- cricket.add("England");
- cricket.add("South Africa");
- List<String> football = new ArrayList<String>();
- football.add("Brazil");
- football.add("Spain");
- football.add("Germany");
- football.add("Netherlands");
- football.add("Italy");
- List<String> basketball = new ArrayList<String>();
- basketball.add("United States");
- basketball.add("Spain");
- basketball.add("Argentina");
- basketball.add("France");
- basketball.add("Russia");
- expandableListDetail.put("CRICKET TEAMS", cricket);
- expandableListDetail.put("FOOTBALL TEAMS", football);
- expandableListDetail.put("BASKETBALL TEAMS", basketball);
- return expandableListDetail;
- }
- }
In the above code the expandableListDetail
object is used to map the group header strings to their respective children using an ArrayList of Strings.
CustomExpandableListAdapter.java
- package com.journaldev.expandablelistview;
- import java.util.HashMap;
- import java.util.List;
- import android.content.Context;
- import android.graphics.Typeface;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.TextView;
- public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
- private Context context;
- private List<String> expandableListTitle;
- private HashMap<String, List<String>> expandableListDetail;
- public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
- HashMap<String, List<String>> expandableListDetail) {
- this.context = context;
- this.expandableListTitle = expandableListTitle;
- this.expandableListDetail = expandableListDetail;
- }
- @Override
- public Object getChild(int listPosition, int expandedListPosition) {
- return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
- .get(expandedListPosition);
- }
- @Override
- public long getChildId(int listPosition, int expandedListPosition) {
- return expandedListPosition;
- }
- @Override
- public View getChildView(int listPosition, final int expandedListPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- final String expandedListText = (String) getChild(listPosition, expandedListPosition);
- if (convertView == null) {
- LayoutInflater layoutInflater = (LayoutInflater) this.context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- convertView = layoutInflater.inflate(R.layout.list_item, null);
- }
- TextView expandedListTextView = (TextView) convertView
- .findViewById(R.id.expandedListItem);
- expandedListTextView.setText(expandedListText);
- return convertView;
- }
- @Override
- public int getChildrenCount(int listPosition) {
- return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
- .size();
- }
- @Override
- public Object getGroup(int listPosition) {
- return this.expandableListTitle.get(listPosition);
- }
- @Override
- public int getGroupCount() {
- return this.expandableListTitle.size();
- }
- @Override
- public long getGroupId(int listPosition) {
- return listPosition;
- }
- @Override
- public View getGroupView(int listPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- String listTitle = (String) getGroup(listPosition);
- if (convertView == null) {
- LayoutInflater layoutInflater = (LayoutInflater) this.context.
- getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- convertView = layoutInflater.inflate(R.layout.list_group, null);
- }
- TextView listTitleTextView = (TextView) convertView
- .findViewById(R.id.listTitle);
- listTitleTextView.setTypeface(null, Typeface.BOLD);
- listTitleTextView.setText(listTitle);
- return convertView;
- }
- @Override
- public boolean hasStableIds() {
- return false;
- }
- @Override
- public boolean isChildSelectable(int listPosition, int expandedListPosition) {
- return true;
- }
- }
This class extends BaseExpandableListAdapter and it overrides the methods in the base class to provide the view for the ExpandableListView. getView() fills in the data into the item’s view with the given index.
MainActivity.java
- package com.journaldev.expandablelistview;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- public class MainActivity extends AppCompatActivity {
- ExpandableListView expandableListView;
- ExpandableListAdapter expandableListAdapter;
- List<String> expandableListTitle;
- HashMap<String, List<String>> expandableListDetail;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
- expandableListDetail = ExpandableListDataPump.getData();
- expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
- expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
- expandableListView.setAdapter(expandableListAdapter);
- expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
- @Override
- public void onGroupExpand(int groupPosition) {
- Toast.makeText(getApplicationContext(),
- expandableListTitle.get(groupPosition) + " List Expanded.",
- Toast.LENGTH_SHORT).show();
- }
- });
- expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
- @Override
- public void onGroupCollapse(int groupPosition) {
- Toast.makeText(getApplicationContext(),
- expandableListTitle.get(groupPosition) + " List Collapsed.",
- Toast.LENGTH_SHORT).show();
- }
- });
- expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
- @Override
- public boolean onChildClick(ExpandableListView parent, View v,
- int groupPosition, int childPosition, long id) {
- Toast.makeText(
- getApplicationContext(),
- expandableListTitle.get(groupPosition)
- + " -> "
- + expandableListDetail.get(
- expandableListTitle.get(groupPosition)).get(
- childPosition), Toast.LENGTH_SHORT
- ).show();
- return false;
- }
- });
- }
- }
In the above code we’ve implemented all the interfaces that were discussed before. For the sake of simplicity, we’ll only display a Toast with the name of the item or the state of the group for every click. But these can be easily modified to perform any other operations.
Below is our app with android expandable list view in action.
Note: ExpandableListViews are scrollable by default.
This brings an end to Android ExpandableListView tutorial. You can download the final Android ExpandableListView Project from the below link.
android 的 ExpandableListView Example Tutorial的更多相关文章
- 22.Android之ExpandableListView树形列表学习
Android经常用到树形菜单,一般ExpandableListView可以满足这个需要,今天学习下. XML代码: <?xml version="1.0" encoding ...
- Android之ExpandableListView
ExpandableListView可以用来表现多层级的listView,本文主要是ExpandableListView的一个简单实现 布局文件 <LinearLayout xmlns:andr ...
- Android 之 ExpandableListView 的使用
喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到an ...
- Android中ExpandableListView控件基本使用
本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...
- Android在ExpandableListView控制的基本使用
在本文中,Demo为了展示Android在ExpandableListView用途管制.如该组/儿子ListView绑定数据源. 直接上代码例如以下: 程序结构图: layout文件夹下的 main. ...
- android原生ExpandableListView
android原生可扩展ExpandableListView就是可以伸缩的listView,一条标题下面有多条内容. 这个list的adapter对的数据要求与普通ListView的数据要求也有一些差 ...
- Android中ExpandableListView的使用
ExpandableListView是Android中可以实现下拉list的一个控件,具体的实现方法如下: 首先:在layout的xml文件中定义一个ExpandableListView < L ...
- android 之 ExpandableListView列表中的列表
有时候,我们需要设计这样一个界面,外面有一个列表,当我们点击其中列表中的某个条目时,就会展开这个条目,出现一个新的列表.比如下图:(程序运行的效果图,在这里贴出来) 当我们点击第一项时,视图变为: - ...
- Android 关于ExpandableListView去掉里头的分割线
关于ExpandableListView去掉里面的分割线关于ExpandableListView,自己写了个类继承自BaseExpandableListAdaptergroups,childs都弄好了 ...
随机推荐
- Visual Studio各版本一览!
上图红线标识处为常用版本,最经典的是VC++ 6.0,专为早期C++开发设计.红框标识处是其内部版本,如VS2008,其内部版本为vc9.0,注意查找区分! 目前,最新版本的VS2017已经发布,很大 ...
- 了解一下Windows Cracker
Windows Cracker 消息拆析宏 可以为消息进行参数分解 无需记住或查阅资料来了解WParam和lParam的意义 可以忘记旧的消息处理方式:switch/case 不适合于大型复杂的需要处 ...
- Java精选笔记_JavaBean
JavaBean组件 初始JavaBean JavaBean是Java开发语言中一个可以重复使用的软件组件,它本质上就是一个Java类. 一个标准的JavaBean组件需要遵循一定的编码规范,具体如下 ...
- row_number()over函数的使用(转)
(转)http://hi.baidu.com/122439049/blog/item/0c9c48131b2734d5f7039e13.html row_number() OVER (PARTITIO ...
- MySQL性能优化(七·上)-- 锁机制 之 表锁
前言 数据库的锁主要用来保证数据的一致性的.MyISAM存储引擎只支持表锁,InnoDB存储引擎既支持行锁,也支持表锁,但默认情况下是采用行锁. 一.锁分类 1.按照对数据操作的类型分:读锁,写锁 读 ...
- 查看内存使用情况:free
free命令用于显示内存的使用情况,常见用法如下: [root@localhost ~]$ free # 以KB为单位显示内存使用情况 [root@localhost ~]$ free -m # 以M ...
- 教你一招解决浏览器兼容问题(PostCSS的使用)
我们在处理网页的时候,往往会遇到兼容性的问题.在这个问题上分为两个大的方向:屏幕自适应&浏览器兼容.而屏幕自使用的方法有许多,包括框架之类的,但是浏览器的兼容却没有一个号的框架.在我们日常处理 ...
- ReactiveCocoa - iOS开发的新框架
本文转载至 http://www.infoq.com/cn/articles/reactivecocoa-ios-new-develop-framework ReactiveCocoa(其简称为RAC ...
- 点击button,button背景图片变化
1.设置背景渐变效果,在drawable目录下建buttonshape.xml文件, 内容为: <?xml version="1.0" encoding="utf- ...
- JS-【同页面多次调用】tab选项卡封装
这两天遇到一个页面,同一个页面中同一个特效会用好多次,比如tab,比如轮播等.我又不想很不负责任的复制一遍代码,那样页面臃肿,自己心里也堵得慌.于是就想着把代码封装起来多次调用. 对于封装,只在公开课 ...