ExpandableListView实现手风琴效果

本文实例为大家分享了ExpandableListView实现手风琴效果的具体代码,供大家参考,具体内容如下

1. 效果示例图

2. 创建方法

(1)第一种方法与ListView等普通控件一样,直接在布局文件中添加ExpandableListView控件即可。

(2)第二种方法则是创建一个Activity继承自ExpandableListActivity,而后通过getExpandableListView()方法可获得一个ExpandableListView对象。

第二种方法仅适用于一个页面中只有一个ExpandableListView的情况。继承的Activity不需要再调用setContentView()方法,在ExpandableListActivity中已经关联了一个系统定义的布局文件。

3. 部分属性和点击事件

android:groupIndicator、android:childIndicator:组条目和子条目前面的图标,默认值为箭头,可设置自定义图片资源。若不显示该图标,则设置为@null。

android:divider、android:childDivider:组和子条目的分隔线。

ExpandableListView的点击事件有两个,分别对应组和子条目的点击事件:

设置组的点击事件:setOnGroupClickListener(OnGroupClickListener listener)

设置子条目的点击事件:setOnChildClickListener(OnChildClickListener listener)

5. 适配器

根据数据源的不同,可使用的适配器有两个:BaseExpandableListAdapter和CursorTreeAdapter,其中,CursorTreeAdapter用于数据源为Cursor对象的情况下,其它情况则使用BaseExpandableListAdapter。

(1)BaseExpandableListAdapter需要重写的方法:

getGroup():从数据源中获取组的数据内容。

getGroupCount():获取组的总数。

getGroupId():获取组的ID。

getGroupView():获取组的视图。

getChild():从数据源中获取子条目的内容。

getChildCount():获取指定组中的子条目总数,并非全部的子条目。

getChildId():获取子条目的ID。

getChildView():获取子条目的视图

hasStableIds():判断id对应的条目是否已经绘制,用于优化列表。

isChildSelectable():子条目是否允许点击,若返回false,则子条目点击事件无效。

(2)CursorTreeAdapter需要重写的方法:

CursorTreeAdapter():构造方法传入组的Cursor对象。

getChildrenCursor():传入组的Cursor对象,获取相应的组的子条目的Cursor对象。

newGroupView():创建组的视图,返回一个新的视图。

bindGroupView():在这里绑定组视图的数据内容,第一个参数即newGroupView()方法的返回值。

newChildView():创建子条目的视图。

bindChildView():绑定子条目视图的数据内容。

6. 简单范例

实现效果图中的例子。

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
  tools:context="com.studying.expandablelistviewdemo.MainActivity">

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

</LinearLayout>

Activity:

public class MainActivity extends Activity {

  private ExpandableListView elv;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    elv = (ExpandableListView) findViewById(R.id.elv_local_data);
    MyBaseExpandableListAdapter adapter = new MyBaseExpandableListAdapter(this, LoadData.getGroupData(), LoadData.getChildData());
    elv.setAdapter(adapter);
  }
}

加载测试数据用的工具类:

public class LoadData {

  // 组的数据内容
  public static List<String> getGroupData() {
    List<String> groupDataList = new ArrayList<>();
    groupDataList.add("计算机基础");
    groupDataList.add("安卓开发");
    return groupDataList;
  }

  // 子条目的数据内容
  public static List<List<String>> getChildData() {
    List<List<String>> childDataList = new ArrayList<>();

    List<String> group1 = new ArrayList<>();
    group1.add("数据结构");
    group1.add("算法");
    group1.add("计算机网络");
    childDataList.add(group1);

    List<String> group2 = new ArrayList<>();
    group2.add("控件使用");
    group2.add("网络操作");
    group2.add("数据存储");
    group2.add("四大组件");
    childDataList.add(group2);

    return childDataList;
  }
}

适配器:

public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {

  private Context mContext;

  private List<String> groupName;
  private List<List<String>> childName;

  public MyBaseExpandableListAdapter(Context mContext, List<String> groupName, List<List<String>> childName) {
    this.mContext = mContext;
    this.groupName = groupName;
    this.childName = childName;
  }

  @Override
  public int getGroupCount() {
    return groupName.size();
  }

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

  @Override
  public String getGroup(int groupPosition) {
    return groupName.get(groupPosition);
  }

  @Override
  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    convertView = View.inflate(mContext, R.layout.item_group_name, null);

    TextView groupName = (TextView) convertView.findViewById(R.id.group_name);
    groupName.setText(getGroup(groupPosition));

    return convertView;
  }

  @Override
  public int getChildrenCount(int groupPosition) {
    return childName.get(groupPosition).size();
  }

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

  @Override
  public String getChild(int groupPosition, int childPosition) {
    return childName.get(groupPosition).get(childPosition);
  }

  @Override
  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    convertView = View.inflate(mContext, R.layout.item_child_name, null);

    TextView childName = (TextView) convertView.findViewById(R.id.child_name);
    childName.setText(getChild(groupPosition, childPosition));

    return convertView;
  }

  @Override
  public boolean hasStableIds() {
    return false;
  }

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android之IphoneTreeView带组指示器的ExpandableListView效果

    之前实现过一次这种效果的ExpandableListView:http://www.jb51.net/article/38482.htm,带效果比较挫,最近,在参考联系人源码PinnedHeaderListView,以及网上各位大侠的源码,封装了一个效果最好,而且使用最简单的IphoneTreeView,下面先看看效果图:  首先让我们看看封装得比较完善的IphoneTreeView: 复制代码 代码如下: public class IphoneTreeView extends Expandab

  • Android中ExpandableListView的用法实例

    本文实例讲述了Android中ExpandableListView的用法,ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下: 首先:在layout的xml文件中定义一个ExpandableListView 复制代码 代码如下: <LinearLayout       android:id="@+id/linearLayout"      android:layout_width="fill_parent"

  • Android改变ExpandableListView的indicator图标实现方法

    本文实例讲述了Android改变ExpandableListView的indicator图标实现方法.分享给大家供大家参考,具体如下: 1)定义xml文件先,命名为expand_list_indicator.xml <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"&

  • Android 关于ExpandableListView刷新问题的解决方法

    正文 首先是最基础的 ExpandableListView vList = (ExpandableListView) this.findViewById(R.id.list); EListAdapter adapter = new EListAdapter(getApplicationContext(), list); //list 是数据源 vList.setAdapter(adapter); //适配器就不写了, 都懂的 class EListAdapter extends BaseExpa

  • Android ExpandableListView展开列表控件使用实例

    你是否觉得手机QQ上的好友列表那个控件非常棒? 不是..... 那也没关系,学多一点知识对自己也有益无害. 那么我们就开始吧. 展开型列表控件, 原名ExpandableListView 是普通的列表控件进阶版, 可以自由的把列表进行收缩, 非常的方便兼好看. 首先看看我完成的截图, 虽然界面不漂亮, 但大家可以自己去修改界面. 该控件需要一个主界面XML 一个标题界面XML及一个列表内容界面XML 首先我们来看看 mian.xml 主界面 复制代码 代码如下: //该界面非常简单, 只要一个E

  • android使用ExpandableListView控件实现小说目录效果的例子

    今天给大家讲讲android的目录实现方法,就像大家看到的小说目录一样,android 提供了ExpandableListView控件可以实现二级列表展示效果,现在给大家讲讲这个控件的用法,下面是XML定义: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/re

  • Android之带group指示器的ExpandableListView(自写)

    我们都知道Android缺省的ExpandableListView的group header无法固定在界面上,当向下滚动后,不能对当前显示的那些child 指示出它们归属于哪个group,在网上搜了很多关于仿手机QQ好友分组效果的ExpandableListView,发现都不尽如意,于是乎在别人的基础上改进了一点点,其实原理还是差不多的,只是增加了往上挤出去的动画效果,而且更加简单,只不过还是没有完全到达跟QQ一样的效果,希望有高手能实现更加逼真的效果,下面我们先看看效果图:  我这里没有把Ex

  • Android ExpandableListView长按事件的完美解决办法

    关于ExpandableListView长按事件处理,网上很多都是使用将上下文菜单注册到ExpandableListView上实现长按事件. 这样做弊端显而易见,不够灵活,不能分别对父项.子项.父项之间.子项之间弹出内容做区分.下面来说我的解决方法,方法有点投机取巧.首先说明一点,使用我这种方法必须使用自定义的BaseExpandableListAdapter,至于为什么,具体后面讲到. ExpandableListView本身有继承自AdapterView的setOnItemLongClick

  • Android UI控件ExpandableListView基本用法详解

    ExpandableListView介绍  ExpandableListView的引入  ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListView).ExpandableListView允许有两个层次:一级列表中有二级列表.  比如在手机设置中,对于分类,有很好的效果.手机版QQ也是这样的效果. 使用ExpandableListView的整体思路 (1)给ExpandableListView设置适配器,那么必须先设置数据源. (2)数据

  • Android 关于ExpandableListView去掉里头分割线的方法

    关于ExpandableListView, 自己写了个类继承自BaseExpandableListAdapter groups,childs 都弄好了,显示出来的效果跟网上很多demo一样,我现在就是想去掉那个组下面各item间的分割线 有知道的么? ------解决方案-------------------- expandableList.setDivider(null); up,还不行就设置一个透明的颜色. ------解决方案-------------------- 可以的, androi

随机推荐