【问题标题】:How To Make Gridlayout Scrollable如何使 Gridlayout 可滚动
【发布时间】:2019-02-14 12:14:42
【问题描述】:

我在 Viewpager 中创建了一个带有 CardView 的 GridLayout。我的布局是线性布局,为了使其可滚动,我将整个布局包裹在一个 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"
  • 最好使用带有 GridLayout 管理器的 RecyclerView。

标签: android android-layout android-gridlayout


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:
    <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 的静态高度,就可以正常工作了。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:
        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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-08-13
          • 2014-12-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-12
          • 1970-01-01
          相关资源
          最近更新 更多