Good afternoon.
I have a viewPager with 5 fragments, each fragment has in its layout a "LottieAnimationView" from the Lottie library that is being used to reproduce the animations, the operation of these animations is correct, when the user enters the activity that has the animations and the screen does NOT touch the animations will be played automatically and will go to the next fragment to play the next animation, so on until the last one is reached; in the case that the user touches the screen the animation that is being played will normally do so but will no longer pass to the next fragment, this implementation is done correctly.
The problem I have is that when the user has touched the screen and the animation that is playing is over, I can not restart (the animation), I just repeat the first animation but not the others .
This is my main activity:
public class TutorialActivity extends BaseActivity implements
Tuto1Fragment.OnFragmentInteractionListener,
Tuto2Fragment.OnFragmentInteractionListener,
Tuto3Fragment.OnFragmentInteractionListener,
Tuto4Fragment.OnFragmentInteractionListener,
Tuto5Fragment.OnFragmentInteractionListener,
PromoTutoFragment.OnFragmentInteractionListener {
private MyPagerAdapter mFragmentAdapter;
public ViewPager mViewPager;
// Sirve para determinar los inicios de las animaciones en cada Fragment
public static boolean noPasar = false;
// Sirve para detener la animación del ViewPager cuando pulsas sobre un tab
public static boolean stopPager = true;
//List<Offer> previewOffers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial);
mViewPager = findViewById(R.id.view_pager);
// Añadir el adapter al ViewPager
mFragmentAdapter = new MyPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mFragmentAdapter);
// Cargar los 5 fragment depués de iniciar la actividad
mViewPager.setOffscreenPageLimit(5);
// Añadir touch cuando el usuario cambie manualmente los fragment
mViewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//Variable en false cuando el usuario ha tocado la pantalla
TutorialActivity.stopPager = false;
return false;
}
});
TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(mViewPager, true);
// Buscar los tab generados en el TabLayout
LinearLayout tabStrip = (LinearLayout) tabLayout.getChildAt(0);
// Añadir el evento click sobre los tabs
for (int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TutorialActivity.stopPager = false;
}
});
}
// Añadir el evento cambio de Fragment a los tabs sin efecto de traslado
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition(), false);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
// Iniciar en el 1° Fragment y no iniciar transición
mViewPager.setCurrentItem(0, false);
}
@Override
public void onFragmentInteraction(Uri uri) {
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Tuto1Fragment();
case 1:
return new Tuto2Fragment();
case 2:
return new Tuto3Fragment();
case 3:
return new Tuto4Fragment();
case 4:
return new Tuto5Fragment();
/*if (previewOffers != null)
return PromoTutoFragment.newInstance(previewOffers.get(0));
else return new PromoTutoFragment();*/
default:
return null;
}
}
// Returns total number of pages
@Override
public int getCount() {
return 5;
}
}
}
This is my fragment 1:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tuto1, container, false);
animationView = view.findViewById(R.id.animation);
animationView.addAnimatorListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
//Log.e("AnimaS", "tuto1");
}
@Override
public void onAnimationEnd(final Animator animation) {
//Log.e("AnimaE", "tuto1");
try {
// Solo realizará el cambio de Fragment si la variable es true
if (TutorialActivity.stopPager){
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
if (getActivity() != null)
((TutorialActivity) getActivity()).mViewPager.setCurrentItem(1, false);
}
},
300);
}else{
//Si es false se reproducirá la misma animación
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
if (getActivity() != null){
((TutorialActivity) getActivity()).mViewPager.setCurrentItem(0, false);
animation.start();
}
}
},
300);
}
} catch (Exception ex) {
//Para capturar cuando no esta listo para cambiar
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
return view;
}
For the other fragments it is the same code, the first animation is repeated correctly, but after 2 no longer , the animation is spoiled (the first is then passed to the second, the animations get a little crazy), the only part that changes is:
if (TutorialActivity.stopPager){
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
if (getActivity() != null)
((TutorialActivity) getActivity()).mViewPager.setCurrentItem(2, false);
}
},
300);
}else{
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
if (getActivity() != null){
((TutorialActivity) getActivity()).mViewPager.setCurrentItem(1, false);
animation.start();
}
}
},
300);
}