如何计算的GridView所需的高度是Android上滚动型的内部? [英] How do I calculate the required height of a GridView that is inside of a ScrollView on Android?

查看:144
本文介绍了如何计算的GridView所需的高度是Android上滚动型的内部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView一个的LinearLayout内滚动型的内部,在从服务器上的数据页。下面的GridView控件是一个按钮来加载更多的数据。我的GridView将具有最终高度是大于屏幕。如果我把我的GridView的高度,要么WRAP_CONTENT或parent_fill,其大小本身的确切可用的屏幕高度,根本不滚动,裁剪掉多余的行。如果我明确地设置layout_height的东西大,像1000dip,滚动的行为不当,但我不能predict我的滚动视图先验的最终高度。

我如何编程方式确定一个GridView来获得所需的行为进行必要的高度?

下面是我下面的布局。正如你可以看到我设置的高度1000dip,但毕竟是假的,我需要的价值得到自动设置/编程方式:

 <滚动型
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:fillViewport =真
    机器人:layout_weight =1
    >
        < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=垂直
            >            < GridView控件的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                机器人:ID =@ + ID /格
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =1000dip
                机器人:columnWidth时=70dp
                机器人:为numColumns =auto_fit
                机器人:verticalSpacing =0dp
                机器人:horizo​​ntalSpacing =0dp
                机器人:stretchMode =columnWidth时
                机器人:比重=中心
                机器人:背景=#000000
                机器人:layout_weight =1
            />
            <按钮
            机器人:ID =@ + ID / load_more
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=载入更多富
            />
        < / LinearLayout中>
    < /滚动型>


解决方案

显然GridView的内部ScrollViews不在Android的土地犹太教。开关与定制行的ListView。这似乎表现得更好。

I have a GridView inside of a LinearLayout inside of a ScrollView that pages in data from the server. Beneath the GridView is a button to load more data. My GridView will have an ultimate height that is larger than the screen. If I set the height of my GridView to either wrap_content or parent_fill, it sizes itself to the exact available on-screen height and does not scroll at all, cropping out the extra rows. If I explicitly set the layout_height to something large, like 1000dip, scrolling behaves properly, however I cannot predict the final height of my scroll view apriori.

How do I programmatically determine the necessary height of a GridView to get the desired behaviour?

Here is my layout below. As you can see I set the height to 1000dip, but that is bogus, I need that value to get set automatically/programmatically:

        <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"        
    android:layout_weight="1"       
    >   
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"    
            >

            <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
                android:id="@+id/grid"
                android:layout_width="fill_parent" 
                android:layout_height="1000dip"
                android:columnWidth="70dp"
                android:numColumns="auto_fit"
                android:verticalSpacing="0dp"
                android:horizontalSpacing="0dp"
                android:stretchMode="columnWidth"
                android:gravity="center"
                android:background="#000000"
                android:layout_weight="1"
            />
            <Button
            android:id="@+id/load_more"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:text="Load More Foo" 
            />
        </LinearLayout>
    </ScrollView>

解决方案

Apparently GridViews inside ScrollViews are not kosher in Android-land. Switching to ListView with custom-made rows. That seems to behave better.

这篇关于如何计算的GridView所需的高度是Android上滚动型的内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆