简体   繁体   中英

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. But when I generate the same project with Java language, it won't navigate to another fragment. 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

How to get this issue

environment:

  • Android Studio: 3.5
  • compileSdkVersion: 29
  • buildToolsVersion: "29.0.0"
  • targetSdkVersion: 29

steps:

  • Step in Android Studio
    • File -> new -> new Module -> Phone & Tablet Module -> Next -> input "test" as Application/Library name -> Next -> Navigation Drawer Activity -> Next - > Finish
    • Select module "test" and Click Run button
  • Step in android phone
    • the page is showing HomeFragment by default
    • click menu button to slide in the DrawerLayout菜单
    • click gallery in the menu 画廊
    • the DrawerLayout closed but fragment has not change to GalleryFragment

Solution

Finally, i found the solution:

Modify main_activity.xml: move NavigationView tag to the end of DrawerLayout

android studio generated the default main_activity.xml is:

<?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 ):

<?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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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