Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LayoutTransition disappearing and changing not happening at same time

I have this custom "bottom action bar" here: https://youtu.be/TPi5jtcs2wE, that appears and disappears with certain types of webpages (e.g. article/not article). I set up the outermost LinearLayout with animateLayoutTransition and made a different LayoutTransition object, but I want the bar to disappear at the same time as the webview adjusts its height. To be clear, the bar (relativeLayout) is being set to View.GONE and the webView should expand to match parent (due to layout_weight) since the bar is gone, but it's not doing both at the same time. I tried changing LayoutTransition.setDuration() and .setStartDelay().

The articleActivity 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"

The configuration of the 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"/>

I though the LayoutTransition.CHANGING animation would make it work, but no. How do I get the webView's height change and the removal of the bar to occur at the same time? Despite setting durations and start delays to line up, it's not working. I think the issue is that CHANGING has to happen after View.GONE because of needing to know height after the view is gone. Either that or this is not a CHANGING animation.

like image 717
Daniel Handojo Avatar asked Oct 19 '22 02:10

Daniel Handojo


1 Answers

I figured it out. Turns out I needed CHANGE_APPEARING and CHANGE_DISAPPEARING to make it smoother, not CHANGING. It's not perfect, but now both animations happen at the same time. I tried the actual top actionbar's hide()/show() functions and I think that this is normal.

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);

This creates an issue where the webview persists the scrollY position though on new webpages, which is wrong (should be at top of page on new pages). I fixed it by containing the webview in a linearLayout.

<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"...
like image 88
Daniel Handojo Avatar answered Nov 02 '22 09:11

Daniel Handojo