4

サーバーからのデータをページングするScrollView内のLinearLayout内にGridViewがあります。GridViewの下には、より多くのデータをロードするためのボタンがあります。私のGridViewの最終的な高さは、画面よりも大きくなります。GridViewの高さをwrap_contentまたはparent_fillのいずれかに設定すると、画面上で使用可能な正確な高さにサイズ変更され、まったくスクロールせず、余分な行が切り取られます。layout_heightを1000dipなどの大きな値に明示的に設定すると、スクロールは適切に動作しますが、スクロールビューの最終的な高さを事前に予測することはできません。

目的の動作を実現するために必要なGridViewの高さをプログラムで決定するにはどうすればよいですか?

これが私のレイアウトです。ご覧のとおり、高さを1000dipに設定しましたが、それは偽物です。自動的に/プログラムで設定するには、その値が必要です。

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

3 に答える 3

15

誰かがそれを必要とする場合、これを行う1つの方法があります。少しハックしますが、トリックを行います。GridViewは、最初はすべてのビューに対して十分な大きさに設定する必要があります(例:10000dip)

final GridView imageContainer = // your GridView
imageContainer.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() 
            {
            @Override
            public void onGlobalLayout() 
                {
                imageContainer.getViewTreeObserver().removeGlobalOnLayoutListener( this );
                View lastChild = imageContainer.getChildAt( imageContainer.getChildCount() - 1 );
                imageContainer.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, lastChild.getBottom() ) );
                }
            });
于 2011-11-24T11:27:32.887 に答える
2

私はそれが古いケースであることを知っていますが、私ScrollViewが複数LinearLayoutのsを含み、それらが順番にヘッダーと。を含むという同様の問題がありましたGridView。基本的に、そのカテゴリに属する​​画像を含むヘッダーで分類されたセクションを作成しました。GridViewの高さは柔軟である必要がありました。

onMeasure()のオーバーライドについて多くの答えが見つかりましたが、すべてではなく一部のデバイスでしか機能しませんでした。高さは最終的に1、3、または0になり、画像の数ピクセルのみが表示されます。

StretchingGridViewクラス

@KaritsadrawableStateChanged()のソリューションに触発されて、このコードでメソッドをオーバーライドしました。

@Override
public void drawableStateChanged() {
    getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            getViewTreeObserver().removeOnGlobalLayoutListener( this );
            View lastChild = getChildAt( getChildCount() - 1 );
            if (lastChild != null) {
                int height = Math.max(lastChild.getBottom(), getColumnWidth());
                float child = getAdapter().getCount();
                float col = getNumColumns();
                int rows = (int) Math.ceil(child / col);
                height = rows * getColumnWidth() + (getHorizontalSpacing() * rows-1);
                setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, height ) );
            }
        }
    });
}

注:私のGridViewは正方形の画像を使用しているため、高さは画像の幅に基づいています。グリッドアイテムの高さが柔軟な場合はうまく機能しないと思います。

于 2013-10-31T10:25:03.560 に答える
1

どうやら、ScrollViews内のGridViewsはAndroidランドではコーシャではありません。カスタムメイドの行を使用してListViewに切り替えます。それはより良い振る舞いをするようです。

于 2010-09-11T02:03:51.597 に答える