Reorder and delete profiles screen second part

This commit is contained in:
Vitaliy 2019-12-17 16:52:43 +02:00
parent c3c65068e1
commit 8a1d666bf6
6 changed files with 441 additions and 172 deletions

View file

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/wikivoyage_search_list_header_height"
android:orientation="vertical"
android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding">
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:letterSpacing="@dimen/description_letter_spacing"
android:paddingTop="@dimen/list_header_settings_top_margin"
android:singleLine="true"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/default_desc_text_size"
osmand:typeface="@string/font_roboto_medium"
tools:text="@string/plugin_settings" />
</LinearLayout>

View file

@ -730,7 +730,7 @@ public class ApplicationMode {
}
}
public static void saveAppModesToSettings(OsmandSettings settings) {
public static void saveAppModesToSettings(OsmandApplication app) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
List<ApplicationModeBean> defaultModeBeans = createApplicationModeBeans(defaultValues);
@ -739,8 +739,8 @@ public class ApplicationMode {
String defaultProfiles = gson.toJson(defaultModeBeans);
String customProfiles = gson.toJson(customModeBeans);
settings.DEFAULT_APP_PROFILES.set(defaultProfiles);
settings.CUSTOM_APP_PROFILES.set(customProfiles);
app.getSettings().DEFAULT_APP_PROFILES.set(defaultProfiles);
app.getSettings().CUSTOM_APP_PROFILES.set(customProfiles);
}
private static void saveAppModesToSettings(OsmandSettings settings, boolean saveCustomModes) {
@ -807,6 +807,13 @@ public class ApplicationMode {
saveAppModesToSettings(app.getSettings(), md.isCustomProfile());
}
public static void deleteCustomModes(List<ApplicationMode> modes, OsmandApplication app) {
values.removeAll(modes);
customValues.removeAll(modes);
cachedFilteredValues.removeAll(modes);
saveAppModesToSettings(app.getSettings(), true);
}
public static boolean changeProfileAvailability(ApplicationMode mode, boolean isSelected, OsmandApplication app) {
Set<ApplicationMode> selectedModes = new LinkedHashSet<>(ApplicationMode.values(app));
StringBuilder vls = new StringBuilder(ApplicationMode.DEFAULT.getStringKey() + ",");

View file

@ -1,34 +1,66 @@
package net.osmand.plus.profiles;
import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.settings.BaseSettingsFragment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
public class EditProfilesFragment extends BaseOsmAndFragment {
private static String DELETED_APP_MODES_KEY = "deleted_app_modes_key";
private static String APP_MODES_ORDER_KEY = "app_modes_order_key";
private List<Object> items = new ArrayList<>();
private HashMap<String, Integer> appModesOrders = new HashMap<>();
private ArrayList<String> deletedModesKeys = new ArrayList<>();
private EditProfilesAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
MapActivity mapActivity = (MapActivity) getActivity();
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final MapActivity mapActivity = (MapActivity) requireActivity();
if (savedInstanceState != null && savedInstanceState.containsKey(APP_MODES_ORDER_KEY) && savedInstanceState.containsKey(DELETED_APP_MODES_KEY)) {
appModesOrders = (HashMap<String, Integer>) savedInstanceState.getSerializable(APP_MODES_ORDER_KEY);
deletedModesKeys = savedInstanceState.getStringArrayList(DELETED_APP_MODES_KEY);
} else {
List<ApplicationMode> appModes = ApplicationMode.allPossibleValues();
for (int i = 0; i < appModes.size(); i++) {
ApplicationMode mode = appModes.get(i);
appModesOrders.put(mode.getStringKey(), i);
}
}
View mainView = inflater.inflate(R.layout.edit_profiles_list_fragment, container, false);
AndroidUtils.addStatusBarPadding21v(getContext(), mainView);
@ -50,12 +82,13 @@ public class EditProfilesFragment extends BaseOsmAndFragment {
RecyclerView recyclerView = mainView.findViewById(R.id.profiles_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
final List<ApplicationMode> applicationModes = new ArrayList<>(ApplicationMode.allPossibleValues());
final ProfilesAdapter adapter = new ProfilesAdapter(mapActivity, applicationModes);
adapter = new EditProfilesAdapter(mapActivity);
updateItems();
final ItemTouchHelper touchHelper = new ItemTouchHelper(new ReorderItemTouchHelperCallback(adapter));
touchHelper.attachToRecyclerView(recyclerView);
adapter.setAdapterListener(new ProfilesAdapter.ProfilesAdapterListener() {
adapter.setAdapterListener(new ProfilesAdapterListener() {
private int fromPosition;
private int toPosition;
@ -73,6 +106,21 @@ public class EditProfilesFragment extends BaseOsmAndFragment {
adapter.notifyDataSetChanged();
}
}
@Override
public void onButtonClicked(int pos) {
Object item = adapter.getItem(pos);
if (item instanceof EditProfileDataObject) {
EditProfileDataObject profileDataObject = (EditProfileDataObject) item;
profileDataObject.toggleDeleted();
if (profileDataObject.deleted) {
deletedModesKeys.add(profileDataObject.getStringKey());
} else {
deletedModesKeys.remove(profileDataObject.getStringKey());
}
updateItems();
}
}
});
recyclerView.setAdapter(adapter);
@ -96,13 +144,32 @@ public class EditProfilesFragment extends BaseOsmAndFragment {
public void onClick(View v) {
MapActivity mapActivity = (MapActivity) getActivity();
if (mapActivity != null) {
OsmandSettings settings = mapActivity.getMyApplication().getSettings();
for (int i = 0; i < applicationModes.size(); i++) {
ApplicationMode mode = applicationModes.get(i);
mode.setOrder(i);
OsmandApplication app = mapActivity.getMyApplication();
OsmandSettings settings = app.getSettings();
if (!deletedModesKeys.isEmpty()) {
List<ApplicationMode> deletedModes = new ArrayList<>();
for (String modeKey : deletedModesKeys) {
ApplicationMode mode = ApplicationMode.valueOfStringKey(modeKey, null);
if (mode != null) {
deletedModes.add(mode);
}
}
ApplicationMode.deleteCustomModes(deletedModes, app);
if (deletedModes.contains(settings.APPLICATION_MODE.get())) {
settings.APPLICATION_MODE.resetToDefault();
}
}
for (ApplicationMode mode : ApplicationMode.allPossibleValues()) {
String modeKey = mode.getStringKey();
Integer order = appModesOrders.get(modeKey);
if (order == null) {
order = mode.getOrder();
}
mode.setOrder(order);
}
ApplicationMode.reorderAppModes();
ApplicationMode.saveAppModesToSettings(settings);
ApplicationMode.saveAppModesToSettings(app);
mapActivity.onBackPressed();
}
}
@ -110,4 +177,319 @@ public class EditProfilesFragment extends BaseOsmAndFragment {
return mainView;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(APP_MODES_ORDER_KEY, appModesOrders);
outState.putStringArrayList(DELETED_APP_MODES_KEY, deletedModesKeys);
}
public List<EditProfileDataObject> getProfiles(boolean deleted) {
List<EditProfileDataObject> profiles = new ArrayList<>();
for (ApplicationMode mode : ApplicationMode.allPossibleValues()) {
String modeKey = mode.getStringKey();
if (deleted && deletedModesKeys.contains(modeKey) || !deleted && !deletedModesKeys.contains(modeKey)) {
Integer order = appModesOrders.get(modeKey);
if (order == null) {
order = mode.getOrder();
}
profiles.add(new EditProfileDataObject(modeKey, mode.toHumanString(getContext()), BaseSettingsFragment.getAppModeDescription(getContext(), mode),
mode.getIconRes(), false, mode.isCustomProfile(), deleted, mode.getIconColorInfo(), order));
}
}
Collections.sort(profiles, new Comparator<EditProfileDataObject>() {
@Override
public int compare(EditProfileDataObject o1, EditProfileDataObject o2) {
return (o1.order < o2.order) ? -1 : ((o1.order == o2.order) ? 0 : 1);
}
});
return profiles;
}
private void updateItems() {
List<EditProfileDataObject> activeObjects = getProfiles(false);
List<EditProfileDataObject> deletedObjects = getProfiles(true);
items.clear();
items.add(getString(R.string.edit_profiles_descr));
items.addAll(activeObjects);
items.add(new SpannableString(getString(R.string.shared_string_deleted)));
items.add(getString(R.string.delete_profiles_descr));
items.addAll(deletedObjects);
adapter.setItems(items);
}
public class EditProfileDataObject extends ProfileDataObject {
private int order;
private boolean deleted;
private boolean customProfile;
EditProfileDataObject(String stringKey, String name, String descr, int iconRes, boolean isSelected, boolean customProfile, boolean deleted, ApplicationMode.ProfileIconColors iconColor, int order) {
super(name, descr, stringKey, iconRes, isSelected, iconColor);
this.customProfile = customProfile;
this.deleted = deleted;
this.order = order;
}
public boolean isDeleted() {
return deleted;
}
public boolean isCustomProfile() {
return customProfile;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public void toggleDeleted() {
this.deleted = !deleted;
}
}
private class EditProfilesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements ReorderItemTouchHelperCallback.OnItemMoveCallback {
private static final int INFO_TYPE = 0;
private static final int PROFILE_EDIT_TYPE = 1;
private static final int CATEGORY_TYPE = 3;
private OsmandApplication app;
private UiUtilities uiUtilities;
private List<Object> items = new ArrayList<>();
private ProfilesAdapterListener listener;
private boolean nightMode;
EditProfilesAdapter(MapActivity mapActivity) {
setHasStableIds(true);
app = mapActivity.getMyApplication();
uiUtilities = app.getUIUtilities();
nightMode = !app.getSettings().isLightContent();
}
public void setItems(List<Object> items) {
this.items = items;
notifyDataSetChanged();
}
public void setAdapterListener(ProfilesAdapterListener listener) {
this.listener = listener;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = UiUtilities.getInflater(viewGroup.getContext(), nightMode);
if (viewType == INFO_TYPE) {
View itemView = inflater.inflate(R.layout.bottom_sheet_item_description_long, viewGroup, false);
return new InfoViewHolder(itemView);
} else if (viewType == PROFILE_EDIT_TYPE) {
View itemView = inflater.inflate(R.layout.profile_edit_list_item, viewGroup, false);
return new ProfileViewHolder(itemView);
} else if (viewType == CATEGORY_TYPE) {
View itemView = inflater.inflate(R.layout.simple_category_item, viewGroup, false);
return new CategoryViewHolder(itemView);
} else {
throw new IllegalArgumentException("Unsupported view type");
}
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int pos) {
if (holder instanceof ProfileViewHolder) {
ProfileViewHolder profileViewHolder = (ProfileViewHolder) holder;
EditProfileDataObject mode = (EditProfileDataObject) items.get(pos);
profileViewHolder.title.setText(mode.getName());
profileViewHolder.description.setText(mode.getDescription());
int iconRes = mode.getIconRes();
if (iconRes == 0 || iconRes == -1) {
iconRes = R.drawable.ic_action_world_globe;
}
int profileColorResId = mode.getIconColor(nightMode);
int colorNoAlpha = ContextCompat.getColor(app, profileColorResId);
profileViewHolder.icon.setImageDrawable(uiUtilities.getIcon(iconRes, profileColorResId));
//set up cell color
Drawable drawable = UiUtilities.getColoredSelectableDrawable(app, colorNoAlpha, 0.3f);
AndroidUtils.setBackground(profileViewHolder.itemsContainer, drawable);
if (mode.isCustomProfile()) {
profileViewHolder.actionIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
listener.onButtonClicked(pos);
}
}
});
}
profileViewHolder.moveIcon.setVisibility(mode.isDeleted() ? View.GONE : View.VISIBLE);
if (!mode.isDeleted()) {
int removeIconColor = mode.isCustomProfile() ? R.color.color_osm_edit_delete : R.color.icon_color_default_light;
profileViewHolder.actionIcon.setImageDrawable(uiUtilities.getIcon(R.drawable.ic_action_remove, removeIconColor));
profileViewHolder.moveIcon.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
listener.onDragStarted(holder);
}
return false;
}
});
} else {
profileViewHolder.actionIcon.setImageDrawable(uiUtilities.getIcon(R.drawable.ic_action_undo, R.color.color_osm_edit_create));
}
} else if (holder instanceof InfoViewHolder) {
InfoViewHolder infoViewHolder = (InfoViewHolder) holder;
String text = (String) items.get(pos);
infoViewHolder.description.setText(text);
} else if (holder instanceof CategoryViewHolder) {
CategoryViewHolder infoViewHolder = (CategoryViewHolder) holder;
SpannableString text = (SpannableString) items.get(pos);
infoViewHolder.title.setText(text);
}
}
@Override
public int getItemViewType(int position) {
Object item = items.get(position);
if (item instanceof EditProfileDataObject) {
return PROFILE_EDIT_TYPE;
} else if (item instanceof String) {
return INFO_TYPE;
} else if (item instanceof SpannableString) {
return CATEGORY_TYPE;
} else {
throw new IllegalArgumentException("Unsupported view type");
}
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public boolean onItemMove(int from, int to) {
Object itemFrom = getItem(from);
Object itemTo = getItem(to);
if (itemFrom instanceof EditProfileDataObject && itemTo instanceof EditProfileDataObject) {
EditProfileDataObject profileDataObjectFrom = (EditProfileDataObject) itemFrom;
EditProfileDataObject profileDataObjectTo = (EditProfileDataObject) itemTo;
int tmp = profileDataObjectFrom.getOrder();
appModesOrders.put(profileDataObjectFrom.getStringKey(), profileDataObjectTo.getOrder());
appModesOrders.put(profileDataObjectTo.getStringKey(), tmp);
Collections.swap(items, from, to);
notifyItemMoved(from, to);
return true;
}
return false;
}
@Override
public long getItemId(int position) {
Object item = items.get(position);
if (item instanceof EditProfileDataObject) {
return ((EditProfileDataObject) item).getStringKey().hashCode();
}
return item.hashCode();
}
public Object getItem(int position) {
return items.get(position);
}
@Override
public void onItemDismiss(RecyclerView.ViewHolder holder) {
listener.onDragOrSwipeEnded(holder);
}
private class ProfileViewHolder extends RecyclerView.ViewHolder implements ReorderItemTouchHelperCallback.UnmovableItem {
TextView title;
TextView description;
ImageView icon;
ImageButton actionIcon;
ImageView moveIcon;
View itemsContainer;
ProfileViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
description = itemView.findViewById(R.id.description);
actionIcon = itemView.findViewById(R.id.action_icon);
icon = itemView.findViewById(R.id.icon);
moveIcon = itemView.findViewById(R.id.move_icon);
itemsContainer = itemView.findViewById(R.id.selectable_list_item);
}
@Override
public boolean isMovingDisabled() {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
Object item = items.get(position);
if (item instanceof EditProfileDataObject) {
return ((EditProfileDataObject) item).isDeleted();
}
}
return false;
}
}
private class InfoViewHolder extends RecyclerView.ViewHolder implements ReorderItemTouchHelperCallback.UnmovableItem {
private TextView description;
InfoViewHolder(View itemView) {
super(itemView);
description = itemView.findViewById(R.id.description);
}
@Override
public boolean isMovingDisabled() {
return true;
}
}
private class CategoryViewHolder extends RecyclerView.ViewHolder implements ReorderItemTouchHelperCallback.UnmovableItem {
private TextView title;
CategoryViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
}
@Override
public boolean isMovingDisabled() {
return true;
}
}
}
public interface ProfilesAdapterListener {
void onDragStarted(RecyclerView.ViewHolder holder);
void onDragOrSwipeEnded(RecyclerView.ViewHolder holder);
void onButtonClicked(int view);
}
}

View file

@ -1,148 +0,0 @@
package net.osmand.plus.profiles;
import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
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;
import android.widget.ImageView;
import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.settings.BaseSettingsFragment;
import java.util.Collections;
import java.util.List;
public class ProfilesAdapter extends RecyclerView.Adapter<ProfilesAdapter.ProfileViewHolder>
implements ReorderItemTouchHelperCallback.OnItemMoveCallback {
private OsmandApplication app;
private UiUtilities uiUtilities;
private List<ApplicationMode> applicationModes;
private ProfilesAdapterListener listener;
private boolean nightMode;
public ProfilesAdapter(MapActivity mapActivity, List<ApplicationMode> appModes) {
setHasStableIds(true);
app = mapActivity.getMyApplication();
uiUtilities = app.getUIUtilities();
applicationModes = appModes;
nightMode = !mapActivity.getMyApplication().getSettings().isLightContent();
}
public void setAdapterListener(ProfilesAdapterListener listener) {
this.listener = listener;
}
@NonNull
@Override
public ProfileViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater themedInflater = UiUtilities.getInflater(viewGroup.getContext(), nightMode);
View itemView = themedInflater.inflate(R.layout.profile_edit_list_item, viewGroup, false);
return new ProfileViewHolder(itemView);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(final ProfileViewHolder holder, final int pos) {
ApplicationMode mode = applicationModes.get(pos);
holder.title.setText(mode.toHumanString(app));
holder.description.setText(BaseSettingsFragment.getAppModeDescription(app, mode));
updateViewHolder(holder, mode);
holder.moveIcon.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
listener.onDragStarted(holder);
}
return false;
}
});
}
@Override
public int getItemCount() {
return applicationModes.size();
}
@Override
public boolean onItemMove(int from, int to) {
Collections.swap(applicationModes, from, to);
notifyItemMoved(from, to);
return true;
}
@Override
public long getItemId(int position) {
return applicationModes.get(position).hashCode();
}
@Override
public void onItemDismiss(RecyclerView.ViewHolder holder) {
listener.onDragOrSwipeEnded(holder);
}
private void updateViewHolder(ProfileViewHolder holder, ApplicationMode mode) {
int iconRes = mode.getIconRes();
if (iconRes == 0 || iconRes == -1) {
iconRes = R.drawable.ic_action_world_globe;
}
int profileColorResId = mode.getIconColorInfo().getColor(nightMode);
int colorNoAlpha = ContextCompat.getColor(app, profileColorResId);
int removeIconColor = mode.isCustomProfile() ? R.color.color_osm_edit_delete : R.color.icon_color_default_light;
holder.icon.setImageDrawable(uiUtilities.getIcon(iconRes, profileColorResId));
holder.actionIcon.setImageDrawable(uiUtilities.getIcon(R.drawable.ic_action_remove, removeIconColor));
//set up cell color
Drawable drawable = UiUtilities.getColoredSelectableDrawable(app, colorNoAlpha, 0.3f);
AndroidUtils.setBackground(holder.itemsContainer, drawable);
}
public ApplicationMode getItem(int position) {
return applicationModes.get(position);
}
public interface ProfilesAdapterListener {
void onDragStarted(RecyclerView.ViewHolder holder);
void onDragOrSwipeEnded(RecyclerView.ViewHolder holder);
}
class ProfileViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView description;
ImageView icon;
ImageView moveIcon;
ImageButton actionIcon;
View itemsContainer;
public ProfileViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
description = itemView.findViewById(R.id.description);
actionIcon = itemView.findViewById(R.id.action_icon);
icon = itemView.findViewById(R.id.icon);
moveIcon = itemView.findViewById(R.id.move_icon);
itemsContainer = itemView.findViewById(R.id.selectable_list_item);
}
}
}

View file

@ -2,7 +2,6 @@ package net.osmand.plus.profiles;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
public class ReorderItemTouchHelperCallback extends ItemTouchHelper.Callback {
@ -25,7 +24,7 @@ public class ReorderItemTouchHelperCallback extends ItemTouchHelper.Callback {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (isImmobileViewHolder(viewHolder)) {
if (isMovingDisabled(viewHolder)) {
return 0;
}
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
@ -38,14 +37,14 @@ public class ReorderItemTouchHelperCallback extends ItemTouchHelper.Callback {
int from = source.getAdapterPosition();
int to = target.getAdapterPosition();
if (from == RecyclerView.NO_POSITION || to == RecyclerView.NO_POSITION
|| isImmobileViewHolder(source) || isImmobileViewHolder(target)) {
|| isMovingDisabled(source) || isMovingDisabled(target)) {
return false;
}
return itemMoveCallback.onItemMove(from, to);
}
private boolean isImmobileViewHolder(RecyclerView.ViewHolder viewHolder) {
return viewHolder instanceof ImmobileViewHolder;
private boolean isMovingDisabled(RecyclerView.ViewHolder viewHolder) {
return viewHolder instanceof UnmovableItem && ((UnmovableItem) viewHolder).isMovingDisabled();
}
@Override
@ -66,10 +65,8 @@ public class ReorderItemTouchHelperCallback extends ItemTouchHelper.Callback {
void onItemDismiss(RecyclerView.ViewHolder holder);
}
public static class ImmobileViewHolder extends RecyclerView.ViewHolder {
public interface UnmovableItem {
public ImmobileViewHolder(View itemView) {
super(itemView);
}
boolean isMovingDisabled();
}
}

View file

@ -29,6 +29,8 @@ import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.profiles.ReorderItemTouchHelperCallback;
import net.osmand.plus.profiles.ReorderItemTouchHelperCallback.UnmovableItem;
import net.osmand.plus.profiles.ReorderItemTouchHelperCallback.OnItemMoveCallback;
import java.util.ArrayList;
import java.util.Collections;
@ -188,7 +190,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
adapter.addItems(quickActionRegistry.getFilteredQuickActions());
}
public class QuickActionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ReorderItemTouchHelperCallback.OnItemMoveCallback {
public class QuickActionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnItemMoveCallback {
public static final int SCREEN_ITEM_TYPE = 1;
public static final int SCREEN_HEADER_TYPE = 2;
@ -412,7 +414,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
}
}
public class QuickActionHeaderVH extends ReorderItemTouchHelperCallback.ImmobileViewHolder {
public class QuickActionHeaderVH extends RecyclerView.ViewHolder implements UnmovableItem {
public TextView headerName;
@ -420,6 +422,11 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
super(itemView);
headerName = (TextView) itemView.findViewById(R.id.header);
}
@Override
public boolean isMovingDisabled() {
return true;
}
}
}