Android, Horizontal and Vertical Scroll for GridLayout

与世无争的帅哥 提交于 2019-12-05 22:16:18

问题


I'm having trouble getting a GridLayout to scroll horizontally.

I found a similar question Gridlayout + ScrollView. I tried that method, but it didn't work.

It cuts out many tables (because it was supposed to go display all tables from 1 to 20).

Here is the xml file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="16dp" >

            <android.support.v7.widget.GridLayout
                android:id="@+id/table_mapGrid"
                android:layout_width="250dp"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

    <include layout="@layout/cell_list_loading" />

    <TextView
        android:id="@+id/table_errorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="20dp"
        android:text="@string/message_error_connection"
        android:visibility="invisible" />

</FrameLayout>

I want to have dynamic content displayed, varying the number of columns and rows possibly with empty spaces between tables. This I have accomplished, but the problem is when the width of the GridLayout becomes greater than its container's, I wanted to solve that using horizontal scroll, but it doesn't seem to work...

Any suggestion?


回答1:


Well I found the solution

It seems like the android ScrollView works as a VerticalScrollView and only that (the name is not so intuitive as HorizontalScrollView).

So to make something scrollable vertically and horizontally, you need to nest a (Vertical)ScrollView inside a HorizontalScrollView, or the other way around, like this

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

            <!-- Your content here -->

     </HorizontalScrollView>
</ScrollView>



回答2:


The nested HorizontalScrollView / ScrollView will not allow you to scroll both directions at the same time. I had this problem and created a custom component for that, here is the link if it can help anybody :

https://gist.github.com/androidseb/9902093



来源:https://stackoverflow.com/questions/15148175/android-horizontal-and-vertical-scroll-for-gridlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!