android列表控件实现展开、收缩功能
程序员文章站
2022-05-16 12:14:44
最近在做一个rss阅读器,我看了一看别人做的阅读器中的lisview可以伸缩,展开,我就在网上搜索了一下。果然让我找到,下面就我找到的一个小例子,给大家分享一下。 ...
最近在做一个rss阅读器,我看了一看别人做的阅读器中的lisview可以伸缩,展开,我就在网上搜索了一下。果然让我找到,下面就我找到的一个小例子,给大家分享一下。
activitymain .java
package com.android; import android.app.expandablelistactivity; import android.os.bundle; import android.view.contextmenu; import android.view.menuitem; import android.view.view; import android.view.contextmenu.contextmenuinfo; import android.widget.expandablelistadapter; import android.widget.expandablelistview; import android.widget.textview; import android.widget.toast; import android.widget.expandablelistview.expandablelistcontextmenuinfo; public class activitymain extends expandablelistactivity { private expandablelistadapter madapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.settitle("expandablelist"); madapter = new myexpandablelistadapter(this); setlistadapter(madapter); registerforcontextmenu(this.getexpandablelistview()); } //为列表的每一项创建上下文菜单(即长按后 呼出的菜单) @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { menu.setheadertitle("contexmenu"); menu.add(0,0,0,"contextmenu"); } //单击上下文菜单后的逻辑 @override public boolean oncontextitemselected(menuitem item) { expandablelistcontextmenuinfo info = (expandablelistcontextmenuinfo)item.getmenuinfo(); string title = ((textview) info.targetview).gettext().tostring(); int type =expandablelistview.getpackedpositiontype(info.packedposition); if(type == expandablelistview.packed_position_type_child) { int grouppos = expandablelistview.getpackedpositiongroup(info.packedposition); int childpos = expandablelistview.getpackedpositionchild(info.packedposition); toast.maketext(this, title+"-group index"+grouppos+"child index:"+childpos, toast.length_short).show(); return true; } return false; }
myexpandablelistadapter.java
package com.android; import android.content.context; import android.view.gravity; import android.view.view; import android.view.viewgroup; import android.widget.abslistview; import android.widget.baseexpandablelistadapter; import android.widget.textview; public class myexpandablelistadapter extends baseexpandablelistadapter { private context mcontext; //父列表数据 private string[] groups ={"group1","group2","group3","group4",""}; //子列表数据 private string [][] children ={ {"child1"}, {"child1","child2"}, {"child1","child2","child3"}, {"child1","child2","child3","child4"}, }; myexpandablelistadapter(context context){ mcontext = context; } @override public object getchild(int groupposition, int childposition) { // todo auto-generated method stub return children[groupposition][childposition]; } @override public long getchildid(int groupposition, int childposition) { // todo auto-generated method stub return childposition; } //取子列表中的某一项的view @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { textview textview = getgenericview();; textview.settext(getchild(groupposition, childposition).tostring()); return textview; } @override public int getchildrencount(int groupposition) { // todo auto-generated method stub return children[groupposition].length; } @override public object getgroup(int groupposition) { return groups[groupposition]; } @override public int getgroupcount() { // todo auto-generated method stub return groups.length; } @override public long getgroupid(int groupposition) { // todo auto-generated method stub return groupposition; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { textview textview = getgenericview(); textview.settext(getgroup(groupposition).tostring()); return textview; } @override public boolean hasstableids() { // todo auto-generated method stub return true; } @override public boolean ischildselectable(int groupposition, int childposition) { // todo auto-generated method stub return true; } //获取某一项的view的逻辑 private textview getgenericview(){ abslistview.layoutparams lp = new abslistview.layoutparams(viewgroup.layoutparams.fill_parent,48); textview textview = new textview(mcontext); textview.setlayoutparams(lp); textview.setgravity(gravity.center_vertical | gravity.left); textview.setpadding(32, 0, 0, 0); return textview; } }
运行的结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Android使用线程更换壁纸
下一篇: 老人血压高吃什么好,一起来看看
推荐阅读
-
Android编程实现多列显示的下拉列表框Spinner功能示例
-
Android中使用Spinner实现下拉列表功能
-
android开发实现列表控件滚动位置精确保存和恢复的方法(推荐)
-
javascript使用substring实现的展开与收缩文字功能示例
-
Android控件RadioButton实现多选一功能
-
Android控件BottomSheet实现底边弹出选择列表
-
android BottomSheetDialog新控件解析实现知乎评论列表效果(实例代码)
-
Android自定义View展开菜单功能的实现
-
Android编程实现点击EditText之外的控件隐藏软键盘功能
-
原生js实现新闻列表展开/收起全文功能