2011-05-14 47 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

相关问题