Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 添加动态删除网格项原因columnCount必须大于或等于所有网格索引的最大值_Android_Kotlin_Android View_Layout Inflater_Android Gridlayout - Fatal编程技术网

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

Android 添加动态删除网格项原因columnCount必须大于或等于所有网格索引的最大值,android,kotlin,android-view,layout-inflater,android-gridlayout,Android,Kotlin,Android View,Layout Inflater,Android Gridlayout,我想在网格布局中动态添加/删除项目。所以我的xml看起来像这样 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://sch

我想在网格布局中动态添加/删除项目。所以我的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>

我的网格项目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>

我的活动中的代码如下

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, GridLayout.FILL, 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
    }
专用变量计数器=0
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
绑定=活动GridLayoutBinding.充气(LayoutFlater)
setContentView(binding.root)
binding.addBtn.setOnClickListener{
柜台++
addView()
}
binding.removeBtn.setOnClickListener{
如果(计数器<0)return@setOnClickListener
removeView()
}
}
fun addView(){
val gridItem=此.layoutInflater.充气(R.layout.grid_项,binding.gridLayout,false)
gridItem.id=View.generateViewId()
val params=GridLayout.LayoutParams(
GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f),
GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f)
).还有{
it.width=0
it.height=0
}
binding.gridLayout.columnCount=if(计数器==2)1 else 2
gridItem.layoutParams=参数
val text=(gridItem作为ViewGroup)。getChildAt(0)作为TextView
text.text=“text${System.currentTimeMillis()}”
binding.gridLayout.addView(gridItem)
}
乐趣移除视图(){
binding.gridLayout.removeViewAt(计数器-1)
柜台--
binding.gridLayout.columnCount=if(计数器==2)1 else 2
}
我的问题如下

  • 尽管我的GridLayout已设置为有2列,但只有当项目编号为2时,我才需要一列和两行,这意味着我的布局中只有2个项目。到目前为止,只有在添加第二项时才有效。但是当我删除项目时(比如从3个项目到2个项目,我会崩溃)
  • 我希望每一个单独在新行中的新项目都具有“匹配父项”这样的宽度
  • 我怎样才能达到我想要的呢?

    这是一个技巧

    计数器==3
    时,请删除2个视图,然后添加一个。因此,
    计数器
    为2

    这段代码实现了您的目标

    fun removeView(){
    binding.gridLayout.removeViewAt(计数器-1)
    如果(计数器==3){
    binding.gridLayout.removeViewAt(计数器-2)
    柜台--
    addView()
    柜台++
    }
    柜台--
    }
    
    找到解决方案。我们只需要改变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, GridLayout.FILL, 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
    
        }
    
    fun addView(){
    val gridItem=此.layoutInflater.充气(R.layout.grid_项,binding.gridLayout,false)
    gridItem.id=View.generateViewId()
    val params=GridLayout.LayoutParams(
    GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f),
    GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f)
    ).还有{
    it.width=0
    it.height=0
    }
    如果(计数器>2)
    binding.gridLayout.orientation=gridLayout.HORIZONTAL
    binding.gridLayout.columnCount=if(计数器>2)2 else 1
    gridItem.layoutParams=参数
    val text=(gridItem作为ViewGroup)。getChildAt(0)作为TextView
    text.text=“text${System.currentTimeMillis()}”
    binding.gridLayout.addView(gridItem)
    }
    乐趣移除视图(){
    柜台--
    binding.gridLayout.removeViewAt(计数器)
    如果(计数器<3)
    binding.gridLayout.orientation=gridLayout.VERTICAL
    }