GithubHelp home page GithubHelp logo

sockeqwe / swipeback Goto Github PK

View Code? Open in Web Editor NEW
695.0 31.0 117.0 256 KB

SwipeBack for Android Activities to do pretty the same as the android "back-button" will do, but in a really intuitive way by using a swipe gesture

License: Apache License 2.0

Java 100.00%

swipeback's Introduction

SwipeBack

SwipeBack is for Android Activities to do pretty the same as the android "back-button" will do, but in a really intuitive way by using a swipe gesture

Not Actively Maintained

Warning: this project is not actively maintained. It works, but don't expect any future improvements or major bug fixes but I'm willing to merge any Pull Request.

Demo

kicker app

Kicker app

See demo video

Dependency

SwipeBack is available on maven central

compile 'com.hannesdorfmann:swipeback:1.0.4'

How to use it

It's not supported yet to build it from xml. You simply have to set it up in you Activities onCreate() method. Instead of Activity.setContentView() call SwipeBack.setContentView().

public class SwipeBackActivity extends FragmentActivity{

	@Override
	public void onCreate(Bundle saved){
		super.onCreate(saved);

		// Init the swipe back
		SwipeBack.attach(this, Position.LEFT)
		    .setContentView(R.layout.activity_simple)
		    .setSwipeBackView(R.layout.swipeback_default);

	}


	@Override
	public void onBackPressed(){
		super.onBackPressed();
		overridePendingTransition(R.anim.swipeback_stack_to_front,
				R.anim.swipeback_stack_right_out);
	}
}

The code above will use the default setup. R.layout.swipeback_default, the default swipe back layout is already provided by this library as well as DefaultSwipeBackTransformer, R.anim.swipeback_stack_to_front, R.anim.swipeback_stack_to_back, R.anim.swipeback_stack_right_in and R.anim.swipeback_stack_right_out.

Customization

The most important thing is the SwipeBackTransformer. This interface provides an API that will be called from the SwipeBack class. Here is where you implement frame by frame animation while the swipe back view will become open (by users swipe gesture). Additionally you can customize the SwipeBack position, the drag mode (drag content or drag window) and if it should be drawn as overlay or not (Type.BEHIND or Type.OVERLAY).

    /**
	 * Attaches the SwipeBack to the Activity.
	 *
	 * @param activity
	 *            The activity the swipe back will be attached to.
	 * @param type
	 *            The {@link SwipeBack.Type} of the drawer.
	 * @param position
	 *            Where to position the swipe back.
	 * @param dragMode
	 *            The drag mode of the drawer. Can be either
	 *            {@link SwipeBack#DRAG_CONTENT} or
	 *            {@link SwipeBack#DRAG_WINDOW}.
	 * @return The created SwipeBack instance.
	 */
	public static SwipeBack attach(Activity activity, Type type, Position position, int dragMode, SwipeBackTransformer transformer)

You can also draw a divider between the normal content view and the swipe back view and a overlay that will fade out while opening the swipe back view.

SwipeBack.attach(this, Position.LEFT)
		.setDrawOverlay(true)
		.setDivider(drawable)
		.setDividerEnabled(true) // Must be called to enable, setDivider() is not enough
		.setSwipeBackTransformer(new SlideSwipeBackTransformer())
		.setContentView(R.layout.activity_simple)
		.setSwipeBackView(R.layout.swipeback_default);

ViewPager

To distinguish a ViewPager swipe gesture from a SwipeBack swipe gesture you have to setup a OnInterceptMoveEventListener:

public class ViewPagerActivity extends FragmentActivity {

	private ViewPager mViewPager;
	private int mPagerPosition;
	private int mPagerOffsetPixels;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SwipeBack.attach(this, Position.LEFT)
		.setContentView(R.layout.activity_view_pager)
		.setSwipeBackView(R.layout.swipeback_default)
		.setDividerAsSolidColor(Color.WHITE)
		.setDividerSize(2)
		.setOnInterceptMoveEventListener(
				new OnInterceptMoveEventListener() {
					@Override
					public boolean isViewDraggable(View v, int dx,
							int x, int y) {
						if (v == mViewPager) {
							return !(mPagerPosition == 0 && mPagerOffsetPixels == 0)
									|| dx < 0;
						}

						return false;
					}
				});


		mViewPager = (ViewPager) findViewById(R.id.viewPager);
		mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager()));

		mViewPager.setOnPageChangeListener(new SimpleOnPageChangeListener(){
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				mPagerPosition = position;
				mPagerOffsetPixels = positionOffsetPixels;
			}

		});
	}


}

Why?

The Samsung Galaxy Nexus was one of the first device without hardware buttons for "back", "home" and "app switching (multitasking)" but used the androids navigation bar on screen (introduced in Android 4.0). The navigation bar was at least in my opinion a big step forward, especially on screen rotation from landscape to portrait and vice versa. But I asked myself, do we really need a navigation bar? I mean the navigation bar takes ca. 10 % of the whole screen. Even at the home screen the "back" and "home" button is useless (because they do nothing). So I thought to myself: Why do we not use swipe gestures instead of a navigation bar? But maybe this idea is to futuristic and not suitable for all kind of android user. A few years later apple introduced the swipe back gesture in iOS 7. Why doesn't Android Apps use swipe gesture as alternative to the back button. Pinterest and Tumblr do so, but at least they use a single Activity and a ViewPager. The problem with this approach is:

  1. You will lost a little bit the ability to jump to any screen by using intents. Take Pinterest as an example: If you get a push notification from Pinterest and you click on it you will see a loading dialog on screen. Internal the navigation stack is generated by adding Fragments to the ViewPager.

  2. ActionBar: The ActionBar is as default not part of a fragment, but it's part of the activity. So you can not (by using a ViewPager) use the default ActionBar to swipe back to the previous Fragment, because the ActionBar will remain sticky. So you have to implement you own ActionBar and attach that to the fragments view.

My approach can be used for activities. It does pretty the same as the android menu drawers do. It adds an aditional layout and slides the content or the window to the side.

Thanks

  • Simon Vig Therkildsen: The most code of handling swipe gestures has been taken from his android-menudrawer library.

swipeback's People

Contributors

michalbednarski avatar moltak avatar mrhether avatar sockeqwe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swipeback's Issues

How to make swipe back transparent?

I like your library but, I want to make transparent swipeback layout from left or other side. How to make transparent background when swipe back to previous activity like Pinterest? Tks

How could I add this into an Activity containing a content Fragment?

public class ItemDescriptionActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, ItemDescriptionFragment.newInstance())
                    .commit();
        }
}

Your sample code shows how to add swipe back gesture to an Activity, however, it misses the part explaining Activity that contains Fragment.

ev.getX(ev.findPointerIndex(mActivePointerId)); crashes due to bad pointer index

java.lang.IllegalArgumentException: pointerIndex out of range
       at android.view.MotionEvent.nativeGetAxisValue(MotionEvent.java)
       at android.view.MotionEvent.getX(MotionEvent.java:2014)
       at com.hannesdorfmann.swipeback.SlidingSwipeBack.onInterceptTouchEvent(SlidingSwipeBack.java:601)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1960)
       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
       at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)
       at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
       at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
       at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330)
       at android.view.View.dispatchPointerEvent(View.java:8666)
       at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
       at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
       at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
       at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
       at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807)
       at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781)
       at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5752)
       at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897)
       at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
       at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
       at android.os.MessageQueue.next(MessageQueue.java:143)
       at android.os.Looper.loop(Looper.java:122)
       at android.app.ActivityThread.main(ActivityThread.java:5254)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

I have a question

Why is it that the background color of the edge is black? Can you customize it?
thank you

Limit area in activity for swipe back

Love your library. Is there a way to limit the swipe back area on the activity? I am running into an issue when trying to scroll around on a webView, if I scroll right the swipe back gets activated. Would like to limit it to a smaller area if you know what I mean.

Thanks

NullPointerException in VerticalTextView.onDraw

Previous versions worked good but in last one I am getting big amount of crashes with this log:

Fatal Exception: java.lang.NullPointerException
       at com.hannesdorfmann.swipeback.view.VerticalTextView.onDraw(VerticalTextView.java:54)
       at android.view.View.draw(View.java:15278)
       at android.view.View.getDisplayList(View.java:14172)
       at android.view.View.getDisplayList(View.java:14214)
       at android.view.View.draw(View.java:14992)
       at android.view.ViewGroup.drawChild(ViewGroup.java:3322)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3158)
       at android.view.View.draw(View.java:15281)
       at android.view.View.getDisplayList(View.java:14172)
       at android.view.View.getDisplayList(View.java:14214)
       at android.view.View.draw(View.java:14992)
       at android.view.ViewGroup.drawChild(ViewGroup.java:3322)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3158)
       at com.hannesdorfmann.swipeback.BuildLayerFrameLayout.dispatchDraw(BuildLayerFrameLayout.java:83)
       at android.view.View.draw(View.java:15281)
       at android.widget.FrameLayout.draw(FrameLayout.java:472)
       at android.view.View.getDisplayList(View.java:14172)
       at android.view.View.getHardwareLayerDisplayList(View.java:14199)
       at android.view.View.getHardwareLayer(View.java:13925)
       at android.view.View.buildLayer(View.java:13853)
       at com.hannesdorfmann.swipeback.BuildLayerFrameLayout$2.run(BuildLayerFrameLayout.java:95)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:146)
       at android.app.ActivityThread.main(ActivityThread.java:5694)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
       at dalvik.system.NativeStart.main(NativeStart.java)

java.lang.IllegalArgumentException: Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed in android P

java.lang.IllegalArgumentException: Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed
at android.graphics.Canvas.checkValidClipOp(Canvas.java:779)
at android.graphics.Canvas.clipRect(Canvas.java:918)
at com.hannesdorfmann.swipeback.view.VerticalTextView.draw(VerticalTextView.java:45)
at android.view.View.updateDisplayListIfDirty(View.java:19082)
at android.view.View.draw(View.java:19935)
at android.view.ViewGroup.drawChild(ViewGroup.java:4333)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112)
at android.view.View.updateDisplayListIfDirty(View.java:19073)
at android.view.View.draw(View.java:19935)
at android.view.ViewGroup.drawChild(ViewGroup.java:4333)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112)
at com.hannesdorfmann.swipeback.BuildLayerFrameLayout.dispatchDraw(BuildLayerFrameLayout.java:83)
at android.view.View.draw(View.java:20210)
at android.view.View.updateDisplayListIfDirty(View.java:19082)
at android.view.View.draw(View.java:19935)
at android.view.ViewGroup.drawChild(ViewGroup.java:4333)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112)
at com.hannesdorfmann.swipeback.SwipeBack.dispatchDraw(SwipeBack.java:796)
at android.view.View.draw(View.java:20210)
at android.view.View.updateDisplayListIfDirty(View.java:19082)
at android.view.View.draw(View.java:19935)
at android.view.ViewGroup.drawChild(ViewGroup.java:4333)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4112)
at android.view.View.draw(View.java:20210)
at com.android.internal.policy.DecorView.draw(DecorView.java:780)
at android.view.View.updateDisplayListIfDirty(View.java:19082)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:686)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:692)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:801)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3311)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3115)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2484)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

How change text caption?

I cannot find method to change text when i drag fragment

ex

 SwipeBack.attach(this, Position.LEFT)
                .setContentView(R.layout.fragment_blank)
                .setSwipeBackView(R.layout.swipeback_default)
                .setText("BACK TO MAIN");

SurfaceView is black

That's good ! But one question is the SurfaceView will show black . How can I resolve it ? Thanks

I want to remove SwipeBackView?

Hello,
I want to remove SwipeBackView? When you swipe left to right to back first activity without swipebackview. hELP ME PLEASE

When I call popBackStack() in a fragmentActivity, It occurred insane screen

Hi. I'm moltak.
Your library is so cool. But It has few problems. So I want to fix it. But library is so complex. Can you help me?
I found problem when call popBackStack() in a fragmentActivity. If you pop a fragment from fragment stack, You'll see a problem. When pop a fragment, back fragment has a insane background.
2014-06-03 9 53 33

and How can I see a back activity when I'm swiping a front activity. Can you give a hint for me?
thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.