gpt4 book ai didi

android - 如何在recyclerview中实现拖放和滑动删除?

转载 作者:行者123 更新时间:2023-12-02 12:13:04 26 4
gpt4 key购买 nike

嗨,我是 Kotlin 的新手,我有将近 1 个月的编程时间,我有一个回收器我正在尝试重新订购回收器中的卡片 View ,并通过刷卡删除我不需要的卡片 View ,我该如何实现这个?可以用图书馆来完成吗?我找不到一个

这就是我所拥有的:

适配器

    open class EmployeesAdapter(var context: Context, var employee:ArrayList<EmployeesTest>):BaseAdapter() {

private class ViewHolder(row: View?) {

var txt_user: TextView
var user_job: TextView
var img_user: ImageView
var checBox: CheckBox

init {
this.txt_user = row?.findViewById(R.id.txt_user) as TextView
this.img_user = row.findViewById(R.id.img_user) as ImageView
this.user_job = row.findViewById(R.id.user_job) as TextView
this.checBox = row.findViewById(R.id.checkBox) as CheckBox


}
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

var view: View
var viewHolder: ViewHolder
if (convertView == null) {
var layout = LayoutInflater.from(context)
view = layout.inflate(R.layout.employee_list_item, parent, false)
viewHolder = ViewHolder(view)
view.tag = viewHolder
} else {
view = convertView
viewHolder = view.tag as ViewHolder
}
var employees:EmployeesTest=getItem(position) as EmployeesTest
viewHolder.txt_user.text = employees.nombre
viewHolder.img_user.setImageResource(employees.profilePic)
viewHolder.user_job.text = employees.cargo
viewHolder.checBox.isChecked = false

return view

}

override fun getItem(position: Int): Any {
return employee[position]
}

override fun getItemId(position: Int): Long {
return position.toLong()
}

override fun getCount(): Int {
return employee.count()
}
}

Activity
    class EditEmployeeFragment : Fragment() {
var tv_name: TextView? = null
var rel_main: RelativeLayout? = null


override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var view:View?=inflater!!.inflate(R.layout.edit_employee_fragment,container,false)

var arrReports: ArrayList<EmployeesTest>? = ArrayList()
arrReports?.add(EmployeesTest("Heber","Desarrollador",R.drawable.img_user))
arrReports?.add(EmployeesTest("Mata","Desarrollador",R.drawable.img_user))
arrReports?.add(EmployeesTest("Ernesto","Desarrollador",R.drawable.img_user))
arrReports?.add(EmployeesTest("Toño","Desarrollador",R.drawable.img_user))

var recyclerView:RecyclerView=view?.findViewById(R.id.recycler_edit_employees) as RecyclerView
recyclerView.layoutManager = LinearLayoutManager(this.context, LinearLayout.VERTICAL, false)
recyclerView.adapter = EditEmployeesAdapter( arrReports!!)
return view
}

}

谢谢你的热心帮助

最佳答案

val itemTouchHelperCallback =
object :
ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {

return false
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
noteViewModel.delete(noteAdapter.getNoteAt(viewHolder.adapterPosition))
Toast.makeText(
this@MainActivity,
getString(R.string.note_deleted),
Toast.LENGTH_SHORT
).show()
}

}

val itemTouchHelper = ItemTouchHelper(itemTouchHelperCallback)
itemTouchHelper.attachToRecyclerView(rvNote)

关于android - 如何在recyclerview中实现拖放和滑动删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49827752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com