Android中利用ListView和GridView实现列表视图和网格(图标)视图的相互切换显示

想必大家在用电脑上网的时候,都会碰到过,文件的显示方式,一般有列表显示,图标显示,等显示方式。当然,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

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值