From 20d49b6d5b0235d1818b900e82a437643f7636e6 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 7 Nov 2020 22:59:46 +0100 Subject: [PATCH] adding an initial implementation of dragging icons --- .../applauncher/adapters/LaunchersAdapter.kt | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/LaunchersAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/LaunchersAdapter.kt index aab59a0..fc765b6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/LaunchersAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/adapters/LaunchersAdapter.kt @@ -1,8 +1,11 @@ package com.simplemobiletools.applauncher.adapters import android.view.Menu +import android.view.MotionEvent import android.view.View import android.view.ViewGroup +import androidx.recyclerview.widget.ItemTouchHelper +import androidx.recyclerview.widget.RecyclerView import com.simplemobiletools.applauncher.R import com.simplemobiletools.applauncher.activities.SimpleActivity import com.simplemobiletools.applauncher.dialogs.EditDialog @@ -14,19 +17,32 @@ import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.beInvisibleIf import com.simplemobiletools.commons.extensions.beVisibleIf +import com.simplemobiletools.commons.interfaces.ItemMoveCallback +import com.simplemobiletools.commons.interfaces.ItemTouchHelperContract import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener +import com.simplemobiletools.commons.interfaces.StartReorderDragListener import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.MyRecyclerView import kotlinx.android.synthetic.main.item_app_launcher.view.* class LaunchersAdapter(activity: SimpleActivity, val launchers: ArrayList, val listener: RefreshRecyclerViewListener?, recyclerView: MyRecyclerView, fastScroller: FastScroller, itemClick: (Any) -> Unit) : - MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) { + MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick), ItemTouchHelperContract { private var isChangingOrder = false + private var startReorderDragListener: StartReorderDragListener init { setupDragListener(true) + + val touchHelper = ItemTouchHelper(ItemMoveCallback(this)) + touchHelper.attachToRecyclerView(recyclerView) + + startReorderDragListener = object : StartReorderDragListener { + override fun requestDrag(viewHolder: RecyclerView.ViewHolder) { + touchHelper.startDrag(viewHolder) + } + } } override fun getActionMenuId() = R.menu.cab @@ -54,7 +70,7 @@ class LaunchersAdapter(activity: SimpleActivity, val launchers: ArrayList - setupView(itemView, launcher, selectedKeys.contains(launcher.packageName.hashCode())) + setupView(itemView, launcher, holder) } bindViewHolder(holder) } @@ -127,17 +143,37 @@ class LaunchersAdapter(activity: SimpleActivity, val launchers: ArrayList + if (event.action == MotionEvent.ACTION_DOWN) { + startReorderDragListener.requestDrag(holder) + } + false + } + } if (isSelected) { launcher_check?.background?.applyColorFilter(primaryColor) } } } + + override fun onRowClear(myViewHolder: ViewHolder?) { + } + + override fun onRowMoved(fromPosition: Int, toPosition: Int) { + } + + override fun onRowSelected(myViewHolder: ViewHolder?) { + } }