TabLayout using activities instead of fragments
Asked Answered
A

2

7

Is it possible to create a swiped TabLayout in an app using multiple activities instead of fragments? I have two activities using which I am trying to create an app that uses a swipable TabLayout. I am searching the net over this but could not find one yet. If that is possible to build, can anyone please provide some links or tutorials over this?

Appear answered 14/10, 2015 at 6:8 Comment(6)
Why my question was downvoted?? Being a learner and curious over something I don't know well doesn't mean should be underrated I think!!Appear
Why do you want to use Activities instead of Fragments?Mismate
Because I don't want to use fragments in this app.Appear
You could use a onTouchEvent/GestureDetectorCompat, detect the 'swipe left/right' gesture and then starting the next activity with an intent. But in most times, it's better to use fragments.Mismate
Okay.. Can you please provide a link or tutorial over that? I don't know much about it @AmyAppear
You can use developer.android.com/training/gestures/detector.html for first information :)Mismate
M
3

From GestureDetector and OnSwipeTouchListener:

public class MainActivity extends Activity { 

    private GestureDetectorCompat mDetector; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDetector = new GestureDetectorCompat(this, new MyGestureListener());
    }

    @Override 
    public boolean onTouchEvent(MotionEvent event){ 
        this.mDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        private static final String DEBUG_TAG = "Gestures"; 

       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() {
    }        
}

Usage from second source:

imageView.setOnTouchListener(new OnSwipeTouchListener() {
    public void onSwipeTop() {
        Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeRight() {
        Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
    public void onSwipeLeft() {
        Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, FirstActivity.class);
        startActivity(intent);
    }
    public void onSwipeBottom() {
        Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
    }

    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
});
Mismate answered 14/10, 2015 at 8:18 Comment(0)
E
2

Yes - it's possible but not that much effective though. It will be like solving your problem with 2-3 year old solution. If you still want to go ahead you can refer to the following link: http://www.mkyong.com/android/android-tablayout-example/ This should help you to understand.

Eleni answered 14/10, 2015 at 6:20 Comment(1)
But that cannot be swiped. Can be clicked only. I want a swipeable oneAppear

© 2022 - 2024 — McMap. All rights reserved.