欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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列表控件实现展开、收缩功能

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