我有一个回收器视图应用程序,它从用户那里获取数据,并将其设置在回收器视图上,我希望当用户点击该视图时,会打开一个警报对话框,它会问他是否应该删除。如果是,视图将被删除。有谁能帮我实现这一点吗?
这是我的主要活动代码
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var todolist= mutableListOf<Todo>()
// Todo("c",false),
/* Todo("java",true),
Todo("python",false),
Todo("c#",false),
Todo("c++",true),
Todo("json",false),
Todo("web dev",true),
Todo("kotlin",true)*/
val adapter=TodoAdapter(todolist)
rvTodos.adapter=adapter
rvTodos.layoutManager=LinearLayoutManager(this)
btnAddTodo.setOnClickListener{
val title=etTodo.text.toString()
if(title==""){
val builder = AlertDialog.Builder(this)
builder.setTitle("Alart")
builder.setMessage("Title can't be empty")
builder.setPositiveButton("OK") { dialog, which ->
// Handle positive button click
}
builder.setNegativeButton("Cancel") { dialog, which ->
Toast.makeText(this,"set title first",Toast.LENGTH_LONG).show()
}
val alertDialog = builder.create()
alertDialog.show()
}
else{
val todo=Todo(title,false)
todolist.add(todo)
adapter.notifyItemInserted(todolist.size-1)
}
}
}
}
下面是适配器类
class TodoAdapter(val todos: List<Todo>) : RecyclerView.Adapter<TodoAdapter.TodoViweholder>(){
inner class TodoViweholder(itemView: View):RecyclerView.ViewHolder(itemView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViweholder {
val view=LayoutInflater.from(parent.context).inflate(R.layout.item_todo,parent,false)
return TodoViweholder(view)
}
override fun onBindViewHolder(holder: TodoViweholder, position: Int) {
holder.itemView.apply {
tvTitle.text=todos[position].title
cbDone.isChecked=todos[position].isChecked
}
}
override fun getItemCount(): Int {
return todos.size
}
}
回收器结构的XML
<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:padding="16dp"
android:background="@color/black"
android:id="@+id/structure"
android:layout_marginBottom="5dp"
android:layout_height="100dp">
<TextView
android:id="@+id/tvTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="@color/white"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cbDone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/cbDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是主要的活动布局
<?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"
android:padding="16dp"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvTodos"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/etTodo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnAddTodo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:text="New Todo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<EditText
android:id="@+id/etTodo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="set title"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnAddTodo"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
1条答案
按热度按时间7lrncoxx1#
我在这里假设你需要在回收器视图的每个视图上显示弹出窗口,并决定是否需要删除该视图。
为了实现这一点,你可以在
TodoAdapter#onBindViewHolder()
类中为每个ViewHolder项添加一个click listener,如下所示: