是否可以禁用MotionLayout?

斯蒂芬曼特尔

我有一个在内Fragment托管一个RecyclerViewMotionLayout在回收器视图的顶部,我有一个可以折叠和展开的视图,所有这些都在我的运动场景中完成。通过单击以及响应拖动回收站视图来触发它。到目前为止,这一切都按预期进行。

现在问题来了:我想在我的应用程序中完全隐藏某些状态的折叠视图。但是,如果我设置视图可见性或更改其高度,则在拖动recyclerview时,它仍会重新显示。

因此可以完全禁用MotionLayout(锁定约束),直到再次启用它为止。禁用拖动识别器也是一种可行的解决方案。

现在使用一些简化的XML来演示这种情况。

该布局包含带有标题视图和recyclerview的运动布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.motion.MotionLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/favouritesMotionLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/favourites_motion_scene"
    android:animateLayoutChanges="true">

    <ImageView
        android:id="@+id/headerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        />

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

        <pinch.nrcnext.view.scroll.NRCRecyclerView
            android:id="@+id/favoritesList"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.SwipeRefreshLayout>

</android.support.constraint.motion.MotionLayout>

对应的动作场景:

<?xml version="1.0" encoding="utf-8"?>
<MotionScene
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto"
>

    <Transition
        motion:constraintSetStart="@id/expanded"
        motion:constraintSetEnd="@id/collapsed"
        motion:duration="300">
        <OnSwipe
            motion:touchAnchorId="@id/swipeRefreshLayout"
            motion:touchAnchorSide="top"
            motion:dragDirection="dragUp" />
    </Transition>

    <ConstraintSet android:id="@+id/expanded">
        ... Constraints for expanded header ...
    </ConstraintSet>

    <ConstraintSet android:id="@+id/collapsed">
        ... ... Constraints for collapsed header ...
    </ConstraintSet>

</MotionScene>

使用约束布局版本“ 2.0.0-alpha3”

implementation "com.android.support.constraint:constraint-layout:2.0.0-alpha3"

回顾一下:我希望能够禁用运动布局,以便可以在不扩展标题的情况下滚动recyclerview,直到我告诉它再次启用为止。不幸的是,这并不像那样简单favouritesMotionLayout.isEnabled = false,但这就是我的想法。

LPirro

现在,有了ConstraintLayout beta 2,您可以!这是官方的方法:

motionLayout.getTransition(R.id.yourTransition).setEnable(false)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章