Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 between entire activities (including action bar)? And I don't mean like viewPager swapping between fragments, I mean swapping the entire screen (like snapchat does on iOS). Is this possible at all? And if so, how can I do this?

2) What is the best way to implement a master/detail type layout? Like twitter for instance, if i have a listView of the tweets, and when a person clicks on a tweet it takes you to a detailed view of that specific tweet... What would be the best way to accomplish this? Would I have an activity with a list view and create a second activity upon clicking on a tweet? Or would I use fragments instead, one for the list view and one for the detailed view?

3) Is there a way to have different action bars for every fragment?

Thank you so very much!!!

like image 999
Gus Avatar asked Dec 19 '22 13:12

Gus


2 Answers

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;

public class OnSwipeTouchListener implements OnTouchListener {



private final 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
 */
public OnSwipeTouchListener(Context context) {
    super();
    this.context = context;
    gestureDetector = new GestureDetector(context, new GestureListener());
}

private final class GestureListener extends SimpleOnGestureListener {

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

    /* (non-Javadoc)
     * @see android.view.GestureDetector.SimpleOnGestureListener#onDown(android.view.MotionEvent)
     */
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    /* (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.
 */
public void onSwipeRight() {
}

/**
 * On swipe left.
 */
public void onSwipeLeft() {
}

/**
 * On swipe top.
 */
public void onSwipeTop() {
}

/**
 * On swipe bottom.
 */
public void onSwipeBottom() {
}
}

Implementation:

OnSwipeTouchListener onSwipeTouchListener = new OnSwipeTouchListener(Activity.this) {
        @Override
        public void onSwipeLeft() {
            //your actions
        }
    };
like image 109
Droidekas Avatar answered Dec 27 '22 11:12

Droidekas


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.

like image 24
Rod_Algonquin Avatar answered Dec 27 '22 11:12

Rod_Algonquin