Gluon移动应用程序NavigationDrawer后退按钮问题 [英] Gluon mobile app NavigationDrawer back button issue

查看:150
本文介绍了Gluon移动应用程序NavigationDrawer后退按钮问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FXML的多视图项目,该项目是由IntelliJ的Gluon插件生成的.

I'm working with a multiple view project with FXML, generated by the Gluon plugin for IntelliJ.

当我从主视图导航到辅助视图时,按下物理android设备上的后退按钮,该视图将变回主视图.这是正常现象.

When I navigate to the secondary view, coming from the primary view, and I push the back button on my physical android device, the view changes back to the primary view. This is normal behavior.

问题是NavigationDrawer停留在辅助视图上.您可以观察到这一点,因为您将无法导航到辅助视图.当您在导航窗格中按下辅助"按钮时,什么也不会发生. 此行为类似于尝试导航到您当前所在的页面,该页面也不执行任何操作.

The problem is that the NavigationDrawer stays on the secondary view. You can observe this because you won't be able to navigate to the secondary view. When you push secondary in the navigation pane, nothing happens. This behavior is similar to trying to navigate to the page that you're currently on, which does also nothing.

有人知道解决方案吗?这是一个错误吗?因为Gluon不想将所有支持转移到SO上,所以报告错误的最佳方法是什么?

Does anyone know a solution? Is this a bug? What's the best way to report bugs for Gluon because they wan't to move all support to SO?

推荐答案

NavigationDrawer基本上是一个带有项目列表的弹出窗口,其中通常每个项目都允许选择View.

The NavigationDrawer basically is a popup with a list of items, where usually each of these items allows the selection of a View.

如果只能通过抽屉访问视图,则不会有任何问题,因为总是将选定的项目与活动视图相关.

If views can only be accessed through the drawer, then you won't have any issue, given that always the selected item will be related to the active view.

当您通过其他方式(例如后退"按钮)访问视图时,就会出现您遇到的问题.

The issue you are having occurs when you access views by other means, like the back button.

默认情况下,抽屉列表不跟踪活动视图以自动选择相关项目.如果以后再尝试选择一个已选择的项目,则侦听器将不会触发视图切换.

By default, the drawer list doesn't track the active view to auto select the related item. If later, you try to select an item that is already selected, the listener won't fire the switch of views.

尽管这可以由控件在内部完成(传入的版本可能会解决此问题),但很容易实现.

While this could be done internally by the control (incoming versions will probably manage that), it's easy to achieve.

只需在主类上的viewProperty()中添加一个侦听器,并且只要视图改变,就更新抽屉上的所选项目.由于这会触发navigationDrawer.selectedItemProperty()中的更改,因此在更新选择之前,我们需要删除侦听器,然后再次添加.

Just add a listener to viewProperty() on your main class, and whenever the view changes, update the selected item on the drawer. Since this will trigger a change in the navigationDrawer.selectedItemProperty(), before updating the selection, we need to remove the listener, and add it again.

public static final String PRIMARY_VIEW = HOME_VIEW;
public static final String SECONDARY_VIEW = "Secondary View";
public static final String MENU_LAYER = "Side Menu";

private Item primaryItem;
private Item secondaryItem;

private final ChangeListener listener = (obs, oldItem, newItem) -> {
        hideLayer(MENU_LAYER);
        switchView(newItem.equals(primaryItem) ? PRIMARY_VIEW : SECONDARY_VIEW);
    };

@Override
public void init() {
    addViewFactory(PRIMARY_VIEW, () -> new PrimaryView(PRIMARY_VIEW).getView());
    addViewFactory(SECONDARY_VIEW, () -> new SecondaryView(SECONDARY_VIEW).getView());

    NavigationDrawer drawer = new NavigationDrawer();

    primaryItem = new Item("Primary", MaterialDesignIcon.HOME.graphic());
    secondaryItem = new Item("Secondary", MaterialDesignIcon.DASHBOARD.graphic());
    drawer.getItems().addAll(primaryItem, secondaryItem);

    primaryItem.setSelected(true);
    drawer.selectedItemProperty().addListener(listener);

    addLayerFactory(MENU_LAYER, () -> new SidePopupView(drawer));

    viewProperty().addListener((obs, ov, nv) -> {
        drawer.selectedItemProperty().removeListener(listener);
        if (nv.getName().equals(PRIMARY_VIEW)) {
            primaryItem.setSelected(true);
            secondaryItem.setSelected(false);
            drawer.setSelectedItem(primaryItem);
        } else {
            primaryItem.setSelected(false);
            secondaryItem.setSelected(true);
            drawer.setSelectedItem(secondaryItem);
        }
        drawer.selectedItemProperty().addListener(listener);
    });
}

这篇关于Gluon移动应用程序NavigationDrawer后退按钮问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆