想必大家在用电脑上网的时候,都会碰到过,文件的显示方式,一般有列表显示,图标显示,等显示方式。当然,android中也有这种显示方式。今天就来一起实现一下。
首先,说一下,刚开始因为项目需求,要有这个列表和网格来回切换显示的功能,没当回事,想着偷懒网上应该挺多,然后,找了大半天发现寥寥无几,唯独的一篇文章是写这个的,但是被转载了多次,都是相同的。可能原文作者只是提供了一个思路罢了,代码给的相当模糊。以至于被模糊的转载了好多次。打开一个连接发现一模一样,就连bug都一样。没办法,还是自己动手,丰衣足食吧。
先来看看实现后的真机效果图:
1.首先,是activity_main.xml
<?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="com.example.myfiledemo.MainActivity" >
<Button
android:id="@+id/btn_select_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="切换视图"/>
<GridView
android:id="@+id/gridview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:columnWidth="60dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:padding="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
布局也同样简单,一个ListView,一个GridView,还有一个可点击切换的Button.
这里GridView中的android:numColumns=”auto_fit”属性是根据手机屏幕会系统自动去设置显示列数,可适配平板。好了,关于GridView的使用属性,不是本篇博客的重点,想要了解的可自行查阅学习。
-
2.items.xml