我查看了很多有关RecyclerView滑动功能的教程。当我附加ItemTouchHelper时,它对ViewHolder无效。
我在onSwiped方法中放置了一个断点,但是当我尝试滑动时它甚至没有到达那里。我创建了一个新项目,并使用一些视图持有者实施了一个随机的recyclerview。我附加了ItemTouchHelper,但遇到了同样的问题。
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val model = ViewModelProviders.of(this)[ViewModel::class.java]
val dues = model.dues
val adapter = Adapter<Due>(R.layout.card_view_dues, fragmentManager!!)
dues.observe(this,
Observer<MutableList<Due>> {
adapter.list = it
}
)
val itemTouchHelper = object: SimpleCallback(0, LEFT and RIGHT) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
) = false
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
dues.value?.removeAt(viewHolder.adapterPosition) //set breakpoint here
adapter.notifyDataSetChanged()
}
}
ItemTouchHelper(itemTouchHelper).attachToRecyclerView(recyclerView)
recyclerView.apply {
layoutManager = LinearLayoutManager(activity!!.applicationContext)
setHasFixedSize(false)
this.adapter = adapter
}
}
我也替换了:
ItemTouchHelper(itemTouchHelper).attachToRecyclerView(recyclerView)
使用
ItemTouchHelper(itemTouchHelper).attachToRecyclerView(activity!!.findViewById<RecyclerView>(R.id.recyclerView))
但是它似乎仍然不起作用。可能出了什么问题?
答案 0 :(得分:0)
您为SimpleCallback提供了错误的参数
val itemTouchHelper = object: SimpleCallback(0, LEFT and RIGHT) {
相反,您应该
val itemTouchHelper = object: SimpleCallback(0, LEFT or RIGHT) {