2011-05-14 46 views
23

我正嘗試使用GridView中的複選框顯示動態增長的字符串列表,該列表本身位於TableLayout中。如何在添加項目時使GridView適應其高度

我可以在一行中顯示這些「複選框」字符串。我讓用戶在GridView中動態添加新字符串時發生問題。

我創建了一個接收字符串列表的自定義適配器。假設我們有n個字符串。適配器返回'n + 1'表示物品計數;在getView,它返回:

  • 用的LinearLayout,本身具有CheckBox和用於第一n項一個EditText視圖,
  • 用一個簡單的按鈕的LinearLayout,具有用於一個「+」字幕的'第n + 1項。

到目前爲止好。當單擊「+」按鈕時,我將一個空字符串添加到字符串列表中,並在適配器中調用notifyDataSetChanged。

GridView重繪自己與一個更多的項目。但它保持原來的高度,並創建一個垂直滾動條。我希望GridView擴展其高度(即在屏幕上佔用更多空間並顯示所有項目)。

我試圖將屏幕更改爲垂直LinearLayout而不是TableLayout,但結果是相同的。

這裏是我的屏幕布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="none"> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 

    <!-- other lines omitted --> 

    <LinearLayout android:layout_width="fill_parent" 
        android:layout_height="wrap_content" > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/wine_rack_explanation" 
      android:layout_gravity="center_vertical" /> 
      <GridView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/gvRack" 
      android:columnWidth="90dp"  
      android:numColumns="auto_fit" /> 
    </LinearLayout> 

<!-- other lines omitted --> 

</LinearLayout> 
</ScrollView> 

從適配器的複選框+字符串項的定義如下:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <CheckBox android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/cbCoordinate" 
       android:checked="true" 
       /> 
    <EditText android:id="@+id/txtCoordinate" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
</LinearLayout> 

的「+」按鈕項被這樣定義:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <Button android:id="@+id/btnAddBottle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/add_bottle_caption" /> 
</LinearLayout> 

我試圖在添加項目後在GridView上調用invalidate或invalideViews。在TableLayout和TableRow上也被稱爲invalidate(在屏幕的前一個佈局中)。沒有成功。

任何想法爲什麼GridView拒絕延長其高度?

(請注意,我用的其他的ViewGroup比GridView的完全開放)

+0

我有幾乎完全相同的問題 - 有誰知道如何做到這一點? – 2011-08-17 10:00:19

+0

我認爲它可以幫助他人http://stackoverflow.com/a/23802813/5887689 – 2016-05-29 08:09:59

+1

也許這個答案可以幫助你https://stackoverflow.com/questions/21482989/how-to-assign-wrap-content-as -height-dynamic-loaded-gridview/46350213#46350213 – 2017-09-21 18:01:19

回答

4

當onMeasure()被調用的GridView,如果heightMode是MeasureSpec.UNSPECIFIED,然後在GridView將是一樣高的所有的它包含元素(加上一些填充)。

爲了確保這樣的話,你可以快速自定義視圖擴展GridView和包括以下重寫:

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, MeasureSpec.UNSPECIFIED); 
    } 
+2

謝謝。當單元格的內容被動態改變時,你嘗試過嗎?我已經重寫了我的代碼以擺脫GridView,因此無法測試您的答案。 – Timores 2012-01-16 23:02:43

+0

不幸的是,不能動態創建'GridView'。 – skywall 2014-08-19 12:31:16

1

格雷厄姆的解決方案並沒有爲我工作。然而,這一個(多一點黑客)做的事:https://stackoverflow.com/a/8483078/479478

+1

小心分享需要黑客入侵的工具嗎? – 2014-01-13 15:18:15

4

這是爲我工作:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/background" > 

     <!-- MORE LAYOUTS IF YOU WANT --> 

    <ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:fillViewport="true" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

       <!-- MORE LAYOUTS IF YOU WANT --> 

     <GridView 
      android:id="@+id/gridview" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp" 
      android:layout_marginTop="5dp" 
      android:gravity="center" 
      android:horizontalSpacing="10dp" 
      android:numColumns="auto_fit" 
      android:stretchMode="columnWidth" 
      android:verticalSpacing="10dp" > 
     </GridView> 

      <!-- MORE LAYOUTS IF YOU WANT --> 

    </RelativeLayout> 

    </ScrollView> 

</RelativeLayout> 
+3

真的這個解決方案爲你工作? – Umesh 2014-06-26 13:59:48

+1

這將適用於高度,但不適用於分頁。你不能在scrollview中使用'android:fillViewport =「true」'來設置分頁。 – 2016-07-29 12:20:24

46

使用此代碼

public class MyGridView extends GridView { 

    public MyGridView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MyGridView(Context context) { 
     super(context); 
    } 

    public MyGridView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, 
       MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, expandSpec); 
    } 
} 
+4

這個效果很好,但是當我添加很多項時,我現在在GridView底部找到一個大的空白空間。我無法給出這個空間,因爲我查看了開發者選項的顯示佈局輪廓特徵。請幫忙。 – 2015-02-27 12:03:45

+0

工作得很好..好工作!!! – 2016-11-16 23:47:03

+0

如果您的物品有不同高度,則此解決方案可能無法正常工作。對於高度不變的物品,這是完美的。 – 2016-12-04 16:48:45

7

我已經做了這種方式:

此代碼將設置GridView高度

注:其中5是列數。

private void setDynamicHeight(GridView gridView) { 
     ListAdapter gridViewAdapter = gridView.getAdapter(); 
     if (gridViewAdapter == null) { 
      // pre-condition 
      return; 
     } 

     int totalHeight = 0; 
     int items = gridViewAdapter.getCount(); 
     int rows = 0; 

     View listItem = gridViewAdapter.getView(0, null, gridView); 
     listItem.measure(0, 0); 
     totalHeight = listItem.getMeasuredHeight(); 

     float x = 1; 
     if(items > 5){ 
      x = items/5; 
      rows = (int) (x + 1); 
      totalHeight *= rows; 
     } 

     ViewGroup.LayoutParams params = gridView.getLayoutParams(); 
     params.height = totalHeight; 
     gridView.setLayoutParams(params); 
    } 

希望這會幫助你。

+1

節省很多時間,謝謝:) – 2016-12-29 13:00:12

+0

什麼是ListAdapter? – dev1998 2017-02-05 16:58:23

+0

@ dev1998默認的Android小部件組件。 Listadapter是一個在https://developer.android.com/reference/android/widget/ListAdapter.html中定義的接口。 – 2017-03-01 11:06:58

相關問題