如何使网格布局可滚动

How To Make Gridlayout Scrollable

我在 Viewpager 中创建了一个带有 CardView 的 GridLayout。我的布局是线性布局,为了使其可滚动,我将整个布局包裹在 ScrollView 中。但我无法滚动屏幕。当我尝试滚动时没有任何反应。我如何使这些网格项目可滚动。我是 android 的初学者。

我的网格布局XML文件。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_marginTop="150dp"
        app:columnCount="2"
        app:rowCount="4">


        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_height="170dp"
            android:layout_marginLeft="10dp"
            app:cardCornerRadius="6dp"
            android:layout_marginStart="10dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_height="170dp"
            android:layout_marginLeft="18dp"
            android:layout_marginStart="18dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_height="170dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_marginTop="20dp"
            android:layout_height="170dp"
            android:layout_marginLeft="30dp"
            android:layout_marginStart="30dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_height="170dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_marginTop="20dp"
            android:layout_height="170dp"
            android:layout_marginLeft="30dp"
            android:layout_marginStart="30dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />
        </android.support.v7.widget.CardView>

    </android.support.v7.widget.GridLayout>
    </LinearLayout>
</ScrollView>
  1. 创建滚动视图
  2. 在其中创建您的网格布局。
  3. 当您将项目添加到网格布局时,根据列数网格布局将开始滚动。

示例:

<ScrollView

    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="com.groot.rang.MainActivity">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10" >

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight = "1"
        android:columnCount="3"
        android:rowCount="5"
        android:alignmentMode="alignMargins"
        android:columnOrderPreserved="false"
        android:padding="10dp">

        //Your grid items..

    </GridLayout>

    </LinearLayout>
</ScrollView>

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
<android.support.v7.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_marginTop="150dp"
    app:columnCount="2"
    app:rowCount="4">


    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_height="170dp"
        android:layout_marginLeft="10dp"
        app:cardCornerRadius="6dp"
        android:layout_marginStart="10dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_height="170dp"
        android:layout_marginLeft="18dp"
        android:layout_marginStart="18dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_height="170dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_marginTop="20dp"
        android:layout_height="170dp"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />
    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_height="170dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_marginTop="20dp"
        android:layout_height="170dp"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />
    </android.support.v7.widget.CardView>

</android.support.v7.widget.GridLayout>
</LinearLayout>

试试这个: main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

page.xml

<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnCount="2"
            android:rowCount="4">

            <!-- fill your grid -->

        </GridLayout>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
<android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        app:columnCount="2"
        app:rowCount="4">

删除 GridLayout 的静态高度,然后它将正常工作。