【问题标题】:Ripple effect BottomNavigationView freezes when using setOnNavigationItemSelectedListener使用 setOnNavigationItemSelectedListener 时,波纹效果 BottomNavigationView 冻结
【发布时间】:2018-12-01 18:04:09
【问题描述】:

当我使用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>

【问题讨论】:

  • 添加更多上下文,可能是 XML 和/或您设计中的任何自定义内容。从您发布的小 sn-p 来看,这个问题看起来很奇怪。
  • 抱歉,已经完成了。
  • 还有你的`style="@style/BottomNavigation"`(这很可能是问题所在)。此外,您可以将宽度设置为match_parent,因为您不想看到它像那样(我知道您使用的是match_constraint):)

标签: android kotlin material-design bottomnavigationview


【解决方案1】:

感谢这个问题已经有点老了,我不能肯定这是否会对你有所帮助。但是由于还没有答案,我将分享我今天在处理相同问题时的发现。事实证明,是一些完全不相关的代码(或者看起来如此)导致了这个问题。

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

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

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

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

【讨论】:

    猜你喜欢
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 2011-11-30
    相关资源
    最近更新 更多