5

我在这里有这个自定义的“底部操作栏”:https ://youtu.be/TPi5jtcs2wE ,它随着某些类型的网页(例如文章/非文章)出现和消失。我设置了最外层LinearLayoutanimateLayoutTransition制作了一个不同的LayoutTransition对象,但我希望栏在 webview 调整其高度的同时消失。需要明确的是,栏(relativeLayout)被设置为View.GONE并且 webView 应该扩展以匹配父级(由于 layout_weight),因为栏已经消失,但它不会同时进行。我尝试更改LayoutTransition.setDuration().setStartDelay().

文章Activity xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/container_article"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="app.morningsignout.com.morningsignoff.ArticleActivity"
    tools:ignore="MergeRootFrame"
    android:background="@android:color/white"
    android:animateLayoutChanges="true">
    <app.com.morningsignout.morningsignout.CustomWebView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/webView_article"
        android:layout_gravity="center"
        android:layout_weight="9" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:layout_weight="0"

LayoutTransition的配置:

LinearLayout container = (LinearLayout) findViewById(R.id.container_article);
LayoutTransition customTransition = new LayoutTransition();
customTransition.enableTransitionType(LayoutTransition.CHANGING);
customTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING);
customTransition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING);
customTransition.setAnimator(LayoutTransition.APPEARING, showArticleBar);
customTransition.setAnimator(LayoutTransition.DISAPPEARING, hideArticleBar);
customTransition.setStartDelay(LayoutTransition.APPEARING, 0);
customTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
customTransition.setStartDelay(LayoutTransition.CHANGING, 0);
customTransition.setDuration(LayoutTransition.APPEARING, showArticleBar.getDuration());
customTransition.setDuration(LayoutTransition.DISAPPEARING, hideArticleBar.getDuration());
customTransition.setDuration(LayoutTransition.APPEARING, showArticleBar.getDuration());

container.setLayoutTransition(customTransition);

hide_action_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="translationY"
    android:duration="300"
    android:valueFrom="0"
    android:valueTo="?android:attr/actionBarSize"/>

show_action_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="translationY"
    android:duration="300"
    android:valueFrom="?android:attr/actionBarSize"
    android:valueTo="0"/>

我虽然LayoutTransition.CHANGING动画会让它工作,但没有。如何让 webView 的高度变化和条形图的移除同时发生?尽管设置了持续时间和开始延迟排队,但它不起作用。我认为问题是CHANGING必须在之后发生,View.GONE因为在视图消失后需要知道高度。不管是那个还是这个都不是CHANGING动画。

4

1 回答 1

3

我想到了。原来我需要 CHANGE_APPEARING 和 CHANGE_DISAPPEARING 让它更平滑,而不是 CHANGING。它并不完美,但现在两个动画同时发生。我尝试了实际顶部操作栏的 hide()/show() 函数,我认为这是正常的。

LinearLayout container = (LinearLayout) findViewById(R.id.container_article);
LayoutTransition customTransition = new LayoutTransition();
// customTransition.enableTransitionType(LayoutTransition.CHANGING);
// customTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING);
// customTransition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING);
customTransition.setAnimator(LayoutTransition.APPEARING, showArticleBar);
customTransition.setAnimator(LayoutTransition.DISAPPEARING, hideArticleBar);
customTransition.setStartDelay(LayoutTransition.APPEARING, 0);
customTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
customTransition.setStartDelay(LayoutTransition.CHANGE_APPEARING, 0);
customTransition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);
// customTransition.setStartDelay(LayoutTransition.CHANGING, 0);
customTransition.setDuration(LayoutTransition.APPEARING, showArticleBar.getDuration());
customTransition.setDuration(LayoutTransition.DISAPPEARING, hideArticleBar.getDuration());
customTransition.setDuration(LayoutTransition.CHANGE_APPEARING, showArticleBar.getDuration());
customTransition.setDuration(LayoutTransition.CHANGE_DISAPPEARING, hideArticleBar.getDuration());
// customTransition.setDuration(LayoutTransition.APPEARING, showArticleBar.getDuration());

container.setLayoutTransition(customTransition);

这会产生一个问题,即 webview 在新网页上仍然保持滚动位置,这是错误的(应该在新页面的页面顶部)。我通过在linearLayout中包含webview来修复它。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/container_article"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="app.morningsignout.com.morningsignoff.ArticleActivity"
    tools:ignore="MergeRootFrame"
    android:background="@android:color/transparent"
    android:animateLayoutChanges="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9">

        <app.com.morningsignout.morningsignout.CustomWebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webView_article"
            android:layout_gravity="center" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:layout_weight="0"...
于 2015-09-21T10:00:50.247 回答