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

Android中ExpandableListView使用示例详解

程序员文章站 2023-12-17 18:34:34
本文实例为大家分享了expandablelistview使用示例,供大家参考,具体内容如下 mainactivity: public class expanda...

本文实例为大家分享了expandablelistview使用示例,供大家参考,具体内容如下

mainactivity:

public class expandable_test extends activity {
  private expandablelistview listview;
  private map<string, list<string>> dataset = new hashmap<>();
  private string[] parentlist = new string[]{"第一个菜单", "第二个菜单"};

  private expandablelistviewadpter adpter;
  private context mcontext = this;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_expandable_test);
    listview=(expandablelistview)findviewbyid(r.id.expandablelistviewtext);
    adpter=new expandablelistviewadpter(this,parentlist);
    listview.setadapter(adpter);
  }

}

mainactivity.xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.fae.mobile.testactivity.expandable_test">
<expandablelistview
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/expandablelistviewtext">

</expandablelistview>
</linearlayout>

expandablelistviewadpter:

public class expandablelistviewadpter extends baseexpandablelistadapter {
  private context mcontext;
  private map<string, list<string>> dataset = new hashmap<>();
  private string[] parentlist = new string[]{"第一个菜单", "第二个菜单"};
  private string[][] chdrenlist=new string[][]
      {{"菜单1","菜单1","菜单1","菜单1","菜单1","菜单1"},{"菜单2","菜单2","菜单2","菜单2","菜单2"}};

  public expandablelistviewadpter(context context, string[] parentlist){
    this.mcontext=context;
    this.dataset=dataset;
    this.parentlist=parentlist;
  }

  @override
  public int getgroupcount() {
    return parentlist.length;
  }

  @override
  public int getchildrencount(int groupposition) {
    return chdrenlist[groupposition].length;
  }

  @override
  public object getgroup(int groupposition) {
    return parentlist[groupposition];
  }

  @override
  public object getchild(int groupposition, int childposition) {
    return chdrenlist[groupposition][childposition];
  }

  @override
  public long getgroupid(int groupposition) {
    return groupposition;
  }

  @override
  public long getchildid(int groupposition, int childposition) {
    return childposition;
  }

  @override
  public boolean hasstableids() {
    return true;
  }

  @override
  public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) {
    groupviewholder holder=null;
    if(convertview==null){
      convertview = layoutinflater.from(mcontext).inflate(r.layout.itemlayoutexpandable, null);
      holder=new groupviewholder();
      holder.text=(textview)convertview.findviewbyid(r.id.item_text);
      convertview.settag(holder);
    }
    else {
      holder=(groupviewholder)convertview.gettag();
    }
    holder.text.settext(parentlist[groupposition]);
    return convertview;
  }

  @override
  public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) {
    childviewholder holder=null;
      if(convertview==null){
        convertview = layoutinflater.from(mcontext).inflate(r.layout.itemlayoutexpandable, null);
        holder=new childviewholder();
        holder.text=(textview)convertview.findviewbyid(r.id.item_text);
        convertview.settag(holder);
    }
    else {
        holder=(childviewholder)convertview.gettag();
      }
      holder.text.settext(chdrenlist[groupposition][childposition]);
    return convertview;
  }

  @override
  public boolean ischildselectable(int groupposition, int childposition) {
    return true;
  }
  class groupviewholder {
    textview text;
  }

  class childviewholder {
    textview text;
  }
}

item.xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
<textview
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="bottom"
  android:id="@+id/item_text"/>
</linearlayout>

Android中ExpandableListView使用示例详解

Android中ExpandableListView使用示例详解

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

上一篇:

下一篇: