我在Kotlin的回收视图中有关于添加和删除项目的问题。第一个任务是添加“添加”按钮,单击它之后,必须一遍又一遍地添加一个特定的图像。当我在主xml中实现该按钮时,该按钮将永远不会显示。第二项任务是单击时删除项目。这是我到目前为止所做的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/recycleview"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add an item"
android:layout_marginStart="280dp"/>
</LinearLayout>
package com.example.lecture11
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private var ItemsList= mutableListOf<ModelItem>()
private lateinit var adapter:MyAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
private fun init(){
adapter= MyAdapter(ItemsList)
recycleview.layoutManager=LinearLayoutManager(this)
recycleview.adapter=adapter
setData()
}
private fun setData(){
ItemsList.add(ModelItem(R.mipmap.messi,"Messi"))
ItemsList.add(ModelItem(R.mipmap.iniesta, "Iniesta"))
ItemsList.add(ModelItem(R.mipmap.henry,"Henry"))
ItemsList.add(ModelItem(R.mipmap.buffon,"Buffon"))
ItemsList.add(ModelItem(R.mipmap.messi,"Messi"))
ItemsList.add(ModelItem(R.mipmap.iniesta, "Iniesta" ))
ItemsList.add(ModelItem(R.mipmap.henry,"Henry"))
ItemsList.add(ModelItem(R.mipmap.buffon,"Buffon"))
}
}
package com.example.lecture11
class ModelItem(var image:Int, var title:String)
package com.example.lecture11
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.items_layout.view.*
class MyAdapter(val ItemsList:MutableList<ModelItem>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
override fun getItemCount(): Int {
return ItemsList.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return
ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.items_layout,parent,false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.onBound()
}
inner class ViewHolder(view :View):RecyclerView.ViewHolder(view){
private lateinit var model:ModelItem
fun onBound(){
model=ItemsList[adapterPosition]
itemView.imageview.setImageResource(model.image)
itemView.title.text=model.title
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:layout_height="wrap_content">
<ImageView
android:scaleType="centerCrop"
android:src="@mipmap/messi"
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/title"
android:textColor="#fff"
android:textStyle="bold"
android:textAlignment="center"
android:text="@string/messi"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="40dp"/>
</LinearLayout>
如果要在屏幕底部显示按钮,并让recyclerview填充其余部分,则必须使用布局权重-
<LinearLayout
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:id="@+id/recycleview"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add an item"
android:layout_marginStart="280dp"/>
</LinearLayout>