Add base reorder item touch helper callback
This commit is contained in:
parent
b8661ec76f
commit
521a691144
9 changed files with 76 additions and 177 deletions
|
@ -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()));
|
||||
|
|
|
@ -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<MeasurementToolAdapter.MeasureToolItemVH>
|
||||
implements MeasurementToolItemTouchHelperCallback.ItemTouchHelperAdapter {
|
||||
implements ReorderItemTouchHelperCallback.OnItemMoveCallback {
|
||||
|
||||
private final MapActivity mapActivity;
|
||||
private final List<WptPt> points;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@ import java.util.Collections;
|
|||
import java.util.List;
|
||||
|
||||
public class ProfilesAdapter extends AbstractProfileMenuAdapter<ProfilesAdapter.SelectProfileViewHolder>
|
||||
implements ProfilesItemTouchHelperCallback.ItemTouchHelperAdapter {
|
||||
implements ReorderItemTouchHelperCallback.OnItemMoveCallback {
|
||||
|
||||
private OsmandApplication app;
|
||||
private List<ApplicationMode> applicationModes;
|
||||
|
|
|
@ -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);
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -33,6 +33,8 @@ public abstract class SwitchableAction<T> extends QuickAction {
|
|||
|
||||
private transient EditText title;
|
||||
|
||||
private transient ItemTouchHelper touchHelper;
|
||||
|
||||
protected SwitchableAction(int type) {
|
||||
super(type);
|
||||
}
|
||||
|
@ -56,20 +58,17 @@ public abstract class SwitchableAction<T> 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<T> 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<T> extends QuickAction {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onViewDropped(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
|
||||
public void onItemDismiss(RecyclerView.ViewHolder holder) {
|
||||
|
||||
}
|
||||
|
||||
public class ItemHolder extends RecyclerView.ViewHolder {
|
||||
|
|
|
@ -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<ApplicationMode> 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() {
|
||||
|
|
Loading…
Reference in a new issue