0
  • 我正在 android studio (Java) 上开发一个应用程序
  • 我创建了一个 tabLayout,它使用 viewPager2 + Fragment State Adapter 在两个片段之间滑动
  • 在 fragment02 上,我为 textView 设置了动画。
  • 当我在片段上滑动时,动画会循环播放,这是应该的。
  • 但是,如果我从片段上滑下然后重新打开,动画将不再播放。
  • 我真的需要动画继续。

欢迎任何建议,我提前谢谢你:)

请注意,我对此很陌生。

// MainActivity.java

package Zoomie.App;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().setTitle("Welcome");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ViewPager2 viewPager2 = findViewById(R.id.ViewPager);
        viewPager2.setAdapter(new FragmentPagerAdapter(this));

        TabLayout tabLayout = findViewById(R.id.tabLayout);
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
                tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {

            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {

                switch (position) {
                    case 0: {
                        tab.setText("Fragment 01");
                        break;
                    }
                    default: {
                        tab.setText("Fragment 02");
                        break;
                    }
                }
            }
        });
        
        tabLayoutMediator.attach();
    }
}

// FragmentPagerAdapter.java

package Zoomie.App;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

public class FragmentPagerAdapter extends FragmentStateAdapter {

    public FragmentPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        switch (position){
            case 0:
                return new Fragment01();
            default:
                return new Fragment02();
        }
    }

    @Override
    public int getItemCount() {
        return 2;
    }
}

// Fragment02.java 

package Zoomie.App;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class Fragment02 extends Fragment {

    public Fragment02() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView=inflater.inflate(R.layout.fragment_02, container, false);

        TextView fade01 = (TextView) rootView.findViewById(R.id.textView_fade_01);
        Animation fade = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_animation);
        fade01.startAnimation(fade);

        return rootView;
    }
}

// fade_animation.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:fromAlpha="0"
        android:toAlpha=".5"
        android:duration="375"
        android:repeatMode="reverse"
        android:repeatCount="infinite"
        />
        
</set>

4

1 回答 1

0

您应该将加载动画方法移动到 onResume() 生命周期方法中。在你的情况下:

private TextView fade01;

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState
) {

    View rootView = inflater.inflate(R.layout.fragment_02, container, false);

    fade01 = (TextView) rootView.findViewById(R.id.textView_fade_01);

    return rootView;
}

@Override
public void onResume() {
    super.onResume();
    Animation fade = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_animation);
    fade01.startAnimation(fade);
}
于 2020-02-12T20:52:59.687 回答