【问题标题】:Android - Jetpack NavigationAndroid - 喷气背包导航
【发布时间】:2019-11-07 04:06:39
【问题描述】:

在 Android Studio 中创建一个新项目并使用 Kotlin 语言选择 N​​avigation Drawer Activity 后,导航就像一个魅力一样工作。但是当我使用 Java 语言生成相同的项目时,它不会导航到另一个片段。这有什么问题?谁能帮帮我?

【问题讨论】:

  • 请提供更多信息,例如异常堆栈跟踪、导致问题的代码或您尝试修复的内容
  • 我只是用 Navigation Drawer Activity 和 Java 语言创建了一个新项目,但是当我单击菜单时,它不起作用。我没有添加一些代码或任何东西。这只是一个新项目@SanketBhat
  • @HusniKamal :如果您不提供更多信息,那么任何人都无法说出您到底出了什么问题,而且问题很可能已经结束。如果你不知道相关代码是什么,也许把项目上传到 github 会让人们看到问题。
  • “新建项目后”不清楚?没关系,感谢您的回复:) @Christian

标签: java android android-jetpack-navigation


【解决方案1】:

我也遇到了同样的问题。

我的解决办法是:

修改main_activity.xml:将NavigationView标签移到DrawerLayout的末尾

如何解决这个问题

环境:

  • Android Studio:3.5
  • compileSdkVersion: 29
  • buildToolsVersion:“29.0.0”
  • targetSdkVersion:29

步骤:

  • 进入 Android Studio
    • 文件 -> 新建 -> 新模块 -> 手机和平板电脑模块 -> 下一步 -> 输入“test”作为应用程序/库名称 -> 下一步 -> 导航抽屉活动 -> 下一步 -> 完成
    • 选择模块“test”并点击Run button
  • 进入安卓手机
    • 页面默认显示 HomeFragment
    • 点击menu button滑动抽屉布局
    • 点击菜单中的gallery
    • DrawerLayout 关闭,但片段未更改为 GalleryFragment

解决方案

终于找到了解决办法:

修改 main_activity.xml:将 NavigationView 标签移动到 DrawerLayout 的末尾

android studio生成的默认main_activity.xml是:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>

我们应该这样改变它(将NavigationView移动到DrawerLayout的末尾):

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

这个问题是怎么来的

我们可以通过 DrawerLayout 和 ViewDragHelper 的源码找到原因:

抽屉布局

public class DrawerLayout extends ViewGroup {
    //..........
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getActionMasked();
        //.........
        boolean interceptForTap = false;

        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                final float x = ev.getX();
                final float y = ev.getY();
                if (mScrimOpacity > 0) {
                    /////////////////
                    //
                    //  take a look at: ViewDragHelper.findTopChildUnder(x, y) 
                    //  it finds child from last to first
                    //
                    /////////////////
                    final View child = mLeftDragger.findTopChildUnder((int) x, (int) y);
                    if (child != null && isContentView(child)) {
                        // if child is the contentView of DrawerLayout, intercept it
                        interceptForTap = true;
                    }
                }
                //.........
                break;
            }
            //...........
        }

        return interceptForDrag || interceptForTap || hasPeekingDrawer() || mChildrenCanceledTouch;
    }
    //...........
}

ViewDragHelper

public class ViewDragHelper {
    //.........
    @Nullable
    public View findTopChildUnder(int x, int y) {
        final int childCount = mParentView.getChildCount();
        for (int i = childCount - 1; i >= 0; i--) {
            final View child = mParentView.getChildAt(mCallback.getOrderedChildIndex(i));
            if (x >= child.getLeft() && x < child.getRight()
                    && y >= child.getTop() && y < child.getBottom()) {
                return child;
            }
        }
        return null;
    }
    //........
}

【讨论】:

  • 更新:当我使用 Navigation Drawer Activity 创建一个新项目时,activity_main 已更改,与您的代码 @billy 相同
猜你喜欢
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 2019-01-19
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
相关资源
最近更新 更多