Подтвердить что ты не робот

Отображение Gridview в соответствии с фактической высотой в прокрутке

 <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/titleBarBG"
        android:layout_alignParentLeft="true" >

    <RelativeLayout
        android:id="@+id/scrollContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         >

    <GridView
        android:id="@+id/issueList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/archiveTitle"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/customshape"
        android:numColumns="3"
        android:overScrollMode="never"
        android:scrollbars="none" >
    </GridView>

</RelativeLayout>
 </ScrollView>

Я хотел бы создать gridview, который будет действовать как таблица. Например, размер сетки будет увеличиваться, что сделает gridview выше. Вместо того, чтобы скрывать дополнительный контент, мне бы хотелось, чтобы вид сетки показывал весь контент и расширял высоту, когда есть дополнительный контент

Как это реализовать? спасибо

4b9b3361

Ответ 1

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

Ответ 2

Это немного очищенная версия: Сетка изображений внутри ScrollView

WrappedGridView.java:

/**
 * Use this class when you want a gridview that doesn't scroll and automatically
 * wraps to the height of its contents
 */
public class WrappedGridView extends GridView {
    public WrappedGridView(Context context) {
        super(context);
    }

    public WrappedGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WrappedGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

Включите в XML-макет, как и GridLayout. Используйте адаптер для представления его.

Насколько я могу судить, это самое простое решение, доступное сейчас. В структуре, которая обрабатывает упаковку, нет другого представления. Было бы неплохо, если бы кто-то предоставил элегантный, автоматически подгоняемый стол. Модификация GridView.java для этой цели может быть не плохой идеей.

В качестве альтернативы вы можете найти один из проектов FlowLayout. Существует android-flowlayout и FlowLayout. Они немного более гибкие, чем простая сетка, и, я полагаю, немного менее эффективны. Вам также не нужно будет предоставлять им адаптер.