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?
Asked
Active
Viewed 9,404 times
7
-
9Why my question was downvoted?? Being a learner and curious over something I don't know well doesn't mean should be underrated I think!! – Oct 14 '15 at 06:10
-
Why do you want to use Activities instead of Fragments? – yennsarah Oct 14 '15 at 06:56
-
Because I don't want to use fragments in this app. – Oct 14 '15 at 06:58
-
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. – yennsarah Oct 14 '15 at 07:01
-
Okay.. Can you please provide a link or tutorial over that? I don't know much about it @Amy – Oct 14 '15 at 07:05
-
1You can use https://developer.android.com/training/gestures/detector.html for first information :) – yennsarah Oct 14 '15 at 07:07
2 Answers
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);
}
});
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.

Santanu
- 168
- 7