IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android ExpandableListView+ListView开发三级目录 -> 正文阅读

[移动开发]Android ExpandableListView+ListView开发三级目录

1.一、二级目录adapter

package com.example.testing.expand;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.testing.R;

import java.util.List;

/**
 * @author Liushihua
 * @date 2022-4-27 11:52
 * @desc
 */
public class TopExpandAdapter extends BaseExpandableListAdapter {
    private List<ExpandBean> dataList;
    private Context context;
    private onClickChildListener listener;

    public interface onClickChildListener {
        void onClickLevel2(int index1, int index2);

        void onClickLevel3(int index1, int index2, int index3);
    }

    public void setListener(onClickChildListener listener) {
        this.listener = listener;
    }

    private int clickGroupIndex = -1, clickChildIndex = -1;

    public void setClickChild(int clickGroupIndex, int clickChildIndex) {
        if (this.clickChildIndex != clickChildIndex || this.clickGroupIndex != clickGroupIndex) {
            this.clickGroupIndex = clickGroupIndex;
            this.clickChildIndex = clickChildIndex;
        } else {
            this.clickGroupIndex = -1;
            this.clickChildIndex = -1;
        }
        notifyDataSetChanged();
    }

    public TopExpandAdapter(Context context, List<ExpandBean> dataList) {
        this.dataList = dataList;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return dataList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return dataList.get(groupPosition).getSubBeans().size();
    }

    @Override
    public ExpandBean getGroup(int groupPosition) {
        return dataList.get(groupPosition);
    }

    @Override
    public ExpandBean getChild(int groupPosition, int childPosition) {
        return dataList.get(groupPosition).getSubBeans().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_test, parent, false);
        }
        TextView titleT = convertView.findViewById(R.id.titleT);
        titleT.setText(getGroup(groupPosition).getTitle());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand, parent, false);
        }
        ExpandBean bean = getChild(groupPosition, childPosition);
        ListView listView = convertView.findViewById(R.id.listView);
        RelativeLayout titleBox = convertView.findViewById(R.id.titleBox);
        TextView title2 = convertView.findViewById(R.id.title2);
        title2.setText(bean.getTitle());
        titleBox.setOnClickListener(v -> {
            if (listener != null)
                listener.onClickLevel2(groupPosition, childPosition);
            if (bean.getSubBeans() != null && bean.getSubBeans().size() > 0) {
                setClickChild(groupPosition, childPosition);
            }
        });
        ListAdapter listAdapter = new ListAdapter(context,
                R.layout.item_expand_three,
                getChild(groupPosition, childPosition).getSubBeans());
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener((parent1, view, position, id) -> {
            if (listener != null)
                listener.onClickLevel3(groupPosition, childPosition, position);
        });
        int totalHeight = 0;
        for (int k = 0; k < listAdapter.getCount(); k++) {//通过其适配器获取总高度
            View mView = listAdapter.getView(k, null, listView);
            mView.measure(
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            totalHeight += mView.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);//设置ListView的显示高度【外层是SrcollView,或
        listView.setVisibility((groupPosition == clickGroupIndex && childPosition == clickChildIndex) ? View.VISIBLE : View.GONE);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}

2.第三级目录adapter

package com.example.testing.expand;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.testing.R;

import java.util.List;

/**
 * @author Liushihua
 * @date 2022-4-27 11:42
 * @desc
 */
public class ListAdapter extends ArrayAdapter<ExpandBean> {

    public ListAdapter(@NonNull Context context, int resource, @NonNull List<ExpandBean> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertViewIn, @NonNull ViewGroup parent) {
        if (convertViewIn == null) {
            convertViewIn = LayoutInflater.from(getContext()).inflate(R.layout.item_expand_three, parent, false);
        }
        TextView text3 = convertViewIn.findViewById(R.id.text3);
        text3.setText(getItem(position).getTitle());
        return convertViewIn;
    }
}

3.实体类

package com.example.testing.expand;

import java.util.List;

/**
 * @author Liushihua
 * @date 2022-4-26 15:23
 * @desc
 */
public class ExpandBean {
    private String title;
    private List<ExpandBean> subBeans;

    public ExpandBean(String title, List<ExpandBean> subBeans) {
        this.title = title;
        this.subBeans = subBeans;
    }

    public ExpandBean() {
    }

    public ExpandBean(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<ExpandBean> getSubBeans() {
        return subBeans;
    }

    public void setSubBeans(List<ExpandBean> subBeans) {
        this.subBeans = subBeans;
    }
}

4.Activity调用

package com.example.testing;

import android.os.Bundle;
import android.widget.ExpandableListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.testing.expand.ExpandBean;
import com.example.testing.expand.TopExpandAdapter;
import com.example.testing.utils.L;

import java.util.ArrayList;
import java.util.List;

//com.example.test
public class TestActivity extends AppCompatActivity {

    private ExpandableListView listView;
    private List<ExpandBean> dataList;
    private TopExpandAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        initData();
        initView();
    }

    private void initData() {
        dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<ExpandBean> children2 = new ArrayList<>();
            for (int j = 0; j < 3; j++) {
                List<ExpandBean> children3 = new ArrayList<>();
                for (int k = 0; k < 5; k++) {
                    children3.add(new ExpandBean("3级标题" + k));
                }
                children2.add(new ExpandBean("二级标题" + j, children3));
            }
            dataList.add(new ExpandBean("一级标题" + i, children2));
        }
    }

    private int lastClickGroupIndex = -1;

    private void initView() {
        listView = findViewById(R.id.listView);
        adapter = new TopExpandAdapter(this, dataList);
        adapter.setListener(new TopExpandAdapter.onClickChildListener() {
            @Override
            public void onClickLevel2(int index1, int index2) {
                ExpandBean expandBean = dataList.get(index1).getSubBeans().get(index2);
                if (expandBean.getSubBeans() == null || expandBean.getSubBeans().size() == 0) {
                    //一级菜单无子菜单-直接加载二级菜单内容
                    L.d("onClickLevel2:" + expandBean.getTitle());
                }
            }

            @Override
            public void onClickLevel3(int index1, int index2, int index3) {
                ExpandBean expandBean = dataList.get(index1).getSubBeans().get(index2).getSubBeans().get(index3);
                //加载三级菜单内容
                L.d("onClickLevel3:" + expandBean.getTitle());
            }
        });
        listView.setOnGroupClickListener((parent, v, groupPosition, id) -> {
            if (dataList.get(groupPosition).getSubBeans() == null || dataList.get(groupPosition).getSubBeans().size() == 0) {
                //一级菜单无子菜单-直接加载一级菜单内容
            }
            return false;
        });
        listView.setAdapter(adapter);
        listView.setOnGroupExpandListener(groupPosition -> {
            if (lastClickGroupIndex != groupPosition && lastClickGroupIndex != -1) {
                listView.collapseGroup(lastClickGroupIndex);
                adapter.setClickChild(-1, -1);
            }
            lastClickGroupIndex = groupPosition;
        });
        listView.setOnGroupCollapseListener(groupPosition -> {
            if (groupPosition == lastClickGroupIndex)
                adapter.setClickChild(-1, -1);
        });
    }
}

5.资源文件
TestActivity布局

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity">

    <ExpandableListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
<?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:layout_width="350px"
    android:layout_height="match_parent"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/titleBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/titleT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="50px"
            android:textSize="32dp" />

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>
</LinearLayout>

item_expand.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/titleBox"
        android:focusable="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10px">

        <TextView
            android:id="@+id/title2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="100px" />

    </RelativeLayout>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="200px" />
</LinearLayout>

item_expand_three.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="200dp"
        android:textColor="@color/black"
        android:textSize="16sp" />
</LinearLayout>

效果图:
在这里插入图片描述

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-04-30 08:51:08  更:2022-04-30 08:51:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 23:30:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码