![](/img/trans.png)
[英]How to use jetpack navigation with bottomNavigationView in android java
[英]Android - Jetpack Navigation
在 Android Studio 中使用 Kotlin 語言創建一個新項目和 select Navigation Drawer Activity 后,導航就像一個魅力。 但是當我使用 Java 語言生成相同的項目時,它不會導航到另一個片段。 這有什么問題? 任何人都可以幫助我嗎?
我有同樣的問題。
修改main_activity.xml:將NavigationView標簽移動到DrawerLayout的末尾
環境:
腳步:
Run button
最后,我找到了解決方案:
修改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.