【问题标题】:FragmentTransaction and Animation with a class that extends Fragment带有扩展 Fragment 的类的 FragmentTransaction 和 Animation
【发布时间】:2018-01-06 18:49:57
【问题描述】:

所以在布局中我有三个按钮,对于控制流,我使用一个开关来确定单击每个按钮时会发生什么行为。

我的问题是,当单击每个按钮转到下一个活动时,我希望淡入淡出过渡取代默认过渡。

我认为我的代码已关闭,但是当我运行模拟器时,我没有看到过渡变化?我是 Android 新手,我不知道我做错了什么,我为按钮布局附加了我的 Java 代码和 XML,以及我的输入/输出转换 XML。谢谢!!

package com.example.jonathan.om11;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Jonathan on 12/28/17.
 */

public class Tab1 extends Fragment implements View.OnClickListener {

    Button toEnv1Page;
    Button toEnv2Page;
    Button toEnv3Page;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab1, container, false);

        toEnv1Page = (Button) rootView.findViewById(R.id.envBtn1);
        toEnv1Page.setOnClickListener(this);

        toEnv2Page = (Button) rootView.findViewById(R.id.envBtn2);
        toEnv2Page.setOnClickListener(this);

        toEnv3Page = (Button) rootView.findViewById(R.id.envBtn3);
        toEnv3Page.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.envBtn1:
                Intent i = new Intent("android.intent.action.Env1Page");
                startActivity(i);
                android.support.v4.app.FragmentTransaction fAnimation = this.getActivity().getSupportFragmentManager().beginTransaction();
                fAnimation.setCustomAnimations(android.R.anim.fade_in, R.anim.fade_out);
                fAnimation.commit();
                break;

            case R.id.envBtn2:
                Intent ii = new Intent("android.intent.action.Env2Page");
                startActivity(ii);
                android.support.v4.app.FragmentTransaction f2Animation = this.getActivity().getSupportFragmentManager().beginTransaction();
                f2Animation.setCustomAnimations(android.R.anim.fade_in, R.anim.fade_out);
                f2Animation.commit();
                break;

            case R.id.envBtn3:
                Intent iii = new Intent("android.intent.action.Env3Page");
                startActivity(iii);
                android.support.v4.app.FragmentTransaction f3Animation = this.getActivity().getSupportFragmentManager().beginTransaction();
                f3Animation.setCustomAnimations(android.R.anim.fade_in, R.anim.fade_out);
                f3Animation.commit();
                break;

            default:
                break;
        }
    }

}

布局 XML:

<android.support.constraint.ConstraintLayout 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/tabsLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jonathan.om11.MainTabbedActivity$PlaceholderFragment">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/envBtn1"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/cardview_light_background"
            android:text="Rainforest"
            android:textSize="40sp" />

        <Button
            android:id="@+id/envBtn2"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/cardview_light_background"
            android:text="Desert"
            android:textSize="40sp" />

        <Button
            android:id="@+id/envBtn3"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/cardview_light_background"
            android:text="Fireplace"
            android:textSize="40sp" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

淡入动画 XML:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="0.0"
    android:toAlpha="1.0">

</set>

淡出动画 XML:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:fillAfter="true"
    android:duration="500"
    >

</set>

【问题讨论】:

    标签: java android android-fragments android-animation android-transitions


    【解决方案1】:

    你只需要setCustomAnimations() 之前的beginTransaction() 片段。喜欢,

    YourFragment fragment = new YourFragment();
                 fragment.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
                 android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                 transaction.replace(R.id.your_layout_element_which_is_to_be_replace_by_fragment_layout, fragment);
                 transaction.commit();
    

    编辑 -- 如果您还想在从片段导航返回时执行转换,那么您只需要设置propEnter 和propExit 转换。例如,

    fragment.setCustomAnimations(enterTransition, exitTransition, propEnterTransition, propExitTransition);
    

    【讨论】:

    • 我刚试过这个,但是编译器无法解析符号“f2Animation”。即使我开始上课时:FragmentTransaction f2Animation; 不幸的是,当我单击按钮时,应用程序崩溃了。 :-(
    • 哦!太棒了,我也应该制作我正在过渡到片段的屏幕吗?因为我最初把它做成了一个全屏活动。
    • 不,您可以轻松清除片段中的全屏标志。如果你愿意
    • 老实说,我是新手,我不知道“清除标志”是什么意思。这是什么意思?
    • 我的意思是你可以很容易地从全屏默认你的片段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    相关资源
    最近更新 更多