2

当 RecyclerView 向上拖动时,我正在使用 MotionLayout 来实现一些很酷的效果,但是当我想将 SwipeRefreshLayout 与 RecyclerView 一起使用时,似乎发生了冲突。

如果没有 SwipeRefreshLayout 就好了

如果我用 SwipeRefreshLayout 包围,向上拖动的行为就像反对一样奇怪

https://gist.github.com/GHChrisSu/6aadf40dc693bc47cbb83a167a82a4b9

运动场景如下

https://gist.github.com/GHChrisSu/a154d59f34555cccfc1b48617989ae16

4

2 回答 2

6

你应该把你MotionLayout的包装成SwipeRefreshLayout这样:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.constraintlayout.motion.widget.MotionLayout
                android:id="@+id/motion_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layoutDescription="@xml/play_scene">

                <!-- Some xml code -->

            </androidx.constraintlayout.motion.widget.MotionLayout>

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

然后你必须根据状态添加一个MotionLayout.TransitionListener到你MotionLayout的。enable/disableSwipeRefreshLayoutMotionLayout

binding.motionLayout.setTransitionListener(object :MotionLayout.TransitionListener{
        override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {

        }

        override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
        }

        override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {
            Log.d("RepairsFragment","$p1 $p2 $p3")
            if (p3 == 0f) {
                binding.refresh.isEnabled=true
            } else {
                binding.refresh.isEnabled = false
                binding.refresh.isRefreshing=false
            }
        }

        override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
        }
    })
于 2020-01-20T16:24:26.923 回答
1

我最终通过将 SwipeRefreshLayout 包裹在 motionlayout 之外来解决这个问题,并在 onTransitionChange 函数中更改 SwipeRefreshLayout 的状态来处理这个问题

于 2019-03-31T12:03:19.640 回答