如何有一个GridView,当项目补充说,调整其高度 [英] How to have a GridView that adapts its height when items are added

查看:94
本文介绍了如何有一个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.

我可以显示这些checkboxed弦细成一排。当我让用户动态地在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和一个EditText的前n项的视图,
  • 用一个简单的按钮的LinearLayout,用+的标题为N + 1'th项目。

到目前为止好。当点击+按钮,我想补充一个空字符串字符串列表,并调用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>

我试着打电话给项目添加后无效或invalideViews在GridView控件。也叫无效的TableLayout和的TableRow(在屏幕的previous布局)。没有成功。

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 ?

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

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

推荐答案

使用这种code

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天全站免登陆