diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index c1fe3d0ed2..907779eb4b 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -63,12 +63,12 @@ import net.osmand.plus.measurementtool.SelectedPointBottomSheetDialogFragment.Se import net.osmand.plus.measurementtool.SnapToRoadBottomSheetDialogFragment.SnapToRoadFragmentListener; import net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter; import net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter.MeasurementAdapterListener; -import net.osmand.plus.measurementtool.adapter.MeasurementToolItemTouchHelperCallback; import net.osmand.plus.measurementtool.command.AddPointCommand; import net.osmand.plus.measurementtool.command.ClearPointsCommand; import net.osmand.plus.measurementtool.command.MovePointCommand; import net.osmand.plus.measurementtool.command.RemovePointCommand; import net.osmand.plus.measurementtool.command.ReorderPointCommand; +import net.osmand.plus.profiles.ReorderItemTouchHelperCallback; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController; @@ -452,7 +452,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { } else { pointsRv = new RecyclerView(getActivity()); } - final ItemTouchHelper touchHelper = new ItemTouchHelper(new MeasurementToolItemTouchHelperCallback(adapter)); + ItemTouchHelper touchHelper = new ItemTouchHelper(new ReorderItemTouchHelperCallback(adapter)); touchHelper.attachToRecyclerView(pointsRv); adapter.setAdapterListener(createMeasurementAdapterListener(touchHelper)); pointsRv.setLayoutManager(new LinearLayoutManager(getContext())); diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/adapter/MeasurementToolAdapter.java b/OsmAnd/src/net/osmand/plus/measurementtool/adapter/MeasurementToolAdapter.java index a1c8d5f471..cd5fb5c3de 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/adapter/MeasurementToolAdapter.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/adapter/MeasurementToolAdapter.java @@ -20,12 +20,13 @@ import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.measurementtool.NewGpxData.ActionType; +import net.osmand.plus.profiles.ReorderItemTouchHelperCallback; import java.util.Collections; import java.util.List; public class MeasurementToolAdapter extends RecyclerView.Adapter - implements MeasurementToolItemTouchHelperCallback.ItemTouchHelperAdapter { + implements ReorderItemTouchHelperCallback.OnItemMoveCallback { private final MapActivity mapActivity; private final List points; diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/adapter/MeasurementToolItemTouchHelperCallback.java b/OsmAnd/src/net/osmand/plus/measurementtool/adapter/MeasurementToolItemTouchHelperCallback.java deleted file mode 100644 index 5c5c4d1e7c..0000000000 --- a/OsmAnd/src/net/osmand/plus/measurementtool/adapter/MeasurementToolItemTouchHelperCallback.java +++ /dev/null @@ -1,53 +0,0 @@ -package net.osmand.plus.measurementtool.adapter; - -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.helper.ItemTouchHelper; - - -public class MeasurementToolItemTouchHelperCallback extends ItemTouchHelper.Callback { - - private final ItemTouchHelperAdapter adapter; - - public MeasurementToolItemTouchHelperCallback(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); - } -} diff --git a/OsmAnd/src/net/osmand/plus/profiles/ProfilesAdapter.java b/OsmAnd/src/net/osmand/plus/profiles/ProfilesAdapter.java index 9cac42f24b..c754eba79c 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ProfilesAdapter.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ProfilesAdapter.java @@ -21,7 +21,7 @@ import java.util.Collections; import java.util.List; public class ProfilesAdapter extends AbstractProfileMenuAdapter - implements ProfilesItemTouchHelperCallback.ItemTouchHelperAdapter { + implements ReorderItemTouchHelperCallback.OnItemMoveCallback { private OsmandApplication app; private List applicationModes; diff --git a/OsmAnd/src/net/osmand/plus/profiles/ProfilesItemTouchHelperCallback.java b/OsmAnd/src/net/osmand/plus/profiles/ReorderItemTouchHelperCallback.java similarity index 60% rename from OsmAnd/src/net/osmand/plus/profiles/ProfilesItemTouchHelperCallback.java rename to OsmAnd/src/net/osmand/plus/profiles/ReorderItemTouchHelperCallback.java index bfbb075c8e..0dce4ca014 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ProfilesItemTouchHelperCallback.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ReorderItemTouchHelperCallback.java @@ -3,12 +3,13 @@ package net.osmand.plus.profiles; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; -public class ProfilesItemTouchHelperCallback extends ItemTouchHelper.Callback { - private ProfilesItemTouchHelperCallback.ItemTouchHelperAdapter adapter; +public class ReorderItemTouchHelperCallback extends ItemTouchHelper.Callback { - public ProfilesItemTouchHelperCallback(ProfilesItemTouchHelperCallback.ItemTouchHelperAdapter adapter) { - this.adapter = adapter; + private OnItemMoveCallback itemMoveCallback; + + public ReorderItemTouchHelperCallback(OnItemMoveCallback itemMoveCallback) { + this.itemMoveCallback = itemMoveCallback; } @Override @@ -21,34 +22,35 @@ public class ProfilesItemTouchHelperCallback extends ItemTouchHelper.Callback { return false; } + @Override + public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; + int swipeFlags = 0; + return makeMovementFlags(dragFlags, swipeFlags); + } + + @Override + public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { + int from = viewHolder.getAdapterPosition(); + int to = target.getAdapterPosition(); + if (from == RecyclerView.NO_POSITION || to == RecyclerView.NO_POSITION) { + return false; + } + return itemMoveCallback.onItemMove(from, to); + } + @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { } - @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) { - int from = source.getAdapterPosition(); - int to = target.getAdapterPosition(); - if (from == RecyclerView.NO_POSITION || to == RecyclerView.NO_POSITION) { - return false; - } - return adapter.onItemMove(from, to); - } - @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { super.clearView(recyclerView, viewHolder); - adapter.onItemDismiss(viewHolder); + itemMoveCallback.onItemDismiss(viewHolder); } - interface ItemTouchHelperAdapter { + public interface OnItemMoveCallback { boolean onItemMove(int from, int to); diff --git a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionItemTouchHelperCallback.java b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionItemTouchHelperCallback.java index 9d1897028c..dcd129375c 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionItemTouchHelperCallback.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionItemTouchHelperCallback.java @@ -1,67 +1,33 @@ package net.osmand.plus.quickaction; import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.helper.ItemTouchHelper; + +import net.osmand.plus.profiles.ReorderItemTouchHelperCallback; /** * Created by okorsun on 21.12.16. */ -public class QuickActionItemTouchHelperCallback extends ItemTouchHelper.Callback { +public class QuickActionItemTouchHelperCallback extends ReorderItemTouchHelperCallback { - private OnItemMoveCallback itemMoveCallback; + QuickActionItemTouchHelperCallback(OnItemMoveCallback itemMoveCallback) { + super(itemMoveCallback); + } - public QuickActionItemTouchHelperCallback() { - } + @Override + public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + return !isaHeaderType(viewHolder) ? super.getMovementFlags(recyclerView, viewHolder) : 0; + } - public QuickActionItemTouchHelperCallback(OnItemMoveCallback itemMoveCallback) { - this.itemMoveCallback = itemMoveCallback; - } + @Override + public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { + if (isaHeaderType(viewHolder) || isaHeaderType(target)) { + return false; + } + return super.onMove(recyclerView, viewHolder, target); + } - public void setItemMoveCallback(OnItemMoveCallback itemMoveCallback) { - this.itemMoveCallback = itemMoveCallback; - } - - @Override - public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { - int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; - int swipeFlags = 0; - return !isaHeaderType(viewHolder) ? makeMovementFlags(dragFlags, swipeFlags) : 0; - - } - - @Override - public boolean isItemViewSwipeEnabled() { - return false; - } - - @Override - public boolean isLongPressDragEnabled() { - return false; - } - - @Override - public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { - super.clearView(recyclerView, viewHolder); - itemMoveCallback.onViewDropped(recyclerView, viewHolder); - } - - @Override - public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { - return itemMoveCallback.onMove(recyclerView, viewHolder, target); - } - - @Override - public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { - - } - - private boolean isaHeaderType(RecyclerView.ViewHolder viewHolder) { - return viewHolder.getItemViewType() == QuickActionListFragment.QuickActionAdapter.SCREEN_HEADER_TYPE; - } - - interface OnItemMoveCallback { - boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target); - void onViewDropped(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder); - } + private boolean isaHeaderType(RecyclerView.ViewHolder viewHolder) { + return viewHolder.getItemViewType() == QuickActionListFragment.QuickActionAdapter.SCREEN_HEADER_TYPE; + } } diff --git a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java index 86532e7aee..d4bb874bb9 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java @@ -363,35 +363,26 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick } @Override - public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { - if (viewHolder.getItemViewType() == SCREEN_HEADER_TYPE || target.getItemViewType() == SCREEN_HEADER_TYPE) - return false; - else { - int selectedPosition = viewHolder.getAdapterPosition(); - int targetPosition = target.getAdapterPosition(); - Log.v(TAG, "selected: " + selectedPosition + ", target: " + targetPosition); + public boolean onItemMove(int selectedPosition, int targetPosition) { + Log.v(TAG, "selected: " + selectedPosition + ", target: " + targetPosition); - if (selectedPosition < 0 || targetPosition < 0) - return false; - - Collections.swap(itemsList, selectedPosition, targetPosition); - if (selectedPosition - targetPosition < -1) { - notifyItemMoved(selectedPosition, targetPosition); - notifyItemMoved(targetPosition - 1, selectedPosition); - } else if (selectedPosition - targetPosition > 1) { - notifyItemMoved(selectedPosition, targetPosition); - notifyItemMoved(targetPosition + 1, selectedPosition); - } else { - notifyItemMoved(selectedPosition, targetPosition); - } - notifyItemChanged(selectedPosition); - notifyItemChanged(targetPosition); - return true; + Collections.swap(itemsList, selectedPosition, targetPosition); + if (selectedPosition - targetPosition < -1) { + notifyItemMoved(selectedPosition, targetPosition); + notifyItemMoved(targetPosition - 1, selectedPosition); + } else if (selectedPosition - targetPosition > 1) { + notifyItemMoved(selectedPosition, targetPosition); + notifyItemMoved(targetPosition + 1, selectedPosition); + } else { + notifyItemMoved(selectedPosition, targetPosition); } + notifyItemChanged(selectedPosition); + notifyItemChanged(targetPosition); + return true; } @Override - public void onViewDropped(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + public void onItemDismiss(RecyclerView.ViewHolder holder) { saveQuickActions(); } diff --git a/OsmAnd/src/net/osmand/plus/quickaction/SwitchableAction.java b/OsmAnd/src/net/osmand/plus/quickaction/SwitchableAction.java index 76a3ce9e57..f3258317c7 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/SwitchableAction.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/SwitchableAction.java @@ -33,6 +33,8 @@ public abstract class SwitchableAction extends QuickAction { private transient EditText title; + private transient ItemTouchHelper touchHelper; + protected SwitchableAction(int type) { super(type); } @@ -56,20 +58,17 @@ public abstract class SwitchableAction extends QuickAction { if (!getParams().isEmpty()) { showDialog.setChecked(Boolean.valueOf(getParams().get(KEY_DIALOG))); } - - final RecyclerView list = (RecyclerView) view.findViewById(R.id.list); - final QuickActionItemTouchHelperCallback touchHelperCallback = new QuickActionItemTouchHelperCallback(); - final ItemTouchHelper touchHelper = new ItemTouchHelper(touchHelperCallback); - - final Adapter adapter = new Adapter(activity, new QuickActionListFragment.OnStartDragListener() { + RecyclerView list = (RecyclerView) view.findViewById(R.id.list); + Adapter adapter = new Adapter(activity, new QuickActionListFragment.OnStartDragListener() { @Override public void onStartDrag(RecyclerView.ViewHolder viewHolder) { touchHelper.startDrag(viewHolder); } }); - touchHelperCallback.setItemMoveCallback(adapter); + QuickActionItemTouchHelperCallback touchHelperCallback = new QuickActionItemTouchHelperCallback(adapter); + touchHelper = new ItemTouchHelper(touchHelperCallback); touchHelper.attachToRecyclerView(list); if (!getParams().isEmpty()) { @@ -215,17 +214,9 @@ public abstract class SwitchableAction extends QuickAction { } @Override - public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { - - int selectedPosition = viewHolder.getAdapterPosition(); - int targetPosition = target.getAdapterPosition(); - - if (selectedPosition < 0 || targetPosition < 0) { - return false; - } - + public boolean onItemMove(int selectedPosition, int targetPosition) { String oldTitle = getTitle(itemsList); - String defaultName = recyclerView.getContext().getString(getNameRes()); + String defaultName = context.getString(getNameRes()); Collections.swap(itemsList, selectedPosition, targetPosition); if (selectedPosition - targetPosition < -1) { @@ -256,7 +247,8 @@ public abstract class SwitchableAction extends QuickAction { } @Override - public void onViewDropped(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + public void onItemDismiss(RecyclerView.ViewHolder holder) { + } public class ItemHolder extends RecyclerView.ViewHolder { diff --git a/OsmAnd/src/net/osmand/plus/settings/EditProfilesFragment.java b/OsmAnd/src/net/osmand/plus/settings/EditProfilesFragment.java index b1c8d0b598..5e71071ab2 100644 --- a/OsmAnd/src/net/osmand/plus/settings/EditProfilesFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/EditProfilesFragment.java @@ -20,7 +20,7 @@ import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.profiles.ProfilesAdapter; -import net.osmand.plus.profiles.ProfilesItemTouchHelperCallback; +import net.osmand.plus.profiles.ReorderItemTouchHelperCallback; import java.util.ArrayList; import java.util.List; @@ -54,7 +54,7 @@ public class EditProfilesFragment extends BaseOsmAndFragment { final List applicationModes = new ArrayList<>(ApplicationMode.allPossibleValues()); final ProfilesAdapter adapter = new ProfilesAdapter(mapActivity, applicationModes); - final ItemTouchHelper touchHelper = new ItemTouchHelper(new ProfilesItemTouchHelperCallback(adapter)); + final ItemTouchHelper touchHelper = new ItemTouchHelper(new ReorderItemTouchHelperCallback(adapter)); touchHelper.attachToRecyclerView(recyclerView); adapter.setAdapterListener(new ProfilesAdapter.ProfilesAdapterListener() {