在实际开发中,我们很可能会遇到将许多功能已小图标的形式来展示给用户,因此需要将功能罗列出来。但是,如果只是单纯的网格布局的话,可能会导致占领过多的屏幕,影响到其他数据的展示。因此,我们应该做成 滑动网格的效果。这里我们以美团为例,如下图。
美团的的中间多个小功能区可以横向滑动,所以我们考虑RecyclerView来实现这个效果。同时,内部数据已网格展现,因此我们可以考虑是不是能把GridLayout放入RecyclerView中实现效果。接下来我们就开始尝试一下。
首先,我们在主活动的布局文件中,添加一个RecyclerView控件,并且设置他的属性。
<?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=".MainActivity"
android:orientation="vertical">
......
<!--使用recycler view 实现滑动网格-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"