From c4499b1748b2d3684447e7463d2e33b6967da95e Mon Sep 17 00:00:00 2001 From: Korusn Oleksandr Date: Tue, 20 Dec 2016 16:23:40 +0200 Subject: [PATCH 1/5] quick action list pre set up --- OsmAnd/res/layout/quick_action_list.xml | 13 ++ .../res/layout/quick_action_list_header.xml | 20 +++ OsmAnd/res/layout/quick_action_list_item.xml | 83 ++++++++++ .../plus/quickaction/QuickActionItem.java | 48 ++++++ .../quickaction/QuickActionListFragment.java | 144 ++++++++++++++++++ 5 files changed, 308 insertions(+) create mode 100644 OsmAnd/res/layout/quick_action_list.xml create mode 100644 OsmAnd/res/layout/quick_action_list_header.xml create mode 100644 OsmAnd/res/layout/quick_action_list_item.xml create mode 100644 OsmAnd/src/net/osmand/plus/quickaction/QuickActionItem.java create mode 100644 OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java diff --git a/OsmAnd/res/layout/quick_action_list.xml b/OsmAnd/res/layout/quick_action_list.xml new file mode 100644 index 0000000000..e2d5b0eb1e --- /dev/null +++ b/OsmAnd/res/layout/quick_action_list.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/quick_action_list_header.xml b/OsmAnd/res/layout/quick_action_list_header.xml new file mode 100644 index 0000000000..02102aa060 --- /dev/null +++ b/OsmAnd/res/layout/quick_action_list_header.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/quick_action_list_item.xml b/OsmAnd/res/layout/quick_action_list_item.xml new file mode 100644 index 0000000000..7680ad88ee --- /dev/null +++ b/OsmAnd/res/layout/quick_action_list_item.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionItem.java b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionItem.java new file mode 100644 index 0000000000..87629013d7 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionItem.java @@ -0,0 +1,48 @@ +package net.osmand.plus.quickaction; + +import android.support.annotation.DrawableRes; +import android.support.annotation.StringRes; + +/** + * Created by okorsun on 20.12.16. + */ + +public class QuickActionItem { + private static final int HEADER_VALUE = -1; + + private final int nameRes; + private final int drawableRes; + + public QuickActionItem(@StringRes int nameRes, @DrawableRes int drawableRes) { + this.nameRes = nameRes; + this.drawableRes = drawableRes; + } + + private QuickActionItem(){ + nameRes = HEADER_VALUE; + drawableRes = HEADER_VALUE; + } + + public static QuickActionItem createHeaderItem() { + return new QuickActionItem(); + } + + public int getNameRes() { + return nameRes; + } + + public int getDrawableRes() { + return drawableRes; + } + + public boolean isHeader() { + return nameRes == HEADER_VALUE; + } + +// public interface QuickActionListItem{ +// int getNameRes(); +// int getDrawableRes(); +// boolean isHeader(); +// } + +} diff --git a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java new file mode 100644 index 0000000000..0b23b9bcd7 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java @@ -0,0 +1,144 @@ +package net.osmand.plus.quickaction; + +import android.content.res.Resources; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.widget.RecyclerView; +import android.util.TypedValue; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +import net.osmand.plus.R; +import net.osmand.plus.base.BaseOsmAndFragment; + +import java.util.ArrayList; +import java.util.List; + +import static android.util.TypedValue.COMPLEX_UNIT_DIP; + +/** + * Created by okorsun on 20.12.16. + */ + +public class QuickActionListFragment extends BaseOsmAndFragment { + + RecyclerView quickActionRV; + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.quick_action_list, container, false); + + quickActionRV = (RecyclerView) view.findViewById(R.id.recycler_view); + + + return view; + } + + + public class QuickActionAdapter extends RecyclerView.Adapter { + private static final int SCREEN_ITEM_TYPE = 1; + private static final int SCREEN_TITLE_TYPE = 2; + + private static final int ITEMS_IN_GROUP = 6; + + private List itemsList = new ArrayList<>(); + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + LayoutInflater inflater = LayoutInflater.from(parent.getContext()); + if (viewType == SCREEN_ITEM_TYPE) + return new QuickActionItemVH(inflater.inflate(R.layout.quick_action_list_item, parent, false)); + else + return new QuickActionHeaderVH(inflater.inflate(R.layout.quick_action_list_header, parent, false)); + } + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + int viewType = getItemViewType(position); + QuickActionItem item = itemsList.get(position); + + if (viewType == SCREEN_ITEM_TYPE) { + QuickActionItemVH itemVH = (QuickActionItemVH) holder; + itemVH.icon.setImageResource(item.getDrawableRes()); + itemVH.title.setText(item.getNameRes()); + itemVH.subTitle.setText("Action" + getActionPosition(position)); //TODO: get proper string + + LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); + dividerParams.setMargins(isShortDivider(position) ? dpToPx(56f) : 0, 0, 0, 0); + } else { + QuickActionHeaderVH headerVH = (QuickActionHeaderVH) holder; + headerVH.headerName.setText("Screen" + (position/(ITEMS_IN_GROUP + 1) + 1)); //TODO: get proper string + } + } + + @Override + public int getItemCount() { + return itemsList.size(); + } + + @Override + public int getItemViewType(int position) { + return itemsList.get(position).isHeader() ? SCREEN_TITLE_TYPE : SCREEN_ITEM_TYPE; + } + + public void deleteItem(int position) { + itemsList.remove(position); + + notifyItemRemoved(position); + } + + + private int getActionPosition(int globalPosition) { + return globalPosition % (ITEMS_IN_GROUP + 1); + } + + private boolean isShortDivider(int globalPosition) { + return getActionPosition(globalPosition) == ITEMS_IN_GROUP || (globalPosition + 1) == getItemCount(); + } + + private int dpToPx(float dp) { + Resources r = getActivity().getResources(); + return (int) TypedValue.applyDimension( + COMPLEX_UNIT_DIP, + dp, + r.getDisplayMetrics() + ); + } + + public class QuickActionItemVH extends RecyclerView.ViewHolder { + public TextView title; + public TextView subTitle; + public ImageView icon; + public View divider; + + public QuickActionItemVH(View itemView) { + super(itemView); + title = (TextView) itemView.findViewById(R.id.title); + subTitle = (TextView) itemView.findViewById(R.id.subtitle); + icon = (ImageView) itemView.findViewById(R.id.imageView); + divider = itemView.findViewById(R.id.divider); + + itemView.findViewById(R.id.closeImageButton).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + QuickActionAdapter.this.deleteItem(getAdapterPosition()); + } + }); + } + } + + public class QuickActionHeaderVH extends RecyclerView.ViewHolder { + public TextView headerName; + + public QuickActionHeaderVH(View itemView) { + super(itemView); + headerName = (TextView) itemView.findViewById(R.id.header); + } + } + } +} From 481ea7551c26a0e2ea4bec379e1942b89386c065 Mon Sep 17 00:00:00 2001 From: Korusn Oleksandr Date: Tue, 20 Dec 2016 17:57:14 +0200 Subject: [PATCH 2/5] add button to configure screen (not propper one) --- .../views/mapwidgets/MapWidgetRegistry.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java index 6881cd3142..d647306a4b 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java @@ -24,6 +24,7 @@ import net.osmand.plus.OsmandSettings.OsmandPreference; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.dialogs.ConfigureMapMenu; +import net.osmand.plus.quickaction.QuickActionListFragment; import net.osmand.plus.views.MapInfoLayer; import net.osmand.plus.views.OsmandMapLayer.DrawSettings; import net.osmand.plus.views.OsmandMapTileView; @@ -36,7 +37,6 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import java.util.TreeSet; -import java.util.zip.GZIPOutputStream; public class MapWidgetRegistry { @@ -358,6 +358,7 @@ public class MapWidgetRegistry { public void addControls(MapActivity map, ContextMenuAdapter cm, ApplicationMode mode) { + addQuickActionControl(map, cm, mode); // Right panel cm.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.map_widget_right, map) .setCategory(true).setLayout(R.layout.list_group_title_with_switch).createItem()); @@ -384,6 +385,37 @@ public class MapWidgetRegistry { return leftWidgetSet; } + private void addQuickActionControl(final MapActivity mapActivity, final ContextMenuAdapter contextMenuAdapter, + final ApplicationMode mode) { + boolean selected = true; + contextMenuAdapter.addItem(new ContextMenuItem.ItemBuilder() // TODO: add top margin and proper data + .setTitleId(R.string.av_video_quality_high, mapActivity) + .setIcon(R.drawable.ic_action_quit_dark) + .setSelected(selected) + .setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID) + .setSecondaryIcon(/*r.widget != null*/ true ? R.drawable.ic_action_additional_option : ContextMenuItem.INVALID_ID) + .setListener(new ContextMenuAdapter.OnRowItemClick() { + @Override + public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int position, boolean isChecked) { + return false; + } + + @Override + public boolean onRowItemClick(ArrayAdapter adapter, View view, int itemId, int position) { + int slideInAnim = R.anim.slide_in_bottom; + int slideOutAnim = R.anim.slide_out_bottom; + + mapActivity.getSupportFragmentManager().beginTransaction() + .setCustomAnimations(slideInAnim, slideOutAnim, slideInAnim, slideOutAnim) + .add(R.id.fragmentContainer, new QuickActionListFragment(), QuickActionListFragment.TAG) + .addToBackStack(QuickActionListFragment.TAG).commitAllowingStateLoss(); + + return true; + } + }) + .createItem()); + } + private void addControls(final MapActivity mapActivity, final ContextMenuAdapter contextMenuAdapter, Set groupTitle, final ApplicationMode mode) { for (final MapWidgetRegInfo r : groupTitle) { From 368bb2738f619eafbde8a3aea9ee8ddfcef1539b Mon Sep 17 00:00:00 2001 From: Korusn Oleksandr Date: Tue, 20 Dec 2016 19:26:34 +0200 Subject: [PATCH 3/5] working on quick action list --- OsmAnd/res/layout/quick_action_list.xml | 1 + .../res/layout/quick_action_list_header.xml | 3 +- OsmAnd/res/layout/quick_action_list_item.xml | 5 ++- .../quickaction/QuickActionListFragment.java | 39 ++++++++++++++++++- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/OsmAnd/res/layout/quick_action_list.xml b/OsmAnd/res/layout/quick_action_list.xml index e2d5b0eb1e..3bc404f17b 100644 --- a/OsmAnd/res/layout/quick_action_list.xml +++ b/OsmAnd/res/layout/quick_action_list.xml @@ -8,6 +8,7 @@ android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" + android:background="?attr/ctx_menu_info_view_bg" /> \ No newline at end of file diff --git a/OsmAnd/res/layout/quick_action_list_header.xml b/OsmAnd/res/layout/quick_action_list_header.xml index 02102aa060..1af77b5d88 100644 --- a/OsmAnd/res/layout/quick_action_list_header.xml +++ b/OsmAnd/res/layout/quick_action_list_header.xml @@ -2,10 +2,11 @@ diff --git a/OsmAnd/res/layout/quick_action_list_item.xml b/OsmAnd/res/layout/quick_action_list_item.xml index 7680ad88ee..9bb826a140 100644 --- a/OsmAnd/res/layout/quick_action_list_item.xml +++ b/OsmAnd/res/layout/quick_action_list_item.xml @@ -2,6 +2,7 @@ @@ -20,7 +21,7 @@ android:layout_marginRight="4dp" android:focusable="false" android:scaleType="centerInside" - android:src="@drawable/map_drawer" + android:src="@drawable/ic_action_reorder" android:visibility="gone" tools:visibility="visible"/> @@ -69,7 +70,7 @@ android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" android:layout_marginRight="16dp" - android:src="@drawable/ic_action_close_dark" + android:src="@drawable/ic_action_remove_dark" android:scaleType="centerInside"/> diff --git a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java index 0b23b9bcd7..2f6bee7c39 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java @@ -3,6 +3,7 @@ package net.osmand.plus.quickaction; import android.content.res.Resources; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.TypedValue; import android.view.LayoutInflater; @@ -25,8 +26,10 @@ import static android.util.TypedValue.COMPLEX_UNIT_DIP; */ public class QuickActionListFragment extends BaseOsmAndFragment { + public static final String TAG = QuickActionListFragment.class.getSimpleName(); RecyclerView quickActionRV; + QuickActionAdapter adapter; @Nullable @Override @@ -34,11 +37,30 @@ public class QuickActionListFragment extends BaseOsmAndFragment { View view = inflater.inflate(R.layout.quick_action_list, container, false); quickActionRV = (RecyclerView) view.findViewById(R.id.recycler_view); +// quickActionRV.setBackgroundColor( +// getResources().getColor( +// getMyApplication().getSettings().isLightContent() ? R.color.bg_color_light +// : R.color.bg_color_dark)); + adapter = new QuickActionAdapter(); + quickActionRV.setAdapter(adapter); + quickActionRV.setLayoutManager(new LinearLayoutManager(getContext())); + + adapter.addItems(createMockDada()); + return view; } + private List createMockDada() { + List result = new ArrayList<>(); + for (int i = 0; i < 15; i ++){ + result.add(new QuickActionItem(R.string.favorite, R.drawable.ic_action_flag_dark)); + } + + return result; + } + public class QuickActionAdapter extends RecyclerView.Adapter { private static final int SCREEN_ITEM_TYPE = 1; @@ -66,13 +88,13 @@ public class QuickActionListFragment extends BaseOsmAndFragment { QuickActionItemVH itemVH = (QuickActionItemVH) holder; itemVH.icon.setImageResource(item.getDrawableRes()); itemVH.title.setText(item.getNameRes()); - itemVH.subTitle.setText("Action" + getActionPosition(position)); //TODO: get proper string + itemVH.subTitle.setText("Action " + getActionPosition(position)); //TODO: get proper string LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); dividerParams.setMargins(isShortDivider(position) ? dpToPx(56f) : 0, 0, 0, 0); } else { QuickActionHeaderVH headerVH = (QuickActionHeaderVH) holder; - headerVH.headerName.setText("Screen" + (position/(ITEMS_IN_GROUP + 1) + 1)); //TODO: get proper string + headerVH.headerName.setText("Screen " + (position/(ITEMS_IN_GROUP + 1) + 1)); //TODO: get proper string } } @@ -92,6 +114,19 @@ public class QuickActionListFragment extends BaseOsmAndFragment { notifyItemRemoved(position); } + public void addItems(List data) { + List resultList = new ArrayList<>(); + for (int i = 0; i < data.size(); i++) { + if (i % ITEMS_IN_GROUP == 0) + resultList.add(QuickActionItem.createHeaderItem()); + + resultList.add(data.get(i)); + } + + itemsList = resultList; + notifyDataSetChanged(); + } + private int getActionPosition(int globalPosition) { return globalPosition % (ITEMS_IN_GROUP + 1); From 7af5c8c20b6dfcfaa11134d2210a8cdf9df2b92d Mon Sep 17 00:00:00 2001 From: Korusn Oleksandr Date: Wed, 21 Dec 2016 18:13:50 +0200 Subject: [PATCH 4/5] button on configure screen ready --- .../list_group_empty_title_with_switch.xml | 52 +++++++++++++++++++ OsmAnd/res/values/strings.xml | 3 ++ .../QuickActionItemTouchHelperCallback.java | 8 +++ .../views/mapwidgets/MapWidgetRegistry.java | 28 ++++++++-- 4 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 OsmAnd/res/layout/list_group_empty_title_with_switch.xml create mode 100644 OsmAnd/src/net/osmand/plus/quickaction/QuickActionItemTouchHelperCallback.java diff --git a/OsmAnd/res/layout/list_group_empty_title_with_switch.xml b/OsmAnd/res/layout/list_group_empty_title_with_switch.xml new file mode 100644 index 0000000000..bb4418505d --- /dev/null +++ b/OsmAnd/res/layout/list_group_empty_title_with_switch.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index b0c98f937e..f67f962a0a 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -2456,4 +2456,7 @@ If you need help with OsmAnd application, please contact our support team: suppo Number of edits Report for File name contains illegal character + Quick action + Action %d + Screen %d diff --git a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionItemTouchHelperCallback.java b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionItemTouchHelperCallback.java new file mode 100644 index 0000000000..67ba29223b --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionItemTouchHelperCallback.java @@ -0,0 +1,8 @@ +package net.osmand.plus.quickaction; + +/** + * Created by okorsun on 21.12.16. + */ + +public class QuickActionItemTouchHelperCallback { +} diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java index d647306a4b..b0717cfc8a 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapWidgetRegistry.java @@ -387,16 +387,21 @@ public class MapWidgetRegistry { private void addQuickActionControl(final MapActivity mapActivity, final ContextMenuAdapter contextMenuAdapter, final ApplicationMode mode) { + + contextMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.map_widget_right, mapActivity) + .setCategory(true).setLayout(R.layout.list_group_empty_title_with_switch).createItem()); + boolean selected = true; - contextMenuAdapter.addItem(new ContextMenuItem.ItemBuilder() // TODO: add top margin and proper data - .setTitleId(R.string.av_video_quality_high, mapActivity) - .setIcon(R.drawable.ic_action_quit_dark) + contextMenuAdapter.addItem(new ContextMenuItem.ItemBuilder() + .setTitleId(R.string.configure_screen_quick_action, mapActivity) + .setIcon(R.drawable.map_quick_action) .setSelected(selected) .setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID) - .setSecondaryIcon(/*r.widget != null*/ true ? R.drawable.ic_action_additional_option : ContextMenuItem.INVALID_ID) + .setSecondaryIcon( R.drawable.ic_action_additional_option) .setListener(new ContextMenuAdapter.OnRowItemClick() { @Override public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int position, boolean isChecked) { + setVisibility(adapter, position, isChecked, false); return false; } @@ -412,6 +417,21 @@ public class MapWidgetRegistry { return true; } + + private void setVisibility(ArrayAdapter adapter, + int position, + boolean visible, + boolean collapsed) { +// MapWidgetRegistry.this.setVisibility(r, visible, collapsed); + MapInfoLayer mil = mapActivity.getMapLayers().getMapInfoLayer(); + if (mil != null) { + mil.recreateControls(); + } + ContextMenuItem item = adapter.getItem(position); + item.setSelected(visible); + item.setColorRes(visible ? R.color.osmand_orange : ContextMenuItem.INVALID_ID); + adapter.notifyDataSetChanged(); + } }) .createItem()); } From 86ba0403f89dda4a81a17463af29d1b5a74c589f Mon Sep 17 00:00:00 2001 From: Korusn Oleksandr Date: Wed, 21 Dec 2016 18:14:51 +0200 Subject: [PATCH 5/5] actions list drag and drop implemented --- OsmAnd/res/layout/quick_action_list.xml | 6 + OsmAnd/res/layout/quick_action_list_item.xml | 6 +- .../QuickActionItemTouchHelperCallback.java | 47 +++++++- .../quickaction/QuickActionListFragment.java | 110 ++++++++++++++++-- 4 files changed, 154 insertions(+), 15 deletions(-) diff --git a/OsmAnd/res/layout/quick_action_list.xml b/OsmAnd/res/layout/quick_action_list.xml index 3bc404f17b..66997f59e6 100644 --- a/OsmAnd/res/layout/quick_action_list.xml +++ b/OsmAnd/res/layout/quick_action_list.xml @@ -4,6 +4,12 @@ android:layout_width="match_parent" android:layout_height="match_parent"> + + + android:src="@drawable/ic_action_reorder"/> createMockDada() { List result = new ArrayList<>(); - for (int i = 0; i < 15; i ++){ + for (int i = 0; i < 5; i ++){ result.add(new QuickActionItem(R.string.favorite, R.drawable.ic_action_flag_dark)); + result.add(new QuickActionItem(R.string.poi, R.drawable.ic_action_flag_dark)); + result.add(new QuickActionItem(R.string.map_marker, R.drawable.ic_action_flag_dark)); } return result; } + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + super.onCreateOptionsMenu(menu, inflater); + } - public class QuickActionAdapter extends RecyclerView.Adapter { - private static final int SCREEN_ITEM_TYPE = 1; - private static final int SCREEN_TITLE_TYPE = 2; + public class QuickActionAdapter extends RecyclerView.Adapter implements QuickActionItemTouchHelperCallback.OnItemMoveCallback { + public static final int SCREEN_ITEM_TYPE = 1; + public static final int SCREEN_HEADER_TYPE = 2; private static final int ITEMS_IN_GROUP = 6; private List itemsList = new ArrayList<>(); + private final OnStartDragListener onStartDragListener; + + public QuickActionAdapter(OnStartDragListener onStartDragListener){ + this.onStartDragListener = onStartDragListener; + } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { @@ -85,16 +131,29 @@ public class QuickActionListFragment extends BaseOsmAndFragment { QuickActionItem item = itemsList.get(position); if (viewType == SCREEN_ITEM_TYPE) { - QuickActionItemVH itemVH = (QuickActionItemVH) holder; - itemVH.icon.setImageResource(item.getDrawableRes()); + final QuickActionItemVH itemVH = (QuickActionItemVH) holder; + itemVH.title.setText(item.getNameRes()); - itemVH.subTitle.setText("Action " + getActionPosition(position)); //TODO: get proper string + itemVH.subTitle.setText(getResources().getString(R.string.quick_action_item_action, getActionPosition(position))); + + itemVH.icon.setImageDrawable(getMyApplication().getIconsCache().getThemedIcon(item.getDrawableRes())); + itemVH.handleView.setImageDrawable(getMyApplication().getIconsCache().getThemedIcon(R.drawable.ic_action_reorder)); + itemVH.handleView.setOnTouchListener(new View.OnTouchListener() { + @Override + public boolean onTouch(View v, MotionEvent event) { + if (MotionEventCompat.getActionMasked(event) == + MotionEvent.ACTION_DOWN) { + onStartDragListener.onStartDrag(itemVH); + } + return false; + } + }); LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); dividerParams.setMargins(isShortDivider(position) ? dpToPx(56f) : 0, 0, 0, 0); } else { QuickActionHeaderVH headerVH = (QuickActionHeaderVH) holder; - headerVH.headerName.setText("Screen " + (position/(ITEMS_IN_GROUP + 1) + 1)); //TODO: get proper string + headerVH.headerName.setText(getResources().getString(R.string.quick_action_item_action, position/(ITEMS_IN_GROUP + 1) + 1)); } } @@ -105,7 +164,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment { @Override public int getItemViewType(int position) { - return itemsList.get(position).isHeader() ? SCREEN_TITLE_TYPE : SCREEN_ITEM_TYPE; + return itemsList.get(position).isHeader() ? SCREEN_HEADER_TYPE : SCREEN_ITEM_TYPE; } public void deleteItem(int position) { @@ -145,11 +204,36 @@ public class QuickActionListFragment extends BaseOsmAndFragment { ); } + @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(); + + 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; + } + } + public class QuickActionItemVH extends RecyclerView.ViewHolder { public TextView title; public TextView subTitle; public ImageView icon; public View divider; + public ImageView handleView; public QuickActionItemVH(View itemView) { super(itemView); @@ -157,6 +241,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment { subTitle = (TextView) itemView.findViewById(R.id.subtitle); icon = (ImageView) itemView.findViewById(R.id.imageView); divider = itemView.findViewById(R.id.divider); + handleView = (ImageView) itemView.findViewById(R.id.handle_view); itemView.findViewById(R.id.closeImageButton).setOnClickListener(new View.OnClickListener() { @Override @@ -164,6 +249,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment { QuickActionAdapter.this.deleteItem(getAdapterPosition()); } }); + } } @@ -176,4 +262,8 @@ public class QuickActionListFragment extends BaseOsmAndFragment { } } } + + public interface OnStartDragListener { + void onStartDrag(RecyclerView.ViewHolder viewHolder); + } }