如何在添加项目时调整其高度的 GridView [英] How to have a GridView that adapts its height when items are added

查看:17
本文介绍了如何在添加项目时调整其高度的 GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 GridView 中的复选框显示动态增长的字符串列表,GridView 本身在 TableLayout 中.

I am trying to display a dynamically-growing list of strings with a checkbox in a GridView, which is itself in a TableLayout.

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

I can display these "checkboxed" strings fine in a row. My problem occurs when I let the user dynamically add new strings in the GridView.

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

I created a custom adapter that receives the list of strings. Say we have n strings. The adapter returns 'n + 1' for the items count; in getView, it returns:

  • 一个带有 LinearLayout 的视图,它本身有一个 CheckBox 和一个前 n 项的 EditText,
  • 一个带有简单按钮的 LinearLayout,第 'n + 1' 个项目带有一个 '+' 标题.

到目前为止一切顺利.单击+"按钮时,我将一个空字符串添加到字符串列表中,并在适配器中调用 notifyDataSetChanged.

So far so good. When the '+' button is clicked, I add an empty string to the list of strings and call notifyDataSetChanged in the adapter.

GridView 用另外一个项目重新绘制自己.但它保持其原始高度并创建一个垂直滚动条.我希望 GridView 扩展其高度(即在屏幕上占用更多空间并显示所有项目).

The GridView redraws itself with one more item. BUT it keeps its original height and creates a vertical scrollbar. I'd like the GridView to expand its height (i.e. take up more space on screen and show all items).

我尝试将屏幕更改为垂直的 LinearLayout 而不是 TableLayout,但结果是一样的.

I've tried to change the screen to a vertical LinearLayout instead of a TableLayout, but the results is the same.

这是我的屏幕布局:

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

来自适配器的复选框 + 字符串项定义如下:

The checkbox + string item from the adapter is defined like this:

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

+"按钮项的定义如下:

The '+' button item is defined like this:

<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(在屏幕的先前布局中)上也称为无效.没有成功.

I've tried to call invalidate or invalideViews on the GridView after an item is added. Also called invalidate on the TableLayout and TableRow (in the previous layout of the screen). No success.

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

Any idea why the GridView refuses to extend its height ?

(请注意,我完全愿意使用 GridView 以外的其他视图组)

(Note that I am completely open to using an other viewgroup than the GridView)

推荐答案

使用此代码

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

这篇关于如何在添加项目时调整其高度的 GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆