android - 可扩展的 GridView 高度不会扩展到内容的高度

标签 android android-gridview android-scrollview expandable row-height

我正在使用这个 ExpandableHeightGridView GridView“应该”延伸到内容高度的类,因为我将它放在 ScrollView 中。如果我的 GridView 项目是完整的,这就可以正常工作,但由于我的 GridView 项目是动态的,它总是没有相同数量的项目,现在问题是如果我有更少的 GridView 项,它的高度没有换行到内容高度,但有一个 blank 行的空间。我不明白为什么它分配那个空白行。

布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondActivity" >

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="@string/hello_world" />

        <com.example.ExpandableHeightGridView
            android:id="@+id/gridview_module"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:horizontalSpacing="20dp"
            android:isScrollContainer="false"
            android:numColumns="3"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" />

        <Button
            android:id="@+id/dial_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:onClick="dialPhone"
            android:text="Dial Phone" />
    </LinearLayout>
</ScrollView>

我分配的 GridView 权重为 1,其他权重为 0。我做对了吗?

最佳答案

尝试在您的 Activity 中添加这个。

ExpandableHeightGridView exGridView;

exGridView = (ExpandableHeightGridView) findViewById(R.id.myId);
exGridView .setExpanded(true);

我根据你的 link .

关于android - 可扩展的 GridView 高度不会扩展到内容的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18910033/

相关文章:

android sqlite 检查是否插入了新值

包含具有 WebView 滚动行为的 LinearLayout 的 Android ScrollView

android - ScrollView 在约束布局内不起作用

android - 关于 ios 和 android push 和 ionic 的说明

java - Android AsyncTask 运行多次

数据集更改时,Android GridView 在后面的索引槽中加载 0 索引项

android - 如何在 Android 中创建电视指南 View

android - GridView 中的 ImageButton 获得点击位置?

android - overScrollBy 并不总是在 Lollipop (5.x) 平台中反弹

android - 任务进入后台或进入前台时回调?