簡體   English   中英

Android - 噴氣背包導航

[英]Android - Jetpack Navigation

在 Android Studio 中使用 Kotlin 語言創建一個新項目和 select Navigation Drawer Activity 后,導航就像一個魅力。 但是當我使用 Java 語言生成相同的項目時,它不會導航到另一個片段。 這有什么問題? 任何人都可以幫助我嗎?

我有同樣的問題。

我的解決方案是:

修改main_activity.xml:將NavigationView標簽移動到DrawerLayout的末尾

如何得到這個問題

環境:

  • Android 工作室:3.5
  • compileSdkVersion: 29
  • 構建工具版本:“29.0.0”
  • 目標SDK版本:29

腳步:

  • 進入Android Studio
    • 文件 -> 新建 -> 新模塊 -> 手機和平板電腦模塊 -> 下一步 -> 輸入“test”作為應用程序/庫名稱 -> 下一步 -> 導航抽屜活動 -> 下一步 -> 完成
    • Select 模塊“測試”並單擊Run button
  • 步入android手機
    • 該頁面默認顯示 HomeFragment
    • 單擊menu button以在 DrawerLayout 中滑動菜單
    • 點擊菜單中的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;
    }
    //...........
}

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