Reputation: 1038
I have looked up many tutorials on the swipe functionality to the RecyclerView. When I attach the ItemTouchHelper, it has no effect on the ViewHolder.
I had put a put a breakpoint in onSwiped method but it does not even reach there when I am trying to swipe. I made a new project and just implemented a random recyclerview with some view holders. I attached the ItemTouchHelper and I had the same problem.
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
}
}
I have also replaced:
ItemTouchHelper(itemTouchHelper).attachToRecyclerView(recyclerView)
with
ItemTouchHelper(itemTouchHelper).attachToRecyclerView(activity!!.findViewById<RecyclerView>(R.id.recyclerView))
but it still doesn't seem to work. What has possibly gone wrong?
Upvotes: 0
Views: 409
Reputation: 61
You are supplying the wrong argument to SimpleCallback
val itemTouchHelper = object: SimpleCallback(0, LEFT and RIGHT) {
instead you should
val itemTouchHelper = object: SimpleCallback(0, LEFT or RIGHT) {
Upvotes: 1