【问题标题】:Android collapsing toolbar with MotionLayout - disable motion when RecyclerView is empty/not scrollable带有 MotionLayout 的 Android 折叠工具栏 - 当 RecyclerView 为空/不可滚动时禁用运动
【发布时间】:2019-10-18 05:21:33
【问题描述】:

我正在尝试使用 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" />

【问题讨论】:

  • 这有什么更新吗?找到一种方法来禁用过渡?我试过 getTransition().setEnabled(false) 没有成功
  • 我一直在尝试同样的事情一天,但没有运气。如果你做到了,请分享:)

标签: android android-recyclerview android-toolbar android-motionlayout


【解决方案1】:
if (dataList.isEmpty()) {
 motionLayout.enableTransition(R.id.transition, false)
}

【讨论】:

    【解决方案2】:

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

         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 也会滚动到折叠状态

    【讨论】:

      【解决方案3】:

      像这样改变你的 OnSwipe:

      <OnSwipe
            app:dragDirection="dragUp"
            app:touchAnchorId="@id/recycler"
            app:touchAnchorSide="top" />
      

      【讨论】:

      • 对不起,这不起作用。仍然是相同的行为。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 2016-11-25
      • 1970-01-01
      相关资源
      最近更新 更多