10

我正在尝试使用 MotionLayout 视图来获得折叠工具栏的行为。我的方法与此处的示例类似:https ://blog.stylingandroid.com/motionlayout-collapsing-toolbar-part-1/

这工作正常,但即使 recyclerview 为空或条目少于屏幕上的条目,也可以在滑动时开始转换。

如果回收器实际上能够滚动,有没有办法只启用 MotionLayout 过渡?

我的 OnSwipe 描述:

<OnSwipe
    app:dragDirection="dragUp"
    app:maxAcceleration="40"
    app:moveWhenScrollAtTop="true"
    app:touchAnchorId="@id/recycler"
    app:touchAnchorSide="top" />
4

3 回答 3

0

填充回收站视图后调用以下函数

     private fun enableOrDisableMotionLayout() {
                    Handler(Looper.getMainLooper()).postDelayed({
                        try {
                            if (binding.recyclerActivity != null) {
                                if (binding.recyclerActivity.canScrollVertically(-1) || binding.recyclerActivity.canScrollVertically(
                                        1
                                    )
                                ) {
                                    //R.id.transition should be the id used in Transition Tag in MotionScene xml file
                                    binding.motionLayout.enableTransition(R.id.transition, true)
                                } else {
                                    
                                    binding.motionLayout.enableTransition(R.id.transition, false)
                                }
                            }
                        } catch (e: Exception) {
            
                        }
                    }, 50L)
                }

上面的代码将根据recyclerview中的内容禁用或启用motionLayout滚动。注意:当最后一个项目部分可见时,仍然没有找到停止滚动od motionlayout的代码,这使得motionlayout滚动到折叠状态,即使是recycler view的所有内容被展示

于 2021-09-29T12:54:32.263 回答
0
if (dataList.isEmpty()) {
 motionLayout.enableTransition(R.id.transition, false)
}
于 2020-07-17T06:29:57.193 回答
-3

像这样更改您的 OnSwipe:

<OnSwipe
      app:dragDirection="dragUp"
      app:touchAnchorId="@id/recycler"
      app:touchAnchorSide="top" />
于 2019-06-02T10:11:03.387 回答