我实际上在我的应用程序中使用了新的架构组件,并且我已经设置了导航组件。我有一个导航抽屉,我想和它一起使用。我已经设置好了,但我面临一些问题:
1 - 抽屉不会自行关闭。菜单工作并导航到正确的位置,但导航后它不会关闭它。我不得不添加一个 destinationChangedListener 来自己关闭它。
navController.addOnDestinationChangedListener { _, _, _ ->
if (drawer_layout.isDrawerOpen(GravityCompat.START))
drawer_layout.closeDrawer(GravityCompat.START)
}
Run Code Online (Sandbox Code Playgroud)
在codelabs 中,抽屉会自动关闭,我真的不明白为什么。
2 - 向上按钮打开抽屉。当我导航到一个非顶级片段时,菜单图标变为向上箭头,但是当我点击它时,它会打开抽屉菜单而不是回到上一个目的地。
3 我想为我的菜单中的某些项目设置点击自定义。我菜单上的所有按钮都不是用于导航到应用程序的,我不知道在哪里可以覆盖 navController 的行为来告诉它在无法导航时该怎么做。
这是相关的代码:
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/layout_app_bar"/>
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_marginTop="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/app_navigation" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<include layout="@layout/layout_navigation_view"/>
</androidx.drawerlayout.widget.DrawerLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
Layout_app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:theme="@style/AppTheme.AppBarOverlay"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/language_main_color"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout> …Run Code Online (Sandbox Code Playgroud) android navigation-drawer android-architecture-components android-architecture-navigation
我尝试在回收器视图的一个项目上实现动画,自定义滑动,为此,我尝试使用 MotionLayout。实际上,如果动画的触发是单击,那么效果很好,但是当我想进行滑动时,它会干扰回收器视图的滑动。这是我的场景:
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetStart="@layout/chat_item_start"
motion:constraintSetEnd="@layout/chat_item_end"
motion:duration="500">
<OnClick
motion:targetId="@+id/background"
motion:clickAction="toggle" />
<!-- <OnSwipe
motion:touchAnchorId="@+id/background"
motion:touchAnchorSide="right"
motion:dragDirection="dragLeft" /> -->
</Transition>
</MotionScene>
Run Code Online (Sandbox Code Playgroud)
看看我一开始的清单:
最后,当我点击一个项目时:
当我切换到 OnSwipe 触发器时,动画会起作用,但只有当我严格水平滑动时,如果我的手指错误地向上或向下移动一点,动画就会停止,甚至不会自行完成:
我认为问题的出现是因为回收器视图还处理滑动手势,因为当动画停止时(如果我稍微向上/向下)回收器会像平常一样移动。
如果我过多地滑动,我会收到此错误:
java.lang.NullPointerException:尝试在 androidx.constraintlayout.motion.widget.MotionLayout$MyTracker.addMovement(MotionLayout. java:985) 在 androidx.constraintlayout.motion.widget.MotionScene.processTouchEvent(MotionScene.java:1043) 在 androidx.constraintlayout.motion.widget.MotionLayout.onTouchEvent(MotionLayout.java:2992) 在 android.view.View.dispatchTouchEvent (View.java:12515)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3024)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2705)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java: 3030)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2662)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2662)在android。 view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2662) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) 在 android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2662) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2662) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java: 3030) 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2662) 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2662) 在 com. android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:444) 在 com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1829) 在 android.app.Activity.dispatchTouchEvent(Activity.java:3397)在 androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69) 在 com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:402) 在 android.view.View.dispatchPointerEvent(View.java:12754) )在 android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5052) 在 android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4855) 在 …
android android-animation android-recyclerview android-motionlayout