diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/TableViewProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/TableViewProxy.java index c3237779057..c23255e67cd 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/TableViewProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/TableViewProxy.java @@ -250,6 +250,20 @@ public void moveItem(int fromAdapterIndex, int toAdapterIndex) toSection.add(toIndex, fromItem); update(); + } + } + + /** + * Fire `move` event upon finalized movement of an item. + * + * @param fromAdapterIndex Index of item in adapter. + */ + public void fireMoveEvent(int fromAdapterIndex) + { + final TiTableView tableView = getTableView(); + + if (tableView != null) { + final TableViewRowProxy fromItem = tableView.getAdapterItem(fromAdapterIndex); fromItem.fireEvent(TiC.EVENT_MOVE, null); } diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/ItemTouchHandler.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/ItemTouchHandler.java index 0c51b2c71aa..14eea675678 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/ItemTouchHandler.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/ItemTouchHandler.java @@ -6,13 +6,17 @@ */ package ti.modules.titanium.ui.widget.listview; +import android.annotation.SuppressLint; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; +import android.view.MotionEvent; import android.view.View; +import android.view.View.OnTouchListener; import androidx.annotation.NonNull; +import androidx.core.util.Pair; import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.RecyclerView; @@ -25,17 +29,42 @@ public class ItemTouchHandler extends ItemTouchHelper.SimpleCallback { private TiRecyclerViewAdapter adapter; private RecyclerViewProxy recyclerViewProxy; + private Pair movePair = null; private Drawable icon; private final ColorDrawable background; - public ItemTouchHandler(TiRecyclerViewAdapter adapter, RecyclerViewProxy recyclerViewProxy) + @SuppressLint("ClickableViewAccessibility") + public ItemTouchHandler(@NonNull TiRecyclerViewAdapter adapter, + @NonNull RecyclerViewProxy recyclerViewProxy, + @NonNull TiNestedRecyclerView recyclerView) { super(0, 0); this.adapter = adapter; this.recyclerViewProxy = recyclerViewProxy; + // Listen for touch events to fire `move` event. + recyclerView.setOnTouchListener(new OnTouchListener() + { + @Override + public boolean onTouch(View v, MotionEvent event) + { + if (event.getAction() == MotionEvent.ACTION_UP) { + + // Only fire `move` event upon final movement location. + + if (movePair != null) { + final int fromIndex = movePair.first; + + recyclerViewProxy.fireMoveEvent(fromIndex); + movePair = null; + } + } + return false; + } + }); + this.icon = this.adapter.getContext().getResources().getDrawable(R.drawable.titanium_icon_delete); this.background = new ColorDrawable(Color.RED); } @@ -202,7 +231,11 @@ public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder fromHolder, @NonNull RecyclerView.ViewHolder toHolder) { - this.recyclerViewProxy.moveItem(fromHolder.getAdapterPosition(), toHolder.getAdapterPosition()); + final int fromIndex = fromHolder.getAdapterPosition(); + final int toIndex = toHolder.getAdapterPosition(); + + movePair = new Pair<>(fromIndex, toIndex); + this.recyclerViewProxy.moveItem(fromIndex, toIndex); return true; } diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/ListViewProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/ListViewProxy.java index df109ebbc67..507646006b5 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/ListViewProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/ListViewProxy.java @@ -178,6 +178,20 @@ public void moveItem(int fromAdapterIndex, int toAdapterIndex) fromSection.deleteItemsAt(fromIndex, 1, null); toSection.insertItemsAt(toIndex, fromItem, null); + } + } + + /** + * Fire `move` event upon finalized movement of an item. + * + * @param fromAdapterIndex Index of item in adapter. + */ + public void fireMoveEvent(int fromAdapterIndex) + { + final TiListView listView = getListView(); + + if (listView != null) { + final ListItemProxy fromItem = listView.getAdapterItem(fromAdapterIndex); fromItem.fireEvent(TiC.EVENT_MOVE, null); } diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/RecyclerViewProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/RecyclerViewProxy.java index f033b63dd15..cc9d01df71e 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/RecyclerViewProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/RecyclerViewProxy.java @@ -15,4 +15,6 @@ public abstract class RecyclerViewProxy extends TiViewProxy public abstract void swipeItem(int index); public abstract void moveItem(int from, int to); + + public abstract void fireMoveEvent(int from); } diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/TiListView.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/TiListView.java index e427747b405..f1447368d5b 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/TiListView.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/listview/TiListView.java @@ -81,7 +81,7 @@ public TiListView(ListViewProxy proxy) this.recyclerView.setAdapter(this.adapter); // Create ItemTouchHelper for swipe-to-delete and move gestures. - final ItemTouchHandler itemTouchHandler = new ItemTouchHandler(this.adapter, this.proxy); + final ItemTouchHandler itemTouchHandler = new ItemTouchHandler(this.adapter, this.proxy, this.recyclerView); final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHandler); itemTouchHelper.attachToRecyclerView(this.recyclerView); diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tableview/TiTableView.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tableview/TiTableView.java index a023300cdfb..9ec33a8090b 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tableview/TiTableView.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tableview/TiTableView.java @@ -84,7 +84,7 @@ public TiTableView(TableViewProxy proxy) this.recyclerView.setAdapter(this.adapter); // Create ItemTouchHelper for swipe-to-delete and move gestures. - final ItemTouchHandler itemTouchHandler = new ItemTouchHandler(this.adapter, this.proxy); + final ItemTouchHandler itemTouchHandler = new ItemTouchHandler(this.adapter, this.proxy, this.recyclerView); final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHandler); itemTouchHelper.attachToRecyclerView(this.recyclerView);