作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我是 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()
}
}
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/
我是一名优秀的程序员,十分优秀!