【问题标题】:Bottom Sheet doesn't appear sometimes in complicated view底页有时不会出现在复杂视图中
【发布时间】:2016-05-13 17:49:05
【问题描述】:

这个 GIF 几乎显示了我在说什么:

我的应用中有一个相当复杂的 DrawerLayout -> CoordinatorLayout -> ViewPager -> Fragment -> RecyclerView 情况。

当用户长按 RecyclerView 中的某个项目时,它会向 ParentActivity 发送一个广播,然后它会发出 BottomSheet 出现的信号。

通常它可以正常工作,但每隔一段时间,BottomSheet 就不会出现。我有一个 FrameLayout,它充当 ParentActivity 和 ViewPager 之间的叠加层。当您单击 Overlay 时,它会告诉 BottomSheet 折叠。您可以在上面的 GIF 中看到,在没有出现 BottomSheet 的情况下,它仍然会在折叠时进行动画处理。

这种行为在我的 Nexus 5 Genymotion (API 21) 模拟器上经常发生。在真实设备上它几乎不会发生,但有时会发生。

我注意到的是,如果我长按并按住直到出现 BottomSheet,它总是会出现。当我在注册长按后立即释放时,BottomSheet 可能不会出现。

我相当肯定这是 RecyclerView 以某种方式干扰的情况,但我无法真正弄清楚为什么或如何。任何调试提示将不胜感激。

这里是有问题的布局供参考:

<android.support.v4.widget.DrawerLayout android:id="@+id/drawer_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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gray"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="@color/green"
                        android:elevation="4dp"
                        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

                    <android.support.design.widget.TabLayout
                        android:id="@+id/budget_display_mode_tabs"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/green"
                        android:elevation="4dp"
                        android:visibility="gone"
                        app:tabGravity="fill"
                        app:tabIndicatorColor="@color/green"
                        app:tabMode="fixed" />

                </LinearLayout>

            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" >

            <LinearLayout
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/gray"
                android:orientation="vertical" />

        </FrameLayout>

        <FrameLayout
            android:id="@+id/primary_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?attr/actionBarSize"
            android:background="@null"
            android:orientation="vertical">

            <ProgressBar
                android:id="@+id/progress"
                style="@style/ProgressBar"/>

        </FrameLayout>

        <LinearLayout
            android:id="@+id/full_screen_overlay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:background="@color/blackLight"
            android:orientation="vertical"/>

        <LinearLayout
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="true"
            android:background="@color/gray"
            android:orientation="vertical"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

            <TextView
                android:id="@+id/bottom_edit"
                style="@style/BottomSheetButton"
                android:text="@string/option 1"/>

        </LinearLayout>

    </android.support.design.widget.CoordinatorLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/nav_list"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="@color/kEDColorGrayLightest" />

</android.support.v4.widget.DrawerLayout>

当 ViewPager 打开时,primaryViewcontent 设置为 View.GONE

这是触发BottomSheet的接收器:

private final BroadcastReceiver modifyBudgetItemBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        fullScreenOverlay.setVisibility(View.VISIBLE);
    }
};

更新:我有一个次优的工作。我有BottomSheetBehavior 状态侦听器,它等待BottomSheet 展开并且它确实bottomSheet.requestLayout() 这会导致工作表至少出现。体验很差,因为床单似乎在“闪烁”,而不是向上滑动。 requestLayout 解决方法似乎不会在真实设备上产生闪烁。

【问题讨论】:

  • 我也有同样的问题。我在底页上有一个片段,无论我做什么,UI 都不会绘制。这绝对看起来像是 BottomSheet 实现中的一个错误。
  • @chubbsondubs 是的,这就是我所担心的。我注意到它似乎只发生在某些手机上(我的头顶> = 21,但我不完全确定)。如果我有时间,我将设置一个准系统示例项目。如果我可以干净地重现它,我将向 Google/Android 提交错误报告。

标签: android material-design android-support-library


【解决方案1】:

这是 google 开发人员讨论的已知问题 在某些 Pre-lollipop 设备上,底页不起作用

经过一天的研发,我找到了解决方案。试试这个

ViewCompat.postOnAnimation(yourCoordinator, new Runnable() {
            @Override
            public void run() {
                ViewCompat.postInvalidateOnAnimation(yourCoordinator);
            }
        });

将这段代码放在初始化视图之后

【讨论】:

    猜你喜欢
    • 2021-05-30
    • 2017-02-07
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多