Android 添加动态删除网格项原因columnCount必须大于或等于所有网格索引的最大值
我想在网格布局中动态添加/删除项目。所以我的xml看起来像这样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 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
}
我的问题如下计数器==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
}