Android: How To Swipe Between Activities (not Fragments), Master/detail Best Set-up
I'm working on an android app and I'm fairly new to all this, including mobile app development, so I have a few questions. Any help would be amazing! 1) Is it possible to swipe bet
Solution 1:
Yes swiping is possible:
OnTouchSwipeListener
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;
publicclassOnSwipeTouchListener implements OnTouchListener {
privatefinal GestureDetector gestureDetector;
private Context context;
/* (non-Javadoc)
* @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
*/public boolean onTouch(final View view, final MotionEvent motionEvent){
return gestureDetector.onTouchEvent(motionEvent);
}
/**
* Gets the gesture detector.
*
* @return the gesture detector
*/public GestureDetector getGestureDetector(){
return gestureDetector;
}
/**
* Instantiates a new on swipe touch listener.
*
* @param context
* the context
*/publicOnSwipeTouchListener(Context context){
super();
this.context = context;
gestureDetector = newGestureDetector(context, newGestureListener());
}
privatefinalclassGestureListener extends SimpleOnGestureListener {
privatestaticfinalint SWIPE_THRESHOLD = 100;
privatestaticfinalint SWIPE_VELOCITY_THRESHOLD = 100;
/* (non-Javadoc)
* @see android.view.GestureDetector.SimpleOnGestureListener#onDown(android.view.MotionEvent)
*/
@Override
public boolean onDown(MotionEvent e){
returntrue;
}
/* (non-Javadoc)
* @see android.view.GestureDetector.SimpleOnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
boolean result = false;
try {
float diffY = e2.getRawY() - e1.getRawY();
float diffX = e2.getRawX() - e1.getRawX();
if ((Math.abs(diffX) - Math.abs(diffY)) > SWIPE_THRESHOLD) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
} catch (Exception e) {
}
return result;
}
}
/**
* On swipe right.
*/publicvoidonSwipeRight(){
}
/**
* On swipe left.
*/publicvoidonSwipeLeft(){
}
/**
* On swipe top.
*/publicvoidonSwipeTop(){
}
/**
* On swipe bottom.
*/publicvoidonSwipeBottom(){
}
}
Implementation:
OnSwipeTouchListeneronSwipeTouchListener=newOnSwipeTouchListener(Activity.this) {
@OverridepublicvoidonSwipeLeft() {
//your actions
}
};
Solution 2:
Is it possible to swipe between entire activities (including action bar)?
No you cant, you can only do it with Fragments not with Activities.
What is the best way to implement a master/detail type layout?
You can use fragment and just add it to the current layout. Another solution is to create a dialog that will have layout.
Is there a way to have different action bars for every fragment?
Only activity can have ActionBar but you can still make your own ActionBar by creating it using layout and inflate it to the fragment.
Solution 3:
in addition to @Mirek Rusin answer
OnSwipeTouchListener.java:
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;
publicclassOnSwipeTouchListenerimplementsOnTouchListener {
private final GestureDetector gestureDetector;
publicOnSwipeTouchListener (Context ctx){
gestureDetector = newGestureDetector(ctx, newGestureListener());
}
@OverridepublicbooleanonTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final classGestureListenerextendsSimpleOnGestureListener {
privatestatic final int SWIPE_THRESHOLD = 100;
privatestatic final int SWIPE_VELOCITY_THRESHOLD = 100;
@OverridepublicbooleanonDown(MotionEvent e) {
returntrue;
}
@OverridepublicbooleanonFling(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;
}
}
elseif (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;
}
}
publicvoidonSwipeRight() {
}
publicvoidonSwipeLeft() {
}
publicvoidonSwipeTop() {
}
publicvoidonSwipeBottom() {
}
}
Usage on your activity : define a layout (Linear or relative or anything even a toolbar or an image..)
yourLayout.setOnTouchListener(new OnSwipeTouchListener(MyActivity.this) {
publicvoidonSwipeTop() {
Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
}
publicvoidonSwipeRight() {
startActivity(new Intent(MyActivity.this,NewActivity.class));
finish();
}
publicvoidonSwipeLeft() {
Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
}
publicvoidonSwipeBottom() {
Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}
});
Solution 4:
Actually you can swipe between activities. Check this library.Also take a look at this link
Post a Comment for "Android: How To Swipe Between Activities (not Fragments), Master/detail Best Set-up"