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