Add base reorder item touch helper callback

This commit is contained in:
Chumva 2019-12-12 16:00:31 +02:00
parent b8661ec76f
commit 521a691144
9 changed files with 76 additions and 177 deletions

View file

@ -63,12 +63,12 @@ import net.osmand.plus.measurementtool.SelectedPointBottomSheetDialogFragment.Se
import net.osmand.plus.measurementtool.SnapToRoadBottomSheetDialogFragment.SnapToRoadFragmentListener; import net.osmand.plus.measurementtool.SnapToRoadBottomSheetDialogFragment.SnapToRoadFragmentListener;
import net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter; import net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter;
import net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter.MeasurementAdapterListener; 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.AddPointCommand;
import net.osmand.plus.measurementtool.command.ClearPointsCommand; import net.osmand.plus.measurementtool.command.ClearPointsCommand;
import net.osmand.plus.measurementtool.command.MovePointCommand; import net.osmand.plus.measurementtool.command.MovePointCommand;
import net.osmand.plus.measurementtool.command.RemovePointCommand; import net.osmand.plus.measurementtool.command.RemovePointCommand;
import net.osmand.plus.measurementtool.command.ReorderPointCommand; 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;
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController;
@ -452,7 +452,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
} else { } else {
pointsRv = new RecyclerView(getActivity()); pointsRv = new RecyclerView(getActivity());
} }
final ItemTouchHelper touchHelper = new ItemTouchHelper(new MeasurementToolItemTouchHelperCallback(adapter)); ItemTouchHelper touchHelper = new ItemTouchHelper(new ReorderItemTouchHelperCallback(adapter));
touchHelper.attachToRecyclerView(pointsRv); touchHelper.attachToRecyclerView(pointsRv);
adapter.setAdapterListener(createMeasurementAdapterListener(touchHelper)); adapter.setAdapterListener(createMeasurementAdapterListener(touchHelper));
pointsRv.setLayoutManager(new LinearLayoutManager(getContext())); pointsRv.setLayoutManager(new LinearLayoutManager(getContext()));

View file

@ -20,12 +20,13 @@ import net.osmand.plus.R;
import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.measurementtool.NewGpxData.ActionType; import net.osmand.plus.measurementtool.NewGpxData.ActionType;
import net.osmand.plus.profiles.ReorderItemTouchHelperCallback;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementToolAdapter.MeasureToolItemVH> public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementToolAdapter.MeasureToolItemVH>
implements MeasurementToolItemTouchHelperCallback.ItemTouchHelperAdapter { implements ReorderItemTouchHelperCallback.OnItemMoveCallback {
private final MapActivity mapActivity; private final MapActivity mapActivity;
private final List<WptPt> points; private final List<WptPt> points;

View file

@ -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);
}
}

View file

@ -21,7 +21,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
public class ProfilesAdapter extends AbstractProfileMenuAdapter<ProfilesAdapter.SelectProfileViewHolder> public class ProfilesAdapter extends AbstractProfileMenuAdapter<ProfilesAdapter.SelectProfileViewHolder>
implements ProfilesItemTouchHelperCallback.ItemTouchHelperAdapter { implements ReorderItemTouchHelperCallback.OnItemMoveCallback {
private OsmandApplication app; private OsmandApplication app;
private List<ApplicationMode> applicationModes; private List<ApplicationMode> applicationModes;

View file

@ -3,12 +3,13 @@ package net.osmand.plus.profiles;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper; 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) { private OnItemMoveCallback itemMoveCallback;
this.adapter = adapter;
public ReorderItemTouchHelperCallback(OnItemMoveCallback itemMoveCallback) {
this.itemMoveCallback = itemMoveCallback;
} }
@Override @Override
@ -21,34 +22,35 @@ public class ProfilesItemTouchHelperCallback extends ItemTouchHelper.Callback {
return false; 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 @Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { 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 @Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder); super.clearView(recyclerView, viewHolder);
adapter.onItemDismiss(viewHolder); itemMoveCallback.onItemDismiss(viewHolder);
} }
interface ItemTouchHelperAdapter { public interface OnItemMoveCallback {
boolean onItemMove(int from, int to); boolean onItemMove(int from, int to);

View file

@ -1,67 +1,33 @@
package net.osmand.plus.quickaction; package net.osmand.plus.quickaction;
import android.support.v7.widget.RecyclerView; 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. * 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) { @Override
this.itemMoveCallback = itemMoveCallback; 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) { private boolean isaHeaderType(RecyclerView.ViewHolder viewHolder) {
this.itemMoveCallback = itemMoveCallback; return viewHolder.getItemViewType() == QuickActionListFragment.QuickActionAdapter.SCREEN_HEADER_TYPE;
} }
@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);
}
} }

View file

@ -363,35 +363,26 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
} }
@Override @Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { public boolean onItemMove(int selectedPosition, int targetPosition) {
if (viewHolder.getItemViewType() == SCREEN_HEADER_TYPE || target.getItemViewType() == SCREEN_HEADER_TYPE) Log.v(TAG, "selected: " + selectedPosition + ", target: " + targetPosition);
return false;
else {
int selectedPosition = viewHolder.getAdapterPosition();
int targetPosition = target.getAdapterPosition();
Log.v(TAG, "selected: " + selectedPosition + ", target: " + targetPosition);
if (selectedPosition < 0 || targetPosition < 0) Collections.swap(itemsList, selectedPosition, targetPosition);
return false; if (selectedPosition - targetPosition < -1) {
notifyItemMoved(selectedPosition, targetPosition);
Collections.swap(itemsList, selectedPosition, targetPosition); notifyItemMoved(targetPosition - 1, selectedPosition);
if (selectedPosition - targetPosition < -1) { } else if (selectedPosition - targetPosition > 1) {
notifyItemMoved(selectedPosition, targetPosition); notifyItemMoved(selectedPosition, targetPosition);
notifyItemMoved(targetPosition - 1, selectedPosition); notifyItemMoved(targetPosition + 1, selectedPosition);
} else if (selectedPosition - targetPosition > 1) { } else {
notifyItemMoved(selectedPosition, targetPosition); notifyItemMoved(selectedPosition, targetPosition);
notifyItemMoved(targetPosition + 1, selectedPosition);
} else {
notifyItemMoved(selectedPosition, targetPosition);
}
notifyItemChanged(selectedPosition);
notifyItemChanged(targetPosition);
return true;
} }
notifyItemChanged(selectedPosition);
notifyItemChanged(targetPosition);
return true;
} }
@Override @Override
public void onViewDropped(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { public void onItemDismiss(RecyclerView.ViewHolder holder) {
saveQuickActions(); saveQuickActions();
} }

View file

@ -33,6 +33,8 @@ public abstract class SwitchableAction<T> extends QuickAction {
private transient EditText title; private transient EditText title;
private transient ItemTouchHelper touchHelper;
protected SwitchableAction(int type) { protected SwitchableAction(int type) {
super(type); super(type);
} }
@ -56,20 +58,17 @@ public abstract class SwitchableAction<T> extends QuickAction {
if (!getParams().isEmpty()) { if (!getParams().isEmpty()) {
showDialog.setChecked(Boolean.valueOf(getParams().get(KEY_DIALOG))); showDialog.setChecked(Boolean.valueOf(getParams().get(KEY_DIALOG)));
} }
final RecyclerView list = (RecyclerView) view.findViewById(R.id.list);
final QuickActionItemTouchHelperCallback touchHelperCallback = new QuickActionItemTouchHelperCallback(); RecyclerView list = (RecyclerView) view.findViewById(R.id.list);
final ItemTouchHelper touchHelper = new ItemTouchHelper(touchHelperCallback); Adapter adapter = new Adapter(activity, new QuickActionListFragment.OnStartDragListener() {
final Adapter adapter = new Adapter(activity, new QuickActionListFragment.OnStartDragListener() {
@Override @Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) { public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
touchHelper.startDrag(viewHolder); touchHelper.startDrag(viewHolder);
} }
}); });
touchHelperCallback.setItemMoveCallback(adapter); QuickActionItemTouchHelperCallback touchHelperCallback = new QuickActionItemTouchHelperCallback(adapter);
touchHelper = new ItemTouchHelper(touchHelperCallback);
touchHelper.attachToRecyclerView(list); touchHelper.attachToRecyclerView(list);
if (!getParams().isEmpty()) { if (!getParams().isEmpty()) {
@ -215,17 +214,9 @@ public abstract class SwitchableAction<T> extends QuickAction {
} }
@Override @Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { public boolean onItemMove(int selectedPosition, int targetPosition) {
int selectedPosition = viewHolder.getAdapterPosition();
int targetPosition = target.getAdapterPosition();
if (selectedPosition < 0 || targetPosition < 0) {
return false;
}
String oldTitle = getTitle(itemsList); String oldTitle = getTitle(itemsList);
String defaultName = recyclerView.getContext().getString(getNameRes()); String defaultName = context.getString(getNameRes());
Collections.swap(itemsList, selectedPosition, targetPosition); Collections.swap(itemsList, selectedPosition, targetPosition);
if (selectedPosition - targetPosition < -1) { if (selectedPosition - targetPosition < -1) {
@ -256,7 +247,8 @@ public abstract class SwitchableAction<T> extends QuickAction {
} }
@Override @Override
public void onViewDropped(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { public void onItemDismiss(RecyclerView.ViewHolder holder) {
} }
public class ItemHolder extends RecyclerView.ViewHolder { public class ItemHolder extends RecyclerView.ViewHolder {

View file

@ -20,7 +20,7 @@ import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.profiles.ProfilesAdapter; 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.ArrayList;
import java.util.List; import java.util.List;
@ -54,7 +54,7 @@ public class EditProfilesFragment extends BaseOsmAndFragment {
final List<ApplicationMode> applicationModes = new ArrayList<>(ApplicationMode.allPossibleValues()); final List<ApplicationMode> applicationModes = new ArrayList<>(ApplicationMode.allPossibleValues());
final ProfilesAdapter adapter = new ProfilesAdapter(mapActivity, applicationModes); 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); touchHelper.attachToRecyclerView(recyclerView);
adapter.setAdapterListener(new ProfilesAdapter.ProfilesAdapterListener() { adapter.setAdapterListener(new ProfilesAdapter.ProfilesAdapterListener() {