在片段状态适配器内的片段之间滑动时,动画(textView)会不断取消

时间:2020-02-12 20:27:41

标签: java android android-studio android-fragments android-viewpager2

  • 我正在android studio(Java)上开发应用程序
  • 我创建了一个tabView,可使用viewPager2 +片段状态适配器在两个片段之间滑动
  • 在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>

1 个答案:

答案 0 :(得分: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);
}