【问题标题】:Elongate View when another one is being hidden隐藏另一个视图时拉长视图
【发布时间】:2018-02-13 19:40:15
【问题描述】:

我目前正在尝试实现某个动画: 我有一个视图(LinearLayout),它应该在按下 fab 按钮时隐藏/显示,并且该视图(LinearLayout)下方的视图(RecyclerView)应该拉长或缩短,以便再次适合屏幕。 LinearLayout 被向上推,因此下面的 RecyclerView 应该以完美的方式拉长,看起来它们会粘在一起。

目前我有如下自定义动画:

slide_down.xml

<translate
    android:duration="1000"
    android:fromYDelta="-100%"
    android:toYDelta="0" />

slide_up.xml

<translate
    android:duration="1000"
    android:fromYDelta="0"
    android:toYDelta="-100%"/>

布局:

<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.chrono.worker.TabbedActivity">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginBottom="?attr/actionBarSize">

    <!-- THIS ONE IS THE LINEAR LAYOUT I WAS TALKING ABOUT -->
    <LinearLayout
        android:id="@+id/ll_search_whole"
        android:layout_width="match_parent"
        android:layout_height="@dimen/search_grid"
        android:animateLayoutChanges="true"
        android:background="@color/colorPrimary"
        android:orientation="horizontal">


        <LinearLayout
            android:id="@+id/ll_search"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal"
            android:paddingBottom="2dp"
            android:paddingLeft="3dp"
            android:paddingRight="3dp"
            android:paddingTop="2dp"
            android:weightSum="2">

            <!-- took out some code here -->


        </LinearLayout>

    </LinearLayout>

<!-- THIS IS THE RECYCLERVIEW -->
<android.support.v7.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_below="@id/ll_search_whole"
    >

</android.support.v7.widget.RecyclerView>
</RelativeLayout>

不过,RecyclerView 表现迟缓,根本不坚持 LinearLayout。

如何实现我想要的动画?

【问题讨论】:

  • 请与 LinearLayout 及其相邻的 RecyclerView 共享布局
  • 我添加了,请检查:)

标签: android xml android-layout android-animation


【解决方案1】:

为什么不使用过渡框架?对于这种方法,父 RelativeLayout 需要一个 id,假设它是 android:id="@+id/sceneRoot"。 我们将所有相关的Views 设置为Activity/Fragment 的字段:

private ViewGroup sceneRoot = (ViewGroup)findViewById(R.id.sceneRoot);
private View ll_search_whole = findViewById(R.id.ll_search_whole);

下面是隐藏LinearLayout的方法:

TransitionSet tSet = new TransitionSet();
tSet.addTransition(new Fade());
tSet.addTransition(new ChangeBounds());

TransitionManager.beginDelayedTransition(sceneRoot, tSet);
ll_search_whole.setVisibility(View.GONE);

这应该会产生流畅的动画。如果ViewGroups 像RecyclerView 转换一样落后,可以尝试调用setTransitionGroup(true);

【讨论】:

  • 方法tSet.add()不存在,是addTransition();吗?
  • @ProgFroz - 我的错(我以为我心里知道)
  • 可以,但并不完美。首先,当我滚动 recyclerView 并单击 fab 进行动画处理时,我收到一条错误消息。其次,我希望向上和向下推动 LinearLayout(就像在 R.anim 文件中一样)+这个漂亮的平滑布局,我该如何组合呢?感谢TransitionSet顺便说一句,不知道这存在:)
  • @ProgFroz - 幻灯片对翻译动画很有用。基本点似乎是,在您调用TransitionManager.beginDelayedTransition(sceneRoot, tSet); 之后,您设置了所需的可见性最终值以及位置或大小。抱歉,我必须尽快离开,但这是 A.J. 到我的favourite tutorial 的链接。洛克伍德
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 2011-10-15
  • 2023-03-23
  • 1970-01-01
  • 2011-09-02
相关资源
最近更新 更多