Snackbar 的滑动关闭冻结应用程序

Snackbar's swipe to dismiss freezes app

快速上下文:

我有一个 FragmentActivity 托管我在整个应用程序中显示的所有视图组件,特别是用于任何 snackbars[=38= 的 CoordinatorLayout 视图] 我是为了实现滑动关闭效果。

Activity xml

  <android.support.design.widget.CoordinatorLayout
    android:id="@+id/cl_snackbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Activity class

@BindView(R.id.cl_snackbar) View snackBar;

/**
 * To allow for use of a snackbar throughout the app's fragments
 */
public View getSnackBar() {
    return snackBar;
}

片段class

@Nullable private View snackBar;

//This is done in fragment's onCreateView()
if (getActivity() instanceof ParentActivity) snackBar = ((ParentActivity) getActivity()).getSnackBar();

//This is done in a method
if (snackBar != null) Snackbar.make(snackBar, "Working", Snackbar.LENGTH_LONG).show();

此功能在过去一年中运行良好,但我最近将我的 Android 支持库 从 26.1.0 更新到 27.0.0 并且滑动到-解雇效果现在完全冻结应用程序。它变得反应迟钝。在 logcat 中,我收到以下警告:

E/ViewDragHelper: Ignoring pointerId=0 because ACTION_DOWN was not received for this pointer before ACTION_MOVE. It likely happened because  ViewDragHelper did not receive all the events in the event stream.
E/ViewDragHelper: Ignoring pointerId=-1 because ACTION_DOWN was not received for this pointer before ACTION_MOVE. It likely happened because  ViewDragHelper did not receive all the events in the event stream.
E/ViewDragHelper: Ignoring pointerId=-1 because ACTION_DOWN was not received for this pointer before ACTION_MOVE. It likely happened because  ViewDragHelper did not receive all the events in the event stream.
E/ViewDragHelper: Ignoring pointerId=-1 because ACTION_DOWN was not received for this pointer before ACTION_MOVE. It likely happened because  ViewDragHelper did not receive all the events in the event stream.

我检查了 diff 中的所有更改 Android 27.0.0,但我没有看到与任何支持有关的任何更改 class es涉及。任何人都可以就突然出现的问题提供帮助或提示吗?

删除滑动关闭 behaviour 应该可以解决问题:

    snackBar.addCallback(object : BaseTransientBottomBar.BaseCallback<Snackbar>() {
        override fun onShown(transientBottomBar: Snackbar) {
            val layoutParams = transientBottomBar.view.layoutParams as? CoordinatorLayout.LayoutParams
            layoutParams?.let { it.behavior = null }
        }
    })

如果您需要保存行为,您应该复制 behaviour 并在处理触摸事件之前插入 requestDisallowInterceptTouchEvent(true) 并在之后插入 requestDisallowInterceptTouchEvent(false)

我在带有内部 SwipeRefreshLayout 的 CoordinatorLayout 中遇到了类似的问题,其中滑动关闭停止工作,并在 LogCat 中抛出错误但没有崩溃:

E/ViewDragHelper: Ignoring pointerId=0 because ACTION_DOWN was not received for this pointer before ACTION_MOVE. It likely happened because  ViewDragHelper did not receive all the events in the event stream.

pointerId=-1 也出现了同样的错误。

我的 activity 布局是:

<!-- cl_main is the SnackBar's host View -->
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/cl_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/main_include_all_items"/>
    </android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>

我想也许 SwipeRefreshLayout 正在拦截到达 CoordinatorLayout 的滑动。

我通过将 build.gradle 中的 compileSdkVersion 和 targetSdkVersion 升级到 28 以及 build.gradle 中的依赖项升级到 28.0.0 解决了 this/worked 问题(大部分) ;这样做之后,Snackbar 的滑动关闭功能现在可以使用了。

它仍然会抛出与以前相同的错误,但是,与以前一样,应用程序不会崩溃。