如何使GridLayout可滚动

3

我已经创建了一个包含CardView的GridLayout,并将其放置在ViewPager中。我的布局是线性布局,为了使它可滚动,我将整个布局包装在了ScrollView中。但我无法滚动屏幕。当我尝试滚动时,什么也不会发生。我该如何使这些网格项可滚动?我是一个Android的初学者。

我的GridLayout 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>

测试用例使您的滚动视图 android:fillViewport="true" - AskNilesh
最好使用附加到RecyclerView的GridLayout管理器。 - Pallavi Tapkir
4个回答

3

试试这个:

<?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>


1
尝试这个: 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>

1
<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的静态高度,它就能正常工作了。

-2
  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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接