网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

ExpandableListView如何实现手风琴效果-创新互联

这篇文章将为大家详细讲解有关ExpandableListView如何实现手风琴效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

为常宁等地区用户提供了全套网页设计制作服务,及常宁网站建设行业解决方案。主营业务为成都做网站、成都网站建设、常宁网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

具体内容如下

1. 效果示例图

ExpandableListView如何实现手风琴效果

ExpandableListView如何实现手风琴效果

ExpandableListView如何实现手风琴效果

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. 简单范例

实现效果图中的例子。

布局:




  

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 getGroupData() {
    List groupDataList = new ArrayList<>();
    groupDataList.add("计算机基础");
    groupDataList.add("安卓开发");
    return groupDataList;
  }

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

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

    List 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 groupName;
  private List> childName;

  public MyBaseExpandableListAdapter(Context mContext, List groupName, List> 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;
  }
}

关于“ExpandableListView如何实现手风琴效果”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


当前题目:ExpandableListView如何实现手风琴效果-创新互联
本文链接:http://bjjierui.cn/article/pgide.html

其他资讯