手风琴控件android,ExpandableListView实现手风琴效果

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

1. 效果示例图

a18e76aeafc05c6b661f2500b7b745d3.png

0b38ffd130092d70da8960f0fa5c9c1b.png

5e6b9592909f5523854ef68956211247.png

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

实现效果图中的例子。

布局:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.studying.expandablelistviewdemo.mainactivity">

android:id="@+id/elv_local_data"

android:layout_width="match_parent"

android:layout_height="match_parent" />

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;

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值