Search in sources :

Example 6 with ReorderItemTouchHelperCallback

use of net.osmand.plus.views.controls.ReorderItemTouchHelperCallback in project Osmand by osmandapp.

the class ConfigureMenuItemsFragment method onActivityCreated.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    instantiateContextMenuAdapter();
    if (menuItemsOrder.isEmpty()) {
        for (ContextMenuItem item : getCustomizableDefaultItems(contextMenuAdapter.getDefaultItems())) {
            menuItemsOrder.put(item.getId(), item.getOrder());
        }
    }
    if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) {
        initMainActionsIds(appMode);
    }
    recyclerView.setPadding(0, 0, 0, (int) app.getResources().getDimension(R.dimen.dialog_button_ex_min_width));
    rearrangeAdapter = new RearrangeMenuItemsAdapter(app, getAdapterItems(), nightMode);
    recyclerView.setLayoutManager(new LinearLayoutManager(app));
    final ItemTouchHelper touchHelper = new ItemTouchHelper(new ReorderItemTouchHelperCallback(rearrangeAdapter));
    touchHelper.attachToRecyclerView(recyclerView);
    MenuItemsAdapterListener listener = new MenuItemsAdapterListener() {

        private int fromPosition;

        private int toPosition;

        @Override
        public void onDragStarted(RecyclerView.ViewHolder holder) {
            fromPosition = holder.getAdapterPosition();
            touchHelper.startDrag(holder);
        }

        @Override
        public void onDragOrSwipeEnded(RecyclerView.ViewHolder holder) {
            if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) {
                mainActionItems = rearrangeAdapter.getMainActionsIds();
            }
            toPosition = holder.getAdapterPosition();
            if (toPosition >= 0 && fromPosition >= 0 && toPosition != fromPosition) {
                rearrangeAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onButtonClicked(int position) {
            RearrangeMenuAdapterItem rearrangeMenuAdapterItem = rearrangeAdapter.getItem(position);
            if (rearrangeMenuAdapterItem.getValue() instanceof ContextMenuItem) {
                ContextMenuItem menuItemBase = (ContextMenuItem) rearrangeMenuAdapterItem.getValue();
                menuItemBase.setHidden(!menuItemBase.isHidden());
                String id = menuItemBase.getId();
                if (menuItemBase.isHidden()) {
                    hiddenMenuItems.add(id);
                    if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) {
                        mainActionItems.remove(id);
                    }
                } else {
                    hiddenMenuItems.remove(id);
                    if (screenType == ScreenType.CONTEXT_MENU_ACTIONS && mainActionItems.size() < MAIN_BUTTONS_QUANTITY) {
                        mainActionItems.add(id);
                    }
                }
                wasReset = false;
                isChanged = true;
                rearrangeAdapter.updateItems(getAdapterItems());
            }
        }

        @Override
        public void onItemMoved(String id, int position) {
            menuItemsOrder.put(id, position);
            wasReset = false;
            isChanged = true;
        }
    };
    rearrangeAdapter.setListener(listener);
    recyclerView.setAdapter(rearrangeAdapter);
}
Also used : ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper) ReorderItemTouchHelperCallback(net.osmand.plus.views.controls.ReorderItemTouchHelperCallback) ContextMenuItem(net.osmand.plus.ContextMenuItem) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) MenuItemsAdapterListener(net.osmand.plus.settings.fragments.RearrangeMenuItemsAdapter.MenuItemsAdapterListener) RearrangeMenuAdapterItem(net.osmand.plus.settings.fragments.RearrangeMenuItemsAdapter.RearrangeMenuAdapterItem)

Aggregations

ItemTouchHelper (androidx.recyclerview.widget.ItemTouchHelper)6 ReorderItemTouchHelperCallback (net.osmand.plus.views.controls.ReorderItemTouchHelperCallback)6 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)5 RecyclerView (androidx.recyclerview.widget.RecyclerView)5 View (android.view.View)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 Nullable (androidx.annotation.Nullable)3 OsmandApplication (net.osmand.plus.OsmandApplication)3 SuppressLint (android.annotation.SuppressLint)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ApplicationMode (net.osmand.plus.settings.backend.ApplicationMode)2 SpannableString (android.text.SpannableString)1 Button (android.widget.Button)1 ImageButton (android.widget.ImageButton)1 SwitchCompat (androidx.appcompat.widget.SwitchCompat)1 FragmentActivity (androidx.fragment.app.FragmentActivity)1 AppBarLayout (com.google.android.material.appbar.AppBarLayout)1 Snackbar (com.google.android.material.snackbar.Snackbar)1