首页 > 解决方案 > 无法动态更改 GridView

问题描述

我正在开发一个应用程序。在这个应用程序中,当您单击一个点时会打开一个警报对话框。在这个警告对话框中有两个编辑文本,一个网格视图和一个按钮。gridview 关联了一个使用 Glide 将 Uris 转换为 IconView(扩展 ImageView 的类)的 BaseAdapter。

我想设置 350dp 的最大高度,但如果只有一行(总是有一个图像),我希望 gridview 适应。

我尝试过这种方式:如何让 GridView 在添加项目时适应其高度

但它不起作用。所以我尝试计算元素的数量,如果超过 2 个,则设置 350dp 的高度,如果少于 2 个,则设置 175dp。但它不起作用。

警报对话框布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/puntoName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:fontFamily="sans-serif"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/puntoDescripcion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:maxHeight="100dp"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/puntoName" />


    <GridView
        android:id="@+id/photo_grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:gravity="center"
        android:horizontalSpacing="6dp"
        android:numColumns="2"
        android:verticalSpacing="5dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/puntoDescripcion"></GridView>


    <Button
        android:id="@+id/cerrar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cerrar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/photo_grid" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我用来更改 GridView 高度的代码:

fun checkSize() {
    val popUp: View = fragmentCaller.layoutInflater.inflate(R.layout.popup,null)
    var photogrid: GridView = popUp.findViewById(R.id.photo_grid)
    var params: ViewGroup.LayoutParams = photogrid.layoutParams
    var tam: Int = 175
    if (adapter.getDataSource().size>2) {
        tam = DpToPixels(350)
    }
    photogrid.layoutParams.height = tam
    photogrid.requestLayout()
}

private fun DpToPixels(dp: Int) : Int {
    val escala: Float = fragmentCaller.requireContext().resources.displayMetrics.density;
    var tam: Int = (dp * escala + 0.5f).toInt()
    return tam
}

gridView 的大小始终为 0。不知道为什么。我只想以编程方式设置 android:layout_height 。我在调用 Dialog.show() 之后调用 checkSize() 方法,并且每次我对适配器的元素进行更改时。

谢谢。

标签: androidkotlinandroid-gridview

解决方案


推荐阅读