用GridView解决多级目录问题

本文探讨了解决多级目录展示的技术方案,特别是针对大量数据情况下的处理方式。介绍了使用ExpandableListView和TreeView的局限性,并提出了一种结合HorizontalScrollView与GridView的新方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、我在开发中遇到很多次多级目录问题,有两级目录、三级目录、四级目录、五级目录等等。。。

         解决多级目录有一个问题需要跟后台沟通好,他这个结构数据到底是一次性传递给客户端还是分多次,分多次的意思是一你点击某个目录就传递该目录数据给你。这个很重要。如果是两级目录并且数据的量比较少,无论服务器是一次性传数据给你还是分多次传给你,你都可以用ExpandableListView来做,你可以把一级目录数据一次性获取到,在遍历一级目录,拿到说有二级目录数据,这样你就可以展示了,当然如果他一次性把一级和二级目录数据都传递给你就更好,你就不需要遍历请求了。这只能是当数据量比较小的时候建议这样做,这样加载数据虽然耗时,但是效果还是不错的。

         但是往往很多多级目录的数据量比较大,你就不能用ExpandableListView来解决,按照我的经验来说ExpandableListView在本地数据中可以轻松解决三级目录问题,但是在本地数据中到四级目录就很困难了,但是超过三级目录,如果是本地数据可以用TreeView来实现超过三级的目录展示问题。但是开发中很少有可能数据是在本地的,都是从服务器拿来显示的,如果是这样ExpandableListView和TrewView都无法满足需求了,他们准备数据会相当耗时。所以我推荐一个方法,那就是用HorizontalScrollView里面嵌套一个GridView来当做目录头部,下面来一个FrameLayout来加载Fragment显示目录下级数据。

我下面会上传一个图片来方便理解,毕竟文字描述太硬。

以下是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="match_parent"
    android:orientation="vertical">

    <HorizontalScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="18dp"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="horizontal"
            android:padding="3dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/route_name_lv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:gravity="center"
                    android:text="总标题"
                    android:textSize="16sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="5dp"
                    android:src="@mipmap/arrow_right" />
            </LinearLayout>

            <GridView
                android:id="@+id/gridView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />
        </LinearLayout>
    </HorizontalScrollView>

    <View style="@style/levelLine" />

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
上面的XML里面的levelLine是一个普通的横线,在res/values/style.xml里面写的:

 <!--横向线条-->
    <style name="levelLine">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">0.3dp</item>
        <item name="android:background">#ccc</item>
    </style>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值