0

I have a horizontal recyclerView, I want to animate it (by sliding right and then left) when the activity is first opened.

So I do this in onCreate:

final Animation slideRight = AnimationUtils.loadAnimation(this, R.anim.slide_right);
        slideRight.setDuration(200);

        final Animation slideLeft = AnimationUtils.loadAnimation(this, R.anim.slide_left);
        slideLeft.setDuration(200);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.startAnimation(slideRight);
        recyclerView.startAnimation(slideLeft);

but seems like only the right slide works

here are my anims:

left slide

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

    <translate
        android:duration="200"
        android:fromXDelta="-100%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="0%" />

</set>

right slide

<translate
    android:duration="200"
    android:fromXDelta="100%"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="0%" />

what am I doing wrong?

EDIT: how is this related to proposed duplicate?

Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85

2 Answers2

0

In this case slideLeft animation will work.. because before the slideRight is working the slideleft is getting started.. so do like this...

    slideRight.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

            recyclerView.startAnimation(slideLeft);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
}

At the end of animation right start the animation left

Edit Or you can start only one anim by adding repeatMode = reverse and repeatCount = 1` in your anim..

android:repeatMode="reverse" android:repeatCount="1"

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
0

try this one

Animation slideRight = AnimationUtils.loadAnimation(this, R.anim.slide_right);
Animation slideLeft = AnimationUtils.loadAnimation(this, R.anim.slide_left);
slideRight.setDuration(200);
slideLeft.setDuration(200);
slideRight .setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
               recyclerView.startAnimation(slideLeft);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
  recyclerView.startAnimation(slideRight);
Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26