广告
返回顶部
首页 > 资讯 > 移动开发 >Android 中使用ExpandableListView 实现分组的实例
  • 535
分享到

Android 中使用ExpandableListView 实现分组的实例

expandablelistview分组Android 2022-06-06 05:06:07 535人浏览 安东尼
摘要

 Android 中使用ExpandableListView 实现分组 一个视图显示垂直滚动两级列表中的条目。这不同于列表视图,允许两个层次,类似于QQ的好友分组。要

 Android 中使用ExpandableListView 实现分组

一个视图显示垂直滚动两级列表中的条目。这不同于列表视图,允许两个层次,类似于QQ的好友分组。要实现这个效果的整体思路为:

1.要给ExpandableListView 设置适配器,那么必须先设置数据源。

2.数据源,就是此处的适配器类,此方法继承了BaseExpandableListAdapter,它是ExpandableListView的一个子类。需要重写里面的多个方法。方法的意思,代码中都有详细的注释。数据源中,用到了自定义的View布局,此时根据自己的需求,来设置组和子项的布局样式。getChildView()和getGroupView()方法设置自定义布局。

3.数据源设置好,直接给ExpandableListView.setAdapter()即可实现此收缩功能。

下面是我自己简单做的一个显示效果:

 

layout中主视图expandable_layout.xml(ExpandableListView布局):


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="Http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ExpandableListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/el">
  </ExpandableListView>
</LinearLayout>

Layout中group_layout.xml(分组组名展示布局):


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="horizontal" android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center_vertical">
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/ic_launcher"
    android:id="@+id/iv_group" />
  <TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_group" />
</LinearLayout>

Layout中child_layout.xml(子菜单item布局):


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="horizontal" android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center_vertical"
  android:paddingLeft="50dp">
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/ic_launcher"
    android:id="@+id/iv_item" />
  <TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_item" />
</LinearLayout>

Layout中alertdialog_layout.xml(AlertDialog自定义显示布局):


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">
  <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et"
    android:hint="请输入想对他说的话"/>
</LinearLayout>

Activity中Java实现代码(ExpandableListViewDemo.java):


import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableListViewDemo extends AppCompatActivity {
  ExpandableListView el;
  //定义分组名以及对应的图片数组,需一一对应
  String[] country={"魏国","蜀国","吴国"};
  int[] icon={R.mipmap.wei,R.mipmap.shu,R.mipmap.wu};
  //使用二维定义组内成员以及对应头像,同样需要以一一对应
  String[][] heros={{"司马懿","郭嘉","夏侯惇","甄姬"},{"刘备","赵云","张飞"},{"孙权","周瑜"}};
  int[][] icons={{R.mipmap.simayi,R.mipmap.guojia,R.mipmap.xiahoudun,R.mipmap.zhenji},
      {R.mipmap.liubei,R.mipmap.zhaoyun,R.mipmap.zhangfei},{R.mipmap.sunquan,R.mipmap.zhouyu}};
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.expandable_layout);
    el= (ExpandableListView) findViewById(R.id.el);
    //设置点击下拉子菜单的监听事件
    el.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
      @Override
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        //获取分组成员的姓名
        TextView tv = (TextView) v.findViewById(R.id.tv_item);
        String name = tv.getText().toString();
        //调用show方法(自定义AlertDialog)
        show(v,name);
        return false;
      }
    });
    el.setAdapter(new MyAdapter());//设置自定义适配器
  }
  //定义show方法调出AlertDialog(不再赘述,详情请看前期相关博文,文章末尾有链接)
  public void show(View v,String name){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(name);//设置标题名为传入的字符串(分组内点击对应的人物名)
    View msg = getLayoutInflater().inflate(R.layout.altertdialog_layout,null);
    builder.setView(msg);
    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(ExpandableListViewDemo.this, "已发送", Toast.LENGTH_SHORT).show();
      }
    });
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(ExpandableListViewDemo.this, "不想说了", Toast.LENGTH_SHORT).show();
      }
    });
    builder.show();
  }
  //设置自定义适配器
  class MyAdapter extends BaseExpandableListAdapter{
    //获取国家个数
    @Override
    public int getGroupCount() {
      return country.length;
    }
    //获取每个国家对应的人数
    @Override
    public int getChildrenCount(int groupPosition) {
      return heros[groupPosition].length;
    }
    //获取对应国家名
    @Override
    public Object getGroup(int groupPosition) {
      return country[groupPosition];
    }
    //获取对应国家对应的任务
    @Override
    public Object getChild(int groupPosition, int childPosition) {
      return heros[groupPosition][childPosition];
    }
    //获取选择的国家对应数组下标
    @Override
    public long getGroupId(int groupPosition) {
      return groupPosition;
    }
    //获取选择的任务对应数组下标
    @Override
    public long getChildId(int groupPosition, int childPosition) {
      return childPosition;
    }
    //表示人物和国家ID是否稳定的更改底层数据
    @Override
    public boolean hasStableIds() {
      return true;
    }
    //获取一个视图显示国家名以及对应的图标(ListView适配器优化)
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
      ViewHolder vh;
      if(convertView==null){
        convertView=getLayoutInflater().inflate(R.layout.group_layout,null);
        vh=new ViewHolder();
        vh.tv= (TextView) convertView.findViewById(R.id.tv_group);
        vh.iv= (ImageView) convertView.findViewById(R.id.iv_group);
        convertView.setTag(vh);
      }
      vh= (ViewHolder) convertView.getTag();
      vh.tv.setText(country[groupPosition]);
      vh.iv.setImageResource(icon[groupPosition]);
      return convertView;
    }
    //获取一个视图显示国家对应人物以及对应的图标(ListView适配器优化)
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
      ViewHolder vh;
      if(convertView==null){
        convertView=getLayoutInflater().inflate(R.layout.child_layout,null);
        vh=new ViewHolder();
        vh.tv= (TextView) convertView.findViewById(R.id.tv_item);
        vh.iv= (ImageView) convertView.findViewById(R.id.iv_item);
        convertView.setTag(vh);
      }
      vh= (ViewHolder) convertView.getTag();
      vh.tv.setText(heros[groupPosition][childPosition]);
      vh.iv.setImageResource(icons[groupPosition][childPosition]);
      return convertView;
    }
    class ViewHolder{
      ImageView iv;
      TextView tv;
    }
    //设置子选项(对应人物)是可选的
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
    }
  }
}

补充说明:

java实现代码中用到自定义适配器ListView的优化,优化步骤如下:

(1)使用固定宽高(match_parent)的ListView,有助于在填充item(ListView中每行的布局)时避免重复渲染ListView组件,导致重复多次调用getView方法。

(2)Convertview用来重复使用已被隐藏的item对象,从而避免重复创建每个item的view对象。

(3)使用ViewHolder优化提高容器中查找组件的效率。

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:Android UI控件ExpandableListView基本用法详解Android改变ExpandableListView的indicator图标实现方法分享Android中ExpandableListView控件使用教程Android中ExpandableListView的用法实例android使用ExpandableListView控件实现小说目录效果的例子Android ExpandableListView展开列表控件使用实例Android ExpandableListView长按事件的完美解决办法Android之IphoneTreeView带组指示器的ExpandableListView效果Android之带group指示器的ExpandableListView(自写)


--结束END--

本文标题: Android 中使用ExpandableListView 实现分组的实例

本文链接: https://www.lsjlt.com/news/23086.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作