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.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()));

View file

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

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;
public class ProfilesAdapter extends AbstractProfileMenuAdapter<ProfilesAdapter.SelectProfileViewHolder>
implements ProfilesItemTouchHelperCallback.ItemTouchHelperAdapter {
implements ReorderItemTouchHelperCallback.OnItemMoveCallback {
private OsmandApplication app;
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.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);

View file

@ -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;
public QuickActionItemTouchHelperCallback() {
}
public QuickActionItemTouchHelperCallback(OnItemMoveCallback itemMoveCallback) {
this.itemMoveCallback = itemMoveCallback;
}
public void setItemMoveCallback(OnItemMoveCallback itemMoveCallback) {
this.itemMoveCallback = itemMoveCallback;
QuickActionItemTouchHelperCallback(OnItemMoveCallback itemMoveCallback) {
super(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);
return !isaHeaderType(viewHolder) ? super.getMovementFlags(recyclerView, viewHolder) : 0;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return itemMoveCallback.onMove(recyclerView, viewHolder, target);
if (isaHeaderType(viewHolder) || isaHeaderType(target)) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
return super.onMove(recyclerView, viewHolder, target);
}
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,17 +363,9 @@ 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();
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);
@ -388,10 +380,9 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
notifyItemChanged(targetPosition);
return true;
}
}
@Override
public void onViewDropped(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
public void onItemDismiss(RecyclerView.ViewHolder holder) {
saveQuickActions();
}

View file

@ -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);
}
@ -57,19 +59,16 @@ public abstract class SwitchableAction<T> extends QuickAction {
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 {

View file

@ -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() {