Scrolling between screens

Asked

Viewed 182 times

0

I want to scroll between different screens. For example, create an equal scroll of the mobile menu or the desktop that when pulled one will occupy the space of the other. How can I do that?

1 answer

3


Create a folder called "anim" in the "res" directory. Inside, create the files:

slide_in_left_transition.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate
        android:duration="400"
        android:fromXDelta="-100%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="0%" />
</set>

slide_in_right_transition.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate
        android:duration="400"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="-100%"
        android:toYDelta="0%" />
</set>

slide_out_left_transition.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

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

</set>

slide_out_right_transition.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

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

</set>

Now when you want to do the animation of going forward, call the method overridePendingTransition() after giving startActivity, for example:

Intent i = new Intent(Activity1.this, Activity2.class);
startActivity(i);
overridePendingTransition(R.anim.slide_out_left_transition, R.anim.slide_in_right_transition);

Now to do the opposite effect, put overridePendingTransition(R.anim.slide_in_left_transition, R.anim.slide_out_right_transition); right after closing Activity, either after a finish(); or in the event of the cell’s back button (see example below).

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.slide_in_left_transition, R.anim.slide_out_right_transition);
}
  • 1

    Great implementation, I had no idea it was so simple (or not). Very good your example, implemented here and worked perfect, just not quite the way you wanted to question, I believe it is to exchange the views for Touch Left and Touch Right, but with a little effort, I believe it is possible from this example. + 1 for having me help.

  • I really was in doubt if he really wanted this, let’s see when he answered hehe Thanks!!

  • 1

    But this does not take the merit of its solution, very good, it is only necessary to adapt to work for the issue. In my implementation I overwrote the method finish() like the onBackPressed(), if the Activity is finished through this method and not by the back button;

  • 1

    I forgot this detail, I also do it when necessary. I will edit in reply! Thanks guy :p

Browser other questions tagged

You are not signed in. Login or sign up in order to post.