使用setOnNavigationItemSelectedListener时,波纹效果BottomNavigationView冻结

时间:2018-06-22 09:30:00

标签: android kotlin material-design bottomnavigationview

当我使用setOnNavigationItemSelectedListener时,BottomNavigationView会因波纹效果而冻结,如下面的屏幕截图所示。而且效果状态保持这样。

我不明白为什么?

我正在使用com.android.support:design:27.1.1

binding = DataBindingUtil
        .setContentView(this, R.layout.activity_main)

bottomBar = binding?.bottomBarNavigation as BottomNavigationView
bottomBar?.setOnNavigationItemSelectedListener {
    when (it.itemId) {
        TabBarObject.TAB_MESSENGER -> replaceFragment(
            MessengerFragment(),
            TabBarObject.TAB_MESSENGER.tabName
        )
        ...
        ...
    }
}

private fun getTabInfo(menuItemId: Int): TabBarObject {
    return when (menuItemId) {
        R.id.tab_messenger -> TabBarObject.TAB_MESSENGER
        ...
        ...
        else -> throw IllegalArgumentException("UNKNOWN TAB BAR TYPE")
    }
}

private fun replaceFragment(fragment: Fragment, tag: String): Boolean {
    val ft = supportFragmentManager.beginTransaction()

    ft.replace(R.id.frame_layout, fragment, tag)
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
    ft.commitAllowingStateLoss()
    return true
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.constraint.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".Activities.MainActivity">

        <FrameLayout
            android:id="@+id/frame_layout"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/bottom_bar_navigation"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_bar_navigation"
            style="@style/BottomNavigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/bottombar_tabs" />

    </android.support.constraint.ConstraintLayout>
</layout>

bottombar_tabs.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/tab_messenger"
        android:icon="@drawable/ic_messenger_bottom_bar"
        android:title="@string/employer.app.bottombar.messenger.title" />
    ...
    ...
</menu>

enter image description here

1 个答案:

答案 0 :(得分:2)

感谢这个问题已经有点老了,我不能肯定地说这对您有帮助。但是,由于尚未得到答复,我将分享我今天处理同一问题时发现的内容。事实证明,是由一些完全不相关的代码(或看起来如此)引起的。

就像您描述的那样,波纹动画有时会卡住。就我而言,导航到一个特定片段时总是卡住。打开该片段后,它运行了一些代码,该代码有时将任务发布到某个视图的处理程序(View.getHandler())。发布任务之前,该函数正在调用Handler.removeCallbacksAndMessages(null)以清除队列。之所以这样做(可能)是因为错误的假设,即队列中唯一的任务将是该函数的较早调用所发布的任务(在这种情况下,为避免多余的工作,这将是有意义的)。

但是,即使您从特定的视图检索处理程序,它也是UI线程上发生的任何事情共享的一个处理程序。因此,如果您碰巧在错误的时间调用了全部清除方法removeCallbacksAndMessages,则淡出导航栏上波纹效果的动画也将被取消。

删除对removeCallbacksAndMessages的调用后,波纹动画总是按预期完成。

您可以在将任务发布到处理程序时使用令牌,从而以更优雅的方式避免此问题,因为这反过来又允许您仅取消具有特定令牌的任务。令牌并非适用于post的所有变体,并且并非适用于所有API版本。

相关问题