Home > Back-end >  Can I change activities by using ViewPager2?
Can I change activities by using ViewPager2?

Time:04-22

Is it possible, to run another activity, by swipe of hand with using ViewPager? I've saw some examples, but I don't think it was what I wanted. I'd like to have three activities:

  • actual forecast (main)
  • hourly forecast (swiped left)
  • daily forecast (swiped right)

I'd like to have them displayed, after hand swipe in some direction(if it's possible - does activity after changed are being closed, or just paused?)

As far as I've saw on some examples, I have to implement RecyclerView adapter - since I want to display different data for different activities, do I need to have three separated adapters?

I'm trying to get this done with mvvm, and I've found this one very confusing for me.

CodePudding user response:

you need one Activity and three Fragments. In HERE you can find an official doc how to implement that. Keeping three Activities inside ViewPager/adapter isn't possible

CodePudding user response:

You don't need a recycler view for this. You can switch between activities by sliding left or right. Follow the steps:

  1. Create a class named OnSwipeTouchListener.java and paste this code:
import android.content.Context;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener (Context ctx){
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                        result = true;
                    }
                }
                else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                    result = true;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }
}

And then on your activity's root view, add this code:

binding.getRoot().setOnTouchListener(new OnSwipeTouchListener(MyActivity.this) {
    public void onSwipeTop() {
        // we don't need it but still I have given it
    }
    public void onSwipeRight() {
        Intent intent = new Intent(MyActivity.this, DailyForecastActivity.class);
        startActivity(intent);
    }
    public void onSwipeLeft() {
        Intent intent = new Intent(MyActivity.this, HourlyForecastActivity.class);
        startActivity(intent);
    }
    public void onSwipeBottom() {
        // we don't need it but I have given it
    }

});

Below is completely optional code. If you want animations while swiping, add it

And, don't forget to add this line of code in your DailyForecastActivity's onCreate() method:

overridePendingTransition(R.anim.silde_in_right,0);

where R.anim.silde_in_right is a animation file with this code:

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

Also, don't forget to add this line of code in your HourlyForecastActivity's onCreate() method:

overridePendingTransition(R.anim.silde_out_right,0);

where R.anim.silde_out_right is a animation file with this code:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="150"
        android:fromXDelta="0"
        android:toXDelta="100%p"/>
</set>
  • Related