完美实现ExpandableListView二级分栏效果
程序员文章站
2023-11-14 14:26:46
本文实例为大家分享了expandablelistview二级分栏效果的具体代码,供大家参考,具体内容如下
对expandablelistview控件进行封装(未自定义...
本文实例为大家分享了expandablelistview二级分栏效果的具体代码,供大家参考,具体内容如下
对expandablelistview控件进行封装(未自定义)直接上代码:
通用viewholder类,仅在setimageresource中添加代码
package com.svp.haoyan.expandablelistview.tool; import android.content.context; import android.graphics.bitmap; import android.util.sparsearray; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.textview; /** * created by haoyan on 2016/3/24. */ public class viewholder { //存储数据 private sparsearray<view> mviews; private int mposition; private view mconvertview; private boolean misexpanded; public viewholder(context context, viewgroup parent, int layoutid, int position,boolean isexpanded) { this.mposition=position; this.mviews= new sparsearray<view>(); this.misexpanded=isexpanded; //三 mconvertview= layoutinflater.from(context).inflate(layoutid,parent,false); //一 mconvertview.settag(this); } public static viewholder get(context context,view convertview, viewgroup parent,int layoutid,int position,boolean isexpanded){ if (convertview==null){ return new viewholder(context,parent,layoutid,position,isexpanded); }else { //五 六 viewholder holder=(viewholder)convertview.gettag(); holder.mposition=position; return holder; } } //通过viewid获取控件 //泛型t返回子类 public <t extends view> t getview(int viewid){ view view =mviews.get(viewid); if (view==null){ //四 view=mconvertview.findviewbyid(viewid); mviews.put(viewid,view); } return (t)view; } //八 public view getmconvertview() { return mconvertview; } /** * 设置textview的值 * @param viewid * @param text * @return */ //四 七 九 public viewholder settext(int viewid,string text){ textview tv= getview(viewid); tv.settext(text); return this; } /** * 一参传id,二参传图片地址,三参为true则调用点击变化效果,默认为false. * */ public viewholder setimageresource(int viewid,int resid,boolean expanded){ imageview view= getview(viewid); view.setimageresource(resid); if (expanded){ if (misexpanded){ view.setvisibility(view.gone); }else { view.setvisibility(view.visible); } } return this; } public viewholder setimagebitamp(int viewid,bitmap bitmap){ imageview view= getview(viewid); view.setimagebitmap(bitmap); return this; } public viewholder setimageuri(int viewid,string uri){ imageview view= getview(viewid); // imageloader.getinstance().loadimg(view,uri); return this; } }
实体类expand_dto:
package com.svp.haoyan.expandablelistview.dto; /** * created by haoyan on 2016/5/7. */ public class expand_dto { private string name; private string contents; private int pic; private int photo; public static class expand_child { private string childname; private string childcont; public string getchildname() { return childname; } public void setchildname(string childname) { this.childname = childname; } public string getchildcont() { return childcont; } public void setchildcont(string childcont) { this.childcont = childcont; } } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getcontents() { return contents; } public void setcontents(string contents) { this.contents = contents; } public int getpic() { return pic; } public void setpic(int pic) { this.pic = pic; } public int getphoto() { return photo; } public void setphoto(int photo) { this.photo = photo; } }
接下来是最主要的adapter:
package com.svp.haoyan.expandablelistview; import android.content.context; import android.database.datasetobserver; import android.view.view; import android.view.viewgroup; import android.widget.expandablelistadapter; import com.svp.haoyan.expandablelistview.tool.viewholder; import java.util.list; import java.util.map; /** * created by haoyan on 2016/4/28. */ public abstract class myexpandablelistadapter<t,f>implements expandablelistadapter { private context mcontext; private list<t> mexpand_dtos; private map<integer, list<f>> mmap = null; public myexpandablelistadapter(context context, list<t> expand_dtos,map<integer, list<f>> map) { this.mcontext = context; this.mexpand_dtos = expand_dtos; this.mmap=map; } /*注册一个观察者(observer),当此适配器数据修改时即调用此观察者。*/ @override public void registerdatasetobserver(datasetobserver observer) { } /*取消先前通过registerdatasetobserver(datasetobserver)方式注册进该适配器中的观察者对象*/ @override public void unregisterdatasetobserver(datasetobserver observer) { } /*获取组的个数*/ @override public int getgroupcount() { return mexpand_dtos.size(); } /*返回在指定group的child数目。*/ @override public int getchildrencount(int groupposition) { return mmap.get(groupposition).size(); } //获取当前父item的数据 @override public t getgroup(int groupposition) { return mexpand_dtos.get(groupposition); } /*获取与在指定group给予child相关的数据。*/ @override public f getchild(int groupposition, int childposition) { return (mmap.get(groupposition).get(childposition)); } /*获取指定组的id*/ @override public long getgroupid(int groupposition) { return groupposition; } //得到子item的id @override public long getchildid(int groupposition, int childposition) { return childposition; } //组和子元素是否持有稳定的id,也就是底层数据的改变不会影响到它们。(没效果) @override public boolean hasstableids() { return true; } //设置父item组件 @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { viewholder viewholder = viewholder.get(mcontext, convertview, parent, r.layout.expand_groupview, groupposition,isexpanded); convert(viewholder, getgroup(groupposition)); return viewholder.getmconvertview(); } //自己写 public abstract void convert(viewholder viewholder, t t); public abstract void convertchild(viewholder viewholder, f f); //设置子item的组件 @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { viewholder viewholder = viewholder.get(mcontext, convertview, parent, r.layout.expand_childview, groupposition,islastchild); convertchild(viewholder, getchild(groupposition,childposition)); return viewholder.getmconvertview(); /*子元素是否处于组中的最后一个(对每个组的最后一个进行操作)*/ //boolean islastchild /*下边是未封装代码,提供参考*/ // string childname = (mmap.get(groupposition).get(childposition)).getchildname(); // layoutinflater inflater = (layoutinflater)mcontext.getsystemservice(context.layout_inflater_service); // convertview = inflater.inflate(r.layout.expand_childview, null); // textview tv_child = (textview) convertview.findviewbyid(r.id.tv_child); // tv_child.settext(childcont); } /*是否选中指定位置上的子元素。*/ @override public boolean ischildselectable(int groupposition, int childposition) { return true; } /*true所有条目可以选择和点击*/ @override public boolean areallitemsenabled() { return false; } /*如果当前适配器不包含任何数据则返回true。经常用来决定一个空视图是否应该被显示。 一个典型的实现将返回表达式getcount() == 0的结果,但是由于getcount()包含了头部和尾部,适配器可能需要不同的行为。*/ @override public boolean isempty() { return false; } /*当组展开状态的时候此方法被调用。*/ @override public void ongroupexpanded(int groupposition) { } /*当组收缩状态的时候此方法被调用。*/ @override public void ongroupcollapsed(int groupposition) { } /*根据所给的子id号和组id号返回唯一的id。此外,若hasstableids()是true,那么必须要返回稳定的id。*/ @override public long getcombinedchildid(long groupid, long childid) { return 0; } /*同上*/ @override public long getcombinedgroupid(long groupid) { return 0; } }
最后是调用类activity:
package com.svp.haoyan.expandablelistview; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.widget.expandablelistview; import com.svp.haoyan.expandablelistview.dto.expand_dto; import com.svp.haoyan.expandablelistview.tool.viewholder; import java.util.arraylist; import java.util.hashmap; import java.util.list; public class mainactivity extends appcompatactivity { private expandablelistview elistview; private list<expand_dto> expanddata; private hashmap<integer, list<expand_dto.expand_child>> map = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); dataview(); expandview(); } private void expandview() { elistview = (expandablelistview) findviewbyid(r.id.expandable_lv); elistview.setadapter(new myexpandablelistadapter<expand_dto,expand_dto.expand_child>(this, expanddata,map) { @override public void convert(viewholder viewholder, expand_dto expand_dto) { viewholder.settext(r.id.tv_expand, expand_dto.getname()) .settext(r.id.tv_expand_two, expand_dto.getcontents()) .setimageresource(r.id.img_expand, expand_dto.getpic(),false) .setimageresource(r.id.img_expand_two, expand_dto.getphoto(),false); } @override public void convertchild(viewholder viewholder, expand_dto.expand_child expand_child) { viewholder.settext(r.id.tv_child,expand_child.getchildname()) .settext(r.id.tv_child2,expand_child.getchildcont()); } }); //去掉自带箭头 elistview.setgroupindicator(null); } private void dataview() { expanddata = new arraylist<expand_dto>(); expand_dto bean_zero = new expand_dto(); bean_zero.setname("android新技能1"); bean_zero.setcontents("1"); bean_zero.setpic(r.drawable.icon_check); bean_zero.setphoto(r.drawable.travel_airfare_cn_position); expanddata.add(bean_zero); expand_dto bean_one = new expand_dto(); bean_one.setname("android新技能2"); bean_one.setcontents("2"); bean_one.setpic(r.drawable.icon_check); bean_one.setphoto(r.drawable.travel_airfare_cn_position); expanddata.add(bean_one); expand_dto bean_two = new expand_dto(); bean_two.setname("android新技能3"); bean_two.setcontents("3"); bean_two.setpic(r.drawable.icon_check); bean_two.setphoto(r.drawable.travel_airfare_cn_position); expanddata.add(bean_two); //这里可以使用sparsearray优化性能 map = new hashmap<integer, list<expand_dto.expand_child>>(); list<expand_dto.expand_child> expand_children_zero=new arraylist<expand_dto.expand_child>(); expand_dto.expand_child child1=new expand_dto.expand_child(); child1.setchildname("11111"); child1.setchildcont("22222"); expand_children_zero.add(child1); expand_dto.expand_child child2=new expand_dto.expand_child(); child2.setchildname("3333333"); child2.setchildcont("4444444"); expand_children_zero.add(child2); map.put(0,expand_children_zero); list<expand_dto.expand_child> expand_children_one=new arraylist<expand_dto.expand_child>(); expand_dto.expand_child child3=new expand_dto.expand_child(); child3.setchildname("55555"); child3.setchildcont("66666"); expand_children_one.add(child3); expand_dto.expand_child child4=new expand_dto.expand_child(); child4.setchildname("77777"); child4.setchildcont("88888"); expand_children_one.add(child4); map.put(1,expand_children_one); list<expand_dto.expand_child> children_data_two=new arraylist<expand_dto.expand_child>(); expand_dto.expand_child child5=new expand_dto.expand_child(); child5.setchildname("99999"); child5.setchildcont("00000"); children_data_two.add(child5); expand_dto.expand_child child6=new expand_dto.expand_child(); child6.setchildname("12345"); child6.setchildcont("54321"); children_data_two.add(child6); map.put(2,children_data_two); } }
完活布局很简单就不贴出来了,加上图片有图有真相:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。