Android 可展开列表视图示例教程

欢迎学习Android ExpanableListView示例教程。在本教程中,我们将实现一个ExpanableListView ,用于按类别对列表数据进行分组。它是安卓ListView.]中的菜单和子菜单

Android可扩展ListView

Android ExpanableListView 是一种以垂直滚动的两级列表展示项目的视图。与ListView不同的是,它允许两个级别,即** 组** ,可以通过触摸查看及其各自的** 子** 项轻松展开和折叠。Android中的** ExpanableListViewAdapter** 将数据加载到该view关联的项中。下面是该类使用的一些重要方法:

  • setChildIndicator(Drawable) :用于在代表当前状态的每一项旁边显示一个指示器。如果子项是组的最后一个子项,则会设置状态STATE_LAST
  • setGroupIndicator(可绘制) :在分组旁边绘制一个指示器,表示分组的状态,即展开或折叠。如果组为空,则设置状态STATE_EMPTY。如果展开组,则设置状态STATE_EXPANDED
  • getGroupView() :返回列表分组表头的view
  • getChildView() :返回列表子项的view

此类实现的值得注意的接口如下所示:

  • ExpanableListView.OnChildClickListener :重写该参数以实现在单击展开列表中的子级时调用的回调方法
  • ExpanableListView.OnGroupClickListener :重写该参数,以实现点击展开列表中的组头时调用的回调方法 *ExpandableListView.OnGroupCollapseListener :用于通知群组何时折叠
  • ExpanableListView.OnGroupExpanListener :用于在组展开时通知

Android ExpandableListView项目结构

Android ExpandableListView,Android ExpandableListView example,Android中的ExpandableListView该项目由三个类组成。

  • 用ExpanableListView显示布局的 MainActivity**
  • 表示列表中的随机数据并使用HashMap将子项数据映射到各自的组头的 ExpanableListDataPump**
  • A CustomExpanableListAdapter** ,它为MainActivity提供来自ExpanableListDataPump类的数据/li>

Android可扩展ListView代码

Active_main.xml布局由RelativeLayout中的ExpanableListView组成,如下所示:active_main.xml

 1<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
 2    xmlns:tools="https://schemas.android.com/tools"
 3    android:layout_width="match_parent"
 4    android:layout_height="match_parent"
 5    android:paddingLeft="@dimen/activity_horizontal_margin"
 6    android:paddingRight="@dimen/activity_horizontal_margin"
 7    android:paddingTop="@dimen/activity_vertical_margin"
 8    android:paddingBottom="@dimen/activity_vertical_margin"
 9    tools:context=".MainActivity">
10
11    <ExpandableListView
12        android:id="@+id/expandableListView"
13        android:layout_height="match_parent"
14        android:layout_width="match_parent"
15        android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
16        android:divider="@android:color/darker_gray"
17        android:dividerHeight="0.5dp" />
18
19</RelativeLayout>

android:indeatorLeft 是Items指示器的左界。** 注意** :除非严格指定父列表的大小,否则不能对XML中ExpanableListView的** Android:Layout_Height** 属性使用WRAP_CONTENT,每个单独列表的组头布局如下:List_group.xml

 1<?xml version="1.0" encoding="utf-8"?>
 2
 3<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 4    android:orientation="vertical" android:layout_width="match_parent"
 5    android:layout_height="match_parent">
 6    <TextView
 7        android:id="@+id/listTitle"
 8        android:layout_width="fill_parent"
 9        android:layout_height="wrap_content"
10        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
11        android:textColor="@android:color/black"
12        android:paddingTop="10dp"
13        android:paddingBottom="10dp" />
14</LinearLayout>

子项的布局行如下:List_item.xml

 1<?xml version="1.0" encoding="utf-8"?>
 2
 3<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 4    android:orientation="vertical" android:layout_width="match_parent"
 5    android:layout_height="wrap_content">
 6    <TextView
 7        android:id="@+id/expandedListItem"
 8        android:layout_width="fill_parent"
 9        android:layout_height="wrap_content"
10        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
11        android:paddingTop="10dp"
12        android:paddingBottom="10dp" />
13</LinearLayout>

ExpanableListDataPump类定义如下:

 1package com.journaldev.expandablelistview;
 2
 3import java.util.ArrayList;
 4import java.util.HashMap;
 5import java.util.List;
 6
 7public class ExpandableListDataPump {
 8    public static HashMap<String, List<String>> getData() {
 9        HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
10
11        List<String> cricket = new ArrayList<String>();
12        cricket.add("India");
13        cricket.add("Pakistan");
14        cricket.add("Australia");
15        cricket.add("England");
16        cricket.add("South Africa");
17
18        List<String> football = new ArrayList<String>();
19        football.add("Brazil");
20        football.add("Spain");
21        football.add("Germany");
22        football.add("Netherlands");
23        football.add("Italy");
24
25        List<String> basketball = new ArrayList<String>();
26        basketball.add("United States");
27        basketball.add("Spain");
28        basketball.add("Argentina");
29        basketball.add("France");
30        basketball.add("Russia");
31
32        expandableListDetail.put("CRICKET TEAMS", cricket);
33        expandableListDetail.put("FOOTBALL TEAMS", football);
34        expandableListDetail.put("BASKETBALL TEAMS", basketball);
35        return expandableListDetail;
36    }
37}

在上面的代码中,expandableListDetail对象用于使用Strings的ArrayList将组标题字符串映射到它们各自的子级。CustomExpanableListAdapter.java

 1package com.journaldev.expandablelistview;
 2
 3import java.util.HashMap;
 4import java.util.List;
 5import android.content.Context;
 6import android.graphics.Typeface;
 7import android.view.LayoutInflater;
 8import android.view.View;
 9import android.view.ViewGroup;
10import android.widget.BaseExpandableListAdapter;
11import android.widget.TextView;
12
13public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
14
15    private Context context;
16    private List<String> expandableListTitle;
17    private HashMap<String, List<String>> expandableListDetail;
18
19    public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
20                                       HashMap<String, List<String>> expandableListDetail) {
21        this.context = context;
22        this.expandableListTitle = expandableListTitle;
23        this.expandableListDetail = expandableListDetail;
24    }
25
26    @Override
27    public Object getChild(int listPosition, int expandedListPosition) {
28        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
29                .get(expandedListPosition);
30    }
31
32    @Override
33    public long getChildId(int listPosition, int expandedListPosition) {
34        return expandedListPosition;
35    }
36
37    @Override
38    public View getChildView(int listPosition, final int expandedListPosition,
39                             boolean isLastChild, View convertView, ViewGroup parent) {
40        final String expandedListText = (String) getChild(listPosition, expandedListPosition);
41        if (convertView == null) {
42            LayoutInflater layoutInflater = (LayoutInflater) this.context
43                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
44            convertView = layoutInflater.inflate(R.layout.list_item, null);
45        }
46        TextView expandedListTextView = (TextView) convertView
47                .findViewById(R.id.expandedListItem);
48        expandedListTextView.setText(expandedListText);
49        return convertView;
50    }
51
52    @Override
53    public int getChildrenCount(int listPosition) {
54        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
55                .size();
56    }
57
58    @Override
59    public Object getGroup(int listPosition) {
60        return this.expandableListTitle.get(listPosition);
61    }
62
63    @Override
64    public int getGroupCount() {
65        return this.expandableListTitle.size();
66    }
67
68    @Override
69    public long getGroupId(int listPosition) {
70        return listPosition;
71    }
72
73    @Override
74    public View getGroupView(int listPosition, boolean isExpanded,
75                             View convertView, ViewGroup parent) {
76        String listTitle = (String) getGroup(listPosition);
77        if (convertView == null) {
78            LayoutInflater layoutInflater = (LayoutInflater) this.context.
79                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
80            convertView = layoutInflater.inflate(R.layout.list_group, null);
81        }
82        TextView listTitleTextView = (TextView) convertView
83                .findViewById(R.id.listTitle);
84        listTitleTextView.setTypeface(null, Typeface.BOLD);
85        listTitleTextView.setText(listTitle);
86        return convertView;
87    }
88
89    @Override
90    public boolean hasStableIds() {
91        return false;
92    }
93
94    @Override
95    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
96        return true;
97    }
98}

此类扩展了BaseExpanableListAdapter ,并重写了基类中的方法以提供ExpanableListView的视图。GetView()将数据填充到具有给定索引的项的视图中。MainActivity.java

 1package com.journaldev.expandablelistview;
 2
 3import android.support.v7.app.AppCompatActivity;
 4import android.os.Bundle;
 5import android.view.View;
 6import android.widget.ExpandableListAdapter;
 7import android.widget.ExpandableListView;
 8import android.widget.Toast;
 9
10import java.util.ArrayList;
11import java.util.HashMap;
12import java.util.List;
13
14public class MainActivity extends AppCompatActivity {
15
16    ExpandableListView expandableListView;
17    ExpandableListAdapter expandableListAdapter;
18    List<String> expandableListTitle;
19    HashMap<String, List<String>> expandableListDetail;
20
21    @Override
22    protected void onCreate(Bundle savedInstanceState) {
23        super.onCreate(savedInstanceState);
24        setContentView(R.layout.activity_main);
25        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
26        expandableListDetail = ExpandableListDataPump.getData();
27        expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
28        expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
29        expandableListView.setAdapter(expandableListAdapter);
30        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
31
32            @Override
33            public void onGroupExpand(int groupPosition) {
34                Toast.makeText(getApplicationContext(),
35                        expandableListTitle.get(groupPosition) + " List Expanded.",
36                        Toast.LENGTH_SHORT).show();
37            }
38        });
39
40        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
41
42            @Override
43            public void onGroupCollapse(int groupPosition) {
44                Toast.makeText(getApplicationContext(),
45                        expandableListTitle.get(groupPosition) + " List Collapsed.",
46                        Toast.LENGTH_SHORT).show();
47
48            }
49        });
50
51        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
52            @Override
53            public boolean onChildClick(ExpandableListView parent, View v,
54                                        int groupPosition, int childPosition, long id) {
55                Toast.makeText(
56                        getApplicationContext(),
57                        expandableListTitle.get(groupPosition)
58                                + " -> "
59                                + expandableListDetail.get(
60                                expandableListTitle.get(groupPosition)).get(
61                                childPosition), Toast.LENGTH_SHORT
62                ).show();
63                return false;
64            }
65        });
66    }
67
68}

在上面的代码中,我们实现了前面讨论过的所有接口。为简单起见,我们将在每次单击时仅显示带有项目名称或组状态的Toast。但是可以很容易地对它们进行修改,以执行任何其他操作。下面是我们的应用程序,带有Android可扩展列表视图。ExpanableListView,Android ExpanableListView,Android ExpanableListView example备注 :ExpanableListView默认可滚动。这结束了Android ExpanableListView教程。您可以通过下面的链接下载最终的** Android ExpanableListView项目** 。

下载Android ExpandableListView示例项目

Published At
Categories with 技术
Tagged with
comments powered by Disqus