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

Android开发实现ListView点击展开收起效果示例

程序员文章站 2022-08-27 13:41:34
本文实例讲述了android开发实现listview点击展开收起效果。分享给大家供大家参考,具体如下: 废话不说先上效果: 实际上这是采用一个expandablel...

本文实例讲述了android开发实现listview点击展开收起效果。分享给大家供大家参考,具体如下:

废话不说先上效果:

Android开发实现ListView点击展开收起效果示例

实际上这是采用一个expandablelistview实现的

布局文件很简单:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".mainactivity" >
  <!--提示框-->
  <textview
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="请选择您的类型:"
    android:textsize="30sp"
    android:textcolor="#ffffffff"
    android:paddingleft="5dp"
    android:paddingtop="5dp"
    android:paddingbottom="5dp"
    android:background="#ff000000"/>
  <!--定义一个expandablelistview组件-->
  <expandablelistview
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  </expandablelistview>
</linearlayout>

然后就是具体实现:

这里主要是添加几个必须的属性 大多数方法不用重写

参考我代码中的位置稍加改动就行

public class mainactivity extends activity {
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    //创建一个expandablelistadapter对象
    final expandablelistadapter adapter = new expandablelistadapter() {
      int[] logos = new int[]{
          r.drawable.human1st,
          r.drawable.human1st,
          r.drawable.human2nd,
          r.drawable.human3rd
      };
      private string[] humantypes = new string[]{
          "不是人","聪明人","普通人","我这样的人"
      };
      private string[][] humans = new string[][]{
          {"上仙","大神","荷兰猪"},
          {"超人","一般聪明人","假的聪明人"},
          {"努力的人","快乐的普通人","苦逼的普通人"},
          {"天才","傻逼","蠢萌"}
      };
      //获得制定组的位置、指定子列表项处的字列表项数据
      private textview gettextview(){
        abslistview.layoutparams layoutparams =
            new abslistview.layoutparams(viewgroup.layoutparams.match_parent,164);
        textview textview = new textview(mainactivity.this);
        textview.setlayoutparams(layoutparams);
        textview.setgravity(gravity.center_vertical | gravity.left);
        textview.setpadding(36,0,0,0);
        textview.settextsize(30);
        return textview;
      }
      @override
      public void registerdatasetobserver(datasetobserver observer) {
      }
      @override
      public void unregisterdatasetobserver(datasetobserver observer) {
      }
      @override
      public int getgroupcount() {
        return humantypes.length;
      }
      @override
      public int getchildrencount(int groupposition) {
        return humans[groupposition].length;
      }
      //获取制定组位置处的组数据
      @override
      public object getgroup(int groupposition) {
        return humantypes[groupposition];
      }
      @override
      public object getchild(int groupposition, int childposition) {
        return humans[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) {
        linearlayout linearlayout = new linearlayout(mainactivity.this);
        linearlayout.setorientation(linearlayout.horizontal);
        imageview logo = new imageview(mainactivity.this);
        logo.setimageresource(logos[groupposition]);
        linearlayout.addview(logo);
        textview textview = gettextview();
        textview.settext(getgroup(groupposition).tostring());
        linearlayout.addview(textview);
//        linearlayout.setminimumheight(50);
        return linearlayout;
      }
      @override
      public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) {
        textview textview = gettextview();
        textview.settext(getchild(groupposition,childposition).tostring());
        return textview;
      }
      @override
      public boolean ischildselectable(int groupposition, int childposition) {
        return true;
      }
      @override
      public boolean areallitemsenabled() {
        return false;
      }
      @override
      public boolean isempty() {
        return false;
      }
      @override
      public void ongroupexpanded(int groupposition) {
      }
      @override
      public void ongroupcollapsed(int groupposition) {
      }
      @override
      public long getcombinedchildid(long groupid, long childid) {
        return 0;
      }
      @override
      public long getcombinedgroupid(long groupid) {
        return 0;
      }
    };
    expandablelistview expandablelistview = (expandablelistview) findviewbyid(r.id.list);
    expandablelistview.setonchildclicklistener(new expandablelistview.onchildclicklistener() {
      @override
      public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) {
        toast.maketext(mainactivity.this,"你是一个:" +
            adapter.getchild(groupposition,childposition),toast.length_short).show();
        return true;
      }
    });
    expandablelistview.setadapter(adapter);
  }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android控件用法总结》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android数据库操作技巧总结》及《android资源操作技巧汇总

希望本文所述对大家android程序设计有所帮助。