android - 添加项目时如何让 GridView 适应其高度

标签 android gridview

我正在尝试在 GridView 中显示一个动态增长的字符串列表,其中包含一个复选框,它本身位于 TableLayout 中。

我可以连续显示这些“复选框”字符串。当我让用户在 GridView 中动态添加新字符串时,就会出现我的问题。

我创建了一个接收字符串列表的自定义适配器。假设我们有 n 个字符串。适配器为项目计数返回“n + 1”;在 getView 中,它返回:

  • 一个带有 LinearLayout 的 View,它本身有一个 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 上也称为无效(在屏幕的先前布局中)。没有成功。

知道为什么 GridView 拒绝扩展它的高度吗?

(请注意,我完全愿意使用除 GridView 之外的其他 View 组)

最佳答案

使用此代码

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);
    }
}

关于android - 添加项目时如何让 GridView 适应其高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6005245/

相关文章:

Android:使用终端连接到 Wifi

php - 如何在yii2中实现单一搜索表单

java - getView() 方法在带有 gridview 的自定义适配器中未调用

android - 错误垂直视口(viewport)被赋予无限高度

Android Parse 没有收到推送通知

android - Kindle Fire 和 disable_keyguard 权限

android - 在 Android 上将文件移动到 SD 卡

asp.net - 绑定(bind)字段样式

Android - Gridview 图像在滚动时混合