如何解决添加动态删除网格项导致 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
}
我的问题如下
- 虽然我的 GridLayout 已设置为 2 列,但仅当项目编号为 2 时我才需要一列和两行,这意味着我的布局中只有 2 个项目。到目前为止,只有在添加第二项时才有效。但是当我删除项目时(比如从 3 个项目到 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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。