@@ -23,13 +23,15 @@ class SortView<ITEM> @JvmOverloads constructor(
2323
2424 var editor: ItemListEditor <ITEM >? = null
2525 private var onSortListener: OnSortListener ? = null
26+ private var itemTouchHelper: ItemTouchHelper ? = null
2627
2728 fun setupAdapter (adapter : (Slush .SingleType <ITEM >) -> Slush .SingleType <ITEM >) = apply {
2829 editor = adapter(Slush .SingleType ()).into(this ).itemListEditor
2930 }
3031
3132 fun sortDirection (dragDir : Int ) = apply {
32- ItemTouchHelper (object : ItemTouchHelper .SimpleCallback (dragDir, 0 ) {
33+ itemTouchHelper?.attachToRecyclerView(null )
34+ itemTouchHelper = ItemTouchHelper (object : ItemTouchHelper .SimpleCallback (dragDir, 0 ) {
3335 override fun onMove (
3436 recyclerView : RecyclerView ,
3537 start : ViewHolder ,
@@ -42,7 +44,8 @@ class SortView<ITEM> @JvmOverloads constructor(
4244
4345 override fun onSwiped (viewHolder : ViewHolder , direction : Int ) {}
4446
45- }).attachToRecyclerView(this )
47+ })
48+ itemTouchHelper?.attachToRecyclerView(this )
4649 }
4750
4851 fun onSort (listener : (startPosition: Int , target: Int ) -> Unit ) = onSort(
@@ -62,8 +65,10 @@ class SortView<ITEM> @JvmOverloads constructor(
6265 fun onSort (listener : OnSortListener ) = apply {
6366 onSortListener = listener
6467
65- val isHorizontal = (layoutManager as LinearLayoutManager ).orientation == LinearLayoutManager .HORIZONTAL
66- sortDirection( if (isHorizontal) DRAG_HORIZONTAL else DRAG_VERTICAL )
68+ if (itemTouchHelper == null ) {
69+ val isHorizontal = (layoutManager as LinearLayoutManager ).orientation == LinearLayoutManager .HORIZONTAL
70+ sortDirection( if (isHorizontal) DRAG_HORIZONTAL else DRAG_VERTICAL )
71+ }
6772 }
6873
6974 fun equalSpacing (showFirstDivider : Boolean = false, showLastDivider : Boolean = false) = apply {
0 commit comments