简体   繁体   English

Android - 喷气背包导航

[英]Android - Jetpack Navigation

After creating a new project and select Navigation Drawer Activity using Kotlin language in Android Studio, the navigation is working like a charm.在 Android Studio 中使用 Kotlin 语言创建一个新项目和 select Navigation Drawer Activity 后,导航就像一个魅力。 But when I generate the same project with Java language, it won't navigate to another fragment.但是当我使用 Java 语言生成相同的项目时,它不会导航到另一个片段。 What's wrong with this?这有什么问题? Anyone can help me?任何人都可以帮助我吗?

I have got the same issue.我有同样的问题。

My solution is:我的解决方案是:

Modify main_activity.xml: move NavigationView tag to the end of DrawerLayout修改main_activity.xml:将NavigationView标签移动到DrawerLayout的末尾

How to get this issue如何得到这个问题

environment:环境:

  • Android Studio: 3.5 Android 工作室:3.5
  • compileSdkVersion: 29 compileSdkVersion: 29
  • buildToolsVersion: "29.0.0"构建工具版本:“29.0.0”
  • targetSdkVersion: 29目标SDK版本:29

steps:脚步:

  • Step in Android Studio进入Android Studio
    • File -> new -> new Module -> Phone & Tablet Module -> Next -> input "test" as Application/Library name -> Next -> Navigation Drawer Activity -> Next - > Finish文件 -> 新建 -> 新模块 -> 手机和平板电脑模块 -> 下一步 -> 输入“test”作为应用程序/库名称 -> 下一步 -> 导航抽屉活动 -> 下一步 -> 完成
    • Select module "test" and Click Run button Select 模块“测试”并单击Run button
  • Step in android phone步入android手机
    • the page is showing HomeFragment by default该页面默认显示 HomeFragment
    • click menu button to slide in the DrawerLayout单击menu button以在 DrawerLayout 中滑动菜单
    • click gallery in the menu点击菜单中的gallery 画廊
    • the DrawerLayout closed but fragment has not change to GalleryFragment DrawerLayout 关闭但片段未更改为 GalleryFragment

Solution解决方案

Finally, i found the solution:最后,我找到了解决方案:

Modify main_activity.xml: move NavigationView tag to the end of DrawerLayout修改main_activity.xml:将NavigationView标签移动到DrawerLayout的末尾

android studio generated the default main_activity.xml is: 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>

we should change it like this(move NavigationView to the end of 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>

How this issue comes这个问题是怎么来的

We can find the reason with source code of DrawerLayout and ViewDragHelper:我们可以通过 DrawerLayout 和 ViewDragHelper 的源码找到原因:

DrawerLayout抽屉布局

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查看DragHelper

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;
    }
    //........
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM