From c21534e285b7464be7b8fd748546ff1b2b7ccf49 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Thu, 31 Aug 2017 14:57:28 +0300 Subject: [PATCH] Add drag and drop to markers recycler view --- .../mapmarkers/MapMarkersActiveFragment.java | 22 ++++++++ .../adapters/MapMarkersActiveAdapter.java | 32 ++++++++++-- .../MapMarkersItemTouchHelperCallback.java | 52 +++++++++++++++++++ 3 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java index e8263c6cb9..e6cb7502e1 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersActiveFragment.java @@ -6,6 +6,7 @@ import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.helper.ItemTouchHelper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -21,6 +22,7 @@ import net.osmand.plus.base.MapViewTrackingUtilities; import net.osmand.plus.dashboard.DashLocationFragment; import net.osmand.plus.mapmarkers.adapters.MapMarkersActiveAdapter; import net.osmand.plus.mapmarkers.adapters.MapMarkersActiveAdapter.MapMarkersActiveAdapterListener; +import net.osmand.plus.mapmarkers.adapters.MapMarkersItemTouchHelperCallback; import net.osmand.util.MapUtils; public class MapMarkersActiveFragment extends Fragment implements OsmAndCompassListener, OsmAndLocationListener { @@ -38,7 +40,13 @@ public class MapMarkersActiveFragment extends Fragment implements OsmAndCompassL final MapActivity mapActivity = (MapActivity) getActivity(); adapter = new MapMarkersActiveAdapter(mapActivity); + final ItemTouchHelper touchHelper = new ItemTouchHelper(new MapMarkersItemTouchHelperCallback(adapter)); + touchHelper.attachToRecyclerView(recyclerView); adapter.setAdapterListener(new MapMarkersActiveAdapterListener() { + + private int fromPosition; + private int toPosition; + @Override public void onItemClick(View view) { int pos = recyclerView.indexOfChild(view); @@ -48,6 +56,20 @@ public class MapMarkersActiveFragment extends Fragment implements OsmAndCompassL MapActivity.launchMapActivityMoveToTop(mapActivity); ((DialogFragment) getParentFragment()).dismiss(); } + + @Override + public void onDragStarted(RecyclerView.ViewHolder holder) { + fromPosition = holder.getAdapterPosition(); + touchHelper.startDrag(holder); + } + + @Override + public void onDragEnded(RecyclerView.ViewHolder holder) { + toPosition = holder.getAdapterPosition(); + if (toPosition >= 0 && fromPosition >= 0 && toPosition != fromPosition) { + mapActivity.getMyApplication().getMapMarkersHelper().saveMapMarkers(adapter.getItems(), null); + } + } }); recyclerView.setAdapter(adapter); diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java index c188105548..83014d6132 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java @@ -1,5 +1,6 @@ package net.osmand.plus.mapmarkers.adapters; +import android.support.v4.view.MotionEventCompat; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -13,9 +14,11 @@ import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.dashboard.DashLocationFragment; +import java.util.Collections; import java.util.List; -public class MapMarkersActiveAdapter extends RecyclerView.Adapter { +public class MapMarkersActiveAdapter extends RecyclerView.Adapter + implements MapMarkersItemTouchHelperCallback.ItemTouchHelperAdapter { private MapActivity mapActivity; private List markers; @@ -64,14 +67,17 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter getItems() { + return markers; + } + + @Override + public boolean onItemMove(int from, int to) { + Collections.swap(markers, from, to); + notifyItemMoved(from, to); + return true; + } + + @Override + public void onItemDismiss(RecyclerView.ViewHolder holder) { + listener.onDragEnded(holder); + } + public interface MapMarkersActiveAdapterListener { void onItemClick(View view); + + void onDragStarted(RecyclerView.ViewHolder holder); + + void onDragEnded(RecyclerView.ViewHolder holder); } } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java new file mode 100644 index 0000000000..5c1684a0d3 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java @@ -0,0 +1,52 @@ +package net.osmand.plus.mapmarkers.adapters; + +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.helper.ItemTouchHelper; + +public class MapMarkersItemTouchHelperCallback extends ItemTouchHelper.Callback { + + private final ItemTouchHelperAdapter adapter; + + public MapMarkersItemTouchHelperCallback(ItemTouchHelperAdapter adapter) { + this.adapter = adapter; + } + + @Override + public boolean isLongPressDragEnabled() { + return false; + } + + @Override + public boolean isItemViewSwipeEnabled() { + return false; + } + + @Override + public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; + return makeMovementFlags(dragFlags, 0); + } + + @Override + public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) { + return adapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition()); + } + + @Override + public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) { + + } + + @Override + public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + super.clearView(recyclerView, viewHolder); + adapter.onItemDismiss(viewHolder); + } + + interface ItemTouchHelperAdapter { + + boolean onItemMove(int from, int to); + + void onItemDismiss(RecyclerView.ViewHolder holder); + } +}