Android 展开/折叠工具栏拖动视图

Android 展开/折叠工具栏拖动视图,android,android-layout,android-coordinatorlayout,Android,Android Layout,Android Coordinatorlayout,我正在尝试使用折叠工具栏布局和AppBarLayout底部的其他视图创建以下行为,但是当我滚动/拉动PullView时,该条不会折叠/展开,这意味着在折叠时无法打开该条,也无法使用该视图关闭该条 我已经尝试在PullView根目录中使用NestedScrollView,但没有成功 我使用运动布局找到了一个很酷的解决方案 首先,我扩展了MotionLayout来改变一点行为,而不是在整个MotionLayout中拖动,我只想在指示器和菜单上拖动 TLDR: 因此,我的MotionLayout覆盖

我正在尝试使用
折叠工具栏布局
AppBarLayout
底部的其他视图创建以下行为,但是当我滚动/拉动
PullView
时,该条不会折叠/展开,这意味着在折叠时无法打开该条,也无法使用该视图关闭该条

我已经尝试在
PullView
根目录中使用
NestedScrollView
,但没有成功



我使用
运动布局找到了一个很酷的解决方案

首先,我扩展了
MotionLayout
来改变一点行为,而不是在整个
MotionLayout
中拖动,我只想在指示器和菜单上拖动

TLDR:

因此,我的
MotionLayout
覆盖了
OnTouchEvent
,并检查我们是否正在接触其中一个直接子对象

class TouchableMotionLayout @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0)
    : MotionLayout(context, attrs, defStyleAttr), MotionLayout.TransitionListener {

    private val viewRect = Rect()
    private var touchStarted = false

    init {
        initListener()
    }

    private fun initListener() {
        setTransitionListener(this)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.actionMasked) {
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                touchStarted = false
                return super.onTouchEvent(event)
            }
        }

        if (!touchStarted) {
            touchStarted = verifyIfChildHasTouched(event)
        }

        return touchStarted && super.onTouchEvent(event)
    }

    /**
     * Verify if touching one fo the children.
     *
     * @param event The motion event.
     * @return True if touch its in one of the children.
     */
    private fun verifyIfChildHasTouched(event: MotionEvent): Boolean {
        for (index in 0 until childCount) {
            val view = getChildAt(index)
            view.getHitRect(viewRect)
            if (viewRect.contains(event.x.toInt(), event.y.toInt())) {
                return true
            }
        }

        return false
    }

    override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
        touchStarted = false
    }

    override fun allowsTransition(p0: MotionScene.Transition?) = true

    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) {}
}
在此布局中,我有一个viewpager,然后是一个自定义的
运动布局
,只能在
运动布局
的直接子菜单中打开/关闭菜单切换,即
菜单指示器
菜单