用GridView解决多级目录问题

来源:互联网 发布:非appstore 软件 信任 编辑:程序博客网 时间:2024/06/05 07:31

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>


0 0