Add drag and drop for points list

This commit is contained in:
Alexander Sytnyk 2017-08-09 14:15:31 +03:00
parent eaef53d552
commit 654f32652f
4 changed files with 138 additions and 75 deletions

View file

@ -1,84 +1,91 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="?attr/selectableItemBackground"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/measure_point_reorder_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:tint="?attr/secondary_icon_color"
tools:src="@drawable/ic_action_reorder"/>
<android.support.v7.widget.AppCompatImageView
android:id="@+id/measure_point_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_toEndOf="@id/measure_point_reorder_icon"
android:layout_toRightOf="@id/measure_point_reorder_icon"
tools:src="@drawable/ic_action_measure_point"/>
<ImageButton
android:id="@+id/measure_point_remove_image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackground"
android:focusableInTouchMode="true"
android:paddingBottom="12dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="12dp"
tools:src="@drawable/ic_action_remove_dark"/>
<LinearLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/measure_point_icon"
android:layout_toLeftOf="@id/measure_point_remove_image_button"
android:layout_toRightOf="@+id/measure_point_icon"
android:layout_toStartOf="@id/measure_point_remove_image_button"
android:orientation="vertical">
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/measure_point_title"
<android.support.v7.widget.AppCompatImageView
android:id="@+id/measure_point_reorder_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:paddingBottom="12dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="12dp"
android:tint="?attr/secondary_icon_color"
tools:src="@drawable/ic_action_reorder"/>
<android.support.v7.widget.AppCompatImageView
android:id="@+id/measure_point_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_toEndOf="@id/measure_point_reorder_icon"
android:layout_toRightOf="@id/measure_point_reorder_icon"
tools:src="@drawable/ic_action_measure_point"/>
<ImageButton
android:id="@+id/measure_point_remove_image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackground"
android:focusableInTouchMode="true"
android:paddingBottom="12dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="12dp"
tools:src="@drawable/ic_action_remove_dark"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/default_desc_text_size"
tools:text="@string/plugin_distance_point"/>
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/measure_point_icon"
android:layout_toLeftOf="@id/measure_point_remove_image_button"
android:layout_toRightOf="@+id/measure_point_icon"
android:layout_toStartOf="@id/measure_point_remove_image_button"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/measure_point_descr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_sub_text_size"
tools:text="@string/shared_string_control_start"/>
</LinearLayout>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/measure_point_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/default_desc_text_size"
tools:text="@string/plugin_distance_point"/>
<View
android:id="@+id/points_divider"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/measure_point_reorder_icon"
android:layout_toRightOf="@+id/measure_point_reorder_icon"
android:background="?attr/dashboard_divider"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/measure_point_descr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_sub_text_size"
tools:text="@string/shared_string_control_start"/>
</LinearLayout>
</RelativeLayout>
<View
android:id="@+id/points_divider"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/measure_point_reorder_icon"
android:layout_toRightOf="@+id/measure_point_reorder_icon"
android:background="?attr/dashboard_divider"/>
</RelativeLayout>
</FrameLayout>

View file

@ -14,6 +14,7 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ContextThemeWrapper;
@ -41,6 +42,7 @@ import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.activities.TrackActivity.NewGpxLine;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter;
import net.osmand.plus.measurementtool.adapter.MeasurementToolItemTouchHelperCallback;
import net.osmand.plus.measurementtool.command.AddPointCommand;
import net.osmand.plus.measurementtool.command.CommandManager;
import net.osmand.plus.measurementtool.command.RemovePointCommand;
@ -237,6 +239,8 @@ public class MeasurementToolFragment extends Fragment {
adapter = new MeasurementToolAdapter(getMapActivity(), measurementLayer.getMeasurementPoints());
final RecyclerView rv = mainView.findViewById(R.id.measure_points_recycler_view);
final ItemTouchHelper touchHelper = new ItemTouchHelper(new MeasurementToolItemTouchHelperCallback(adapter));
touchHelper.attachToRecyclerView(rv);
adapter.setAdapterListener(new MeasurementToolAdapter.MeasurementAdapterListener() {
@Override
public void onPointRemove(int position) {
@ -258,6 +262,11 @@ public class MeasurementToolFragment extends Fragment {
int pos = rv.indexOfChild(view);
measurementLayer.moveMapToPoint(pos);
}
@Override
public void onDragStarted(RecyclerView.ViewHolder holder) {
touchHelper.startDrag(holder);
}
});
rv.setLayoutManager(new LinearLayoutManager(getContext()));
rv.setAdapter(adapter);

View file

@ -1,7 +1,10 @@
package net.osmand.plus.measurementtool.adapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
@ -15,9 +18,11 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.util.MapUtils;
import java.util.Collections;
import java.util.List;
public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementToolAdapter.MeasureToolItemVH> {
public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementToolAdapter.MeasureToolItemVH>
implements MeasurementToolItemTouchHelperCallback.ItemTouchHelperAdapter {
private final MapActivity mapActivity;
private final List<WptPt> points;
@ -39,6 +44,9 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
if (!nightMode) {
view.findViewById(R.id.points_divider).setBackgroundResource(R.drawable.divider);
}
final int backgroundColor = ContextCompat.getColor(mapActivity,
nightMode ? R.color.ctx_menu_info_view_bg_dark : R.color.ctx_menu_info_view_bg_light);
view.setBackgroundColor(backgroundColor);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -52,6 +60,15 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
public void onBindViewHolder(final MeasureToolItemVH holder, int pos) {
IconsCache iconsCache = mapActivity.getMyApplication().getIconsCache();
holder.iconReorder.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_reorder));
holder.iconReorder.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
adapterListener.onDragStarted(holder);
}
return false;
}
});
holder.icon.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_measure_point));
holder.title.setText(mapActivity.getString(R.string.plugin_distance_point) + " - " + (pos + 1));
if (pos < 1) {
@ -78,6 +95,13 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
return points.size();
}
@Override
public boolean onItemMove(int from, int to) {
Collections.swap(points, from, to);
notifyItemMoved(from, to);
return true;
}
static class MeasureToolItemVH extends RecyclerView.ViewHolder {
final ImageView iconReorder;
@ -101,5 +125,7 @@ public class MeasurementToolAdapter extends RecyclerView.Adapter<MeasurementTool
void onPointRemove(int position);
void onItemClick(View view);
void onDragStarted(RecyclerView.ViewHolder holder);
}
}

View file

@ -6,18 +6,39 @@ import android.support.v7.widget.helper.ItemTouchHelper;
public class MeasurementToolItemTouchHelperCallback extends ItemTouchHelper.Callback {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
return 0;
private final ItemTouchHelperAdapter adapter;
public MeasurementToolItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
this.adapter = adapter;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder viewHolder1) {
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) {
}
interface ItemTouchHelperAdapter {
boolean onItemMove(int from, int to);
}
}