添加动态删除网格项导致 columnCount 必须大于或等于所有网格索引的最大值

问题描述

我想在我的网格布局中动态添加/删除项目。所以我的 xml 看起来像这样

<?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"
    tools:context=".GridLayoutActivity">

    <Button
        android:id="@+id/addBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:layout_margin="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/removeBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Remove"
        android:layout_margin="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:columnCount="2"
        android:useDefaultMargins="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/addBtn"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的 grid_item xml

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello from me"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的Activity中的代码如下

private var counter = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityGridLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.addBtn.setonClickListener {
            counter++
            addView()
        }

        binding.removeBtn.setonClickListener {
            if (counter < 0) return@setonClickListener
            removeView()
        }
    }

    fun addView() {
        val gridItem = this.layoutInflater.inflate(R.layout.grid_item,binding.gridLayout,false)
        gridItem.id = View.generateViewId()
        val params = GridLayout.LayoutParams(
            GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f),GridLayout.spec(GridLayout.UNDEFINED,1f)
        ).also {
            it.width = 0
            it.height = 0
        }
        binding.gridLayout.columnCount = if (counter == 2) 1 else 2
        gridItem.layoutParams = params

        val text = (gridItem as ViewGroup).getChildAt(0) as TextView
        text.text = "Text ${System.currentTimeMillis()}"

        binding.gridLayout.addView(gridItem)
    }

    fun removeView() {
        binding.gridLayout.removeViewAt(counter - 1)
        counter--
        binding.gridLayout.columnCount = if (counter == 2) 1 else 2
    }

我的问题如下

  1. 虽然我的 GridLayout 已设置为 2 列,但仅当项目编号为 2 时我才需要一列和两行,这意味着我的布局中只有 2 个项目。到目前为止,只有在添加第二项时才有效。但是当我删除项目时(比如从 3 个项目到 2 个项目,我会崩溃)
  2. 我希望新行中单独存在的每个新项目的宽度都类似于“match_parent”

我怎样才能实现我想要的?

解决方法

这是一个技巧。

counter == 3时,强制删除2个视图,然后添加一个。所以 counter 为 2。

此代码实现了您的目标。

fun removeView() {
    binding.gridLayout.removeViewAt(counter - 1)
    if (counter == 3) {
        binding.gridLayout.removeViewAt(counter - 2)
        counter--
        addView()
        counter++
    }
    counter--
}
,

找到解决方案。我们只需要改变GridLayout的方向

fun addView() {
        val gridItem = this.layoutInflater.inflate(R.layout.grid_item,binding.gridLayout,false)
        gridItem.id = View.generateViewId()
        val params = GridLayout.LayoutParams(
            GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f),GridLayout.spec(GridLayout.UNDEFINED,1f)
        ).also {
            it.width = 0
            it.height = 0
        }
        if (counter > 2)
            binding.gridLayout.orientation = GridLayout.HORIZONTAL

        binding.gridLayout.columnCount = if (counter > 2) 2 else 1
        gridItem.layoutParams = params

        val text = (gridItem as ViewGroup).getChildAt(0) as TextView
        text.text = "Text ${System.currentTimeMillis()}"

        binding.gridLayout.addView(gridItem)
    }

    fun removeView() {
        counter--
        binding.gridLayout.removeViewAt(counter)
        if (counter < 3)
            binding.gridLayout.orientation = GridLayout.VERTICAL

    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...