Merge pull request #4371 from osmandapp/sasha_pasha_branch

Add drag and drop to markers recycler view
This commit is contained in:
Alexey 2017-08-31 15:02:47 +03:00 committed by GitHub
commit 6b2e10e96d
3 changed files with 103 additions and 3 deletions

View file

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

View file

@ -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<MapMarkerItemViewHolder> {
public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemViewHolder>
implements MapMarkersItemTouchHelperCallback.ItemTouchHelperAdapter {
private MapActivity mapActivity;
private List<MapMarker> markers;
@ -64,14 +67,17 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
}
@Override
public void onBindViewHolder(MapMarkerItemViewHolder holder, int pos) {
public void onBindViewHolder(final MapMarkerItemViewHolder holder, int pos) {
IconsCache iconsCache = mapActivity.getMyApplication().getIconsCache();
MapMarker marker = markers.get(pos);
holder.iconReorder.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_reorder));
holder.iconReorder.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
public boolean onTouch(View view, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
listener.onDragStarted(holder);
}
return false;
}
});
@ -96,8 +102,28 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
return markers.get(position);
}
public List<MapMarker> 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);
}
}

View file

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