Я пытаюсь отобразить динамически растущий список строк с флажком в GridView, который сам находится в TableLayout.
Я могу показать эти строки "checkboxed" в строке. Моя проблема возникает, когда я позволяю пользователю динамически добавлять новые строки в GridView.
Я создал настраиваемый адаптер, который получает список строк. Скажем, у нас есть n строк. Адаптер возвращает "n + 1" для подсчета элементов; в getView он возвращает:
- a Вид с LinearLayout, сам имеющий CheckBox и EditText для первых n элементов,
- 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>
Я попытался вызвать invalidate или invalideViews в GridView после добавления элемента. Также называется invalidate на TableLayout и TableRow (в предыдущем макете экрана). Нет успеха.
Любая идея, почему GridView отказывается продлить свою высоту?
(Обратите внимание, что я полностью открыт для использования другой группы представлений, чем GridView)