![](/img/trans.png)
[英]how to increase the scrollview height with gridview inside in android
[英]How do I calculate the required height of a GridView that is inside of a ScrollView on Android?
我在ScrollView內部的LinearLayout內部有一個GridView,它可以從服務器中分頁。 GridView下面是一個加載更多數據的按鈕。 我的GridView的最終高度將大於屏幕。 如果我將GridView的高度設置為wrap_content或parent_fill,它將自身調整為可用的屏幕高度,並且根本不滾動,裁剪出額外的行。 如果我明確地將layout_height設置為大的,如1000dip,滾動行為正常,但我無法預測滾動視圖apriori的最終高度。
如何以編程方式確定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>
如果有人需要,這是一種方法。 有點黑客,但做的伎倆。 你必須將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() ) );
}
});
我知道這是一個舊案例,但我有一個類似的問題,我的ScrollView
包含多個LinearLayout
,它們又包含一個標題和一個GridView
。 基本上我使用包含屬於該類別的圖像的標題進行了分類。 GridView必須具有靈活的高度。
我找到了很多關於覆蓋onMeasure()的答案,但它只適用於某些設備,而不是全部。 高度最終將為1或3或僅為0,僅顯示圖像的幾個像素。
StretchingGridView類
我使用此代碼覆蓋了drawableStateChanged()
方法,受@ Karitsa解決方案的啟發:
@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使用方形圖像,因此我根據寬度確定高度。 我認為它不適用於靈活的網格物品高度。
顯然,ScrollViews中的GridViews在Android版本中並不是猶太人。 使用自定義行切換到ListView。 這似乎表現得更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.