6

Я хочу анимировать появление фрагмента. При чем чтобы он появлялся "сверху вниз". Мне частично удалось реализовать задумку, но проблема в том что сначала появляется место на экране под фрагмент, а только потом появляется сам фрагмент с анимацией. Как сделать одновременную анимацию появления места на экране и анимацию появления фрагмента?

Вот что имею:

Гифка с анимацией


Экран разметки

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:orientation="vertical"
    tools:context="simpleproject.diary.MainScreenTableView">

    <include
        android:id="@+id/toolbar"
        layout="@layout/two_lines_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible" />

    <FrameLayout
        android:id="@+id/calendarView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
    </FrameLayout>

    <include
        layout="@layout/recyclerview_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" />

</LinearLayout>

Методы, запускающие анимацию

private void calendarShow() {
    FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.setCustomAnimations(R.animator.calendar_show, R.animator.calendar_hide);
    t.replace(R.id.calendarView, mCalendar, CALENDAR_TAG);
    t.show(mCalendar);
    t.commit();
    mCalendar.refreshView();
}

private void calendarHide() {
    android.support.v4.app.FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.setCustomAnimations(R.animator.calendar_show, R.animator.calendar_hide);
    t.hide(getSupportFragmentManager().findFragmentByTag(CALENDAR_TAG));
    t.commit();
}

Анимации появления и исчезновения

calendar_show.xml

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

    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="300"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:propertyName="y"
        android:valueFrom="-1000"
        android:valueTo="0"
        android:valueType="floatType" />

</set>

calendar_hide.xml

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

    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="300"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:propertyName="y"
        android:valueFrom="0"
        android:valueTo="-1000"
        android:valueType="floatType" />

</set>
4
  • Евгений, а ты хочешь это сделать в виде падающего окна (popup) или тебе необходимо смещать список чтобы появлялся фрагмент ? 3 мар 2018 в 15:03
  • @KolinLoures я бы хотел смещать список 4 мар 2018 в 10:24
  • А дефолтные анимации из animateLayoutChanges вас не устраивают?
    – Real KEK
    4 мар 2018 в 18:04
  • @RealKEK я пробовал метод setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) и setTransition(FragmentTransaction.TRANSIT_ENTER_MASK) но они никак не влияли на анимацию. 5 мар 2018 в 10:07

1 ответ 1

1

Попробуйте добавить AnimationListener для управления видимостью фрагмента:

private void calendarShow() {
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
Animation anim = AnimationUtils.loadAnimation(this, R.animator.calendar_show);

anim.setAnimationListener(new Animation.AnimationListener() {
     @Override
     public void onAnimationStart(Animation animation) {
     }
     @Override
     public void onAnimationRepeat(Animation animation) {
     }
     @Override
     public void onAnimationEnd(Animation animation) {
     //Сделайте фрагмент видимым - setVisibility(VISIBLE);
     }
 });

t.startAnimation(anim);
t.replace(R.id.calendarView, mCalendar, CALENDAR_TAG);
t.show(mCalendar);
t.commit();

//Сделайте фрагмент невидимым - setVisibility(GONE);
mCalendar.refreshView();
}

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.