Gestures navigation.
I like fresh approach. And while I was developing my Urban Curiosity application I wanted to make it fresh by solving the common issue in a different manner . So I decided to make the application navigation based on swipe gestures and not on ActionBar, which is cool and handy navigation pattern that is used in near all modern applications.
Why gestures? Well, gestures can be performed on a larger screen area, so you can make a desired action faster and easier if to compare with a finger tap. Just swipe with your finger from any place to the desired direction! Also gestures are fun to use. :-)
In Urban Curiosity application you may navigate across all 4 main parts (Places, Profile, History and More) with a swipe gesture. Regular tap navigation is also available. I've prepared a short demo of the 4-directions (left, right, top, bottom) swipe navigation. The video itself is jaggy a little bit due to the video recording frame rate limitation. You may play with the real app to see how smooth it is. The demo video:
So in this article I want to share how to implement this in your projects.
Implementation.
Horizontal swipes are trivial - ViewPager class from the support library is perfect for this purpose. But what about vertical swipes and how to mix it with horizontal VerticalPager?
There are several open source implementation available. I've played with most of them and of the best of them is an implementation by Grantland Chew https://github.com/grantland/android-verticalpager.
So I chose it as a base for vertical paging. However, to reach my needs I needed to extend it a little bit:
Next, we need to notify somehow VerticalPager (it's part of the activity) from the ViewPager (it's part of the fragment) about pages swipes to allow/disallow vertical scrolling. E.g. for left and right pages there should be no (i.e. disabled) vertical scrolling, while for central page there should be one (swipe to the top or bottom pages).
The common way for fragment-activity or fragment-fragment communication is to:
There are several Android event bus implementations. I wish to highlight three of them here. They were created specifically for Android and I used each of them extensively in a lot of projects. So feel free to choose the one you like more.
For this sample project I chose Otto. It has proven history of usage (at least by me and my team and by Square guys in their apps :-) and pretty easy to integrate and use.
So all communications are pretty easy now:
So, event bus is a powerful tool in your arsenal, but use it with caution.
The last thing I want to mention is ViewPager scrolling behavior. The default ViewPager from support library is a good and mature component. However, when it's used in a scroll based container it becomes hardly usable due to the scroll container touch events interception. I will write a note about soon. Right now you can find here my ViewPager extension that is much much better at resolving scrolling directions.
Sources.
The full sample project with 4-directions swipe navigation is shared at my Bitbucket account:
https://bitbucket.org/NxAlex/swipes-navigation-demo
Let me to know if it helps you or you have some question related to the topic! :-)
I like fresh approach. And while I was developing my Urban Curiosity application I wanted to make it fresh by solving the common issue in a different manner . So I decided to make the application navigation based on swipe gestures and not on ActionBar, which is cool and handy navigation pattern that is used in near all modern applications.
Why gestures? Well, gestures can be performed on a larger screen area, so you can make a desired action faster and easier if to compare with a finger tap. Just swipe with your finger from any place to the desired direction! Also gestures are fun to use. :-)
In Urban Curiosity application you may navigate across all 4 main parts (Places, Profile, History and More) with a swipe gesture. Regular tap navigation is also available. I've prepared a short demo of the 4-directions (left, right, top, bottom) swipe navigation. The video itself is jaggy a little bit due to the video recording frame rate limitation. You may play with the real app to see how smooth it is. The demo video:
So in this article I want to share how to implement this in your projects.
Implementation.
Horizontal swipes are trivial - ViewPager class from the support library is perfect for this purpose. But what about vertical swipes and how to mix it with horizontal VerticalPager?
There are several open source implementation available. I've played with most of them and of the best of them is an implementation by Grantland Chew https://github.com/grantland/android-verticalpager.
So I chose it as a base for vertical paging. However, to reach my needs I needed to extend it a little bit:
- remove the code that shrinks the first page a little bit to make the beginning of the next page visible
- change onMeasure to a better mesuring pages height
- add method to request page snap with a custom duration
- add method to lock/unlock pages switching
Next, we need to notify somehow VerticalPager (it's part of the activity) from the ViewPager (it's part of the fragment) about pages swipes to allow/disallow vertical scrolling. E.g. for left and right pages there should be no (i.e. disabled) vertical scrolling, while for central page there should be one (swipe to the top or bottom pages).
The common way for fragment-activity or fragment-fragment communication is to:
- declare an interface into a fragment
- implement it into an activity
- provide that implementation to the fragment
- finally, the fragment will trigger it when needed (e.g. page changed).
There are several Android event bus implementations. I wish to highlight three of them here. They were created specifically for Android and I used each of them extensively in a lot of projects. So feel free to choose the one you like more.
For this sample project I chose Otto. It has proven history of usage (at least by me and my team and by Square guys in their apps :-) and pretty easy to integrate and use.
So all communications are pretty easy now:
- fragment with a ViewPager will post an PageChangedEvent event
- activity with a VerticalPager will receive that event and decide whether there is any need to lock the vertical paging or not.
So, event bus is a powerful tool in your arsenal, but use it with caution.
The last thing I want to mention is ViewPager scrolling behavior. The default ViewPager from support library is a good and mature component. However, when it's used in a scroll based container it becomes hardly usable due to the scroll container touch events interception. I will write a note about soon. Right now you can find here my ViewPager extension that is much much better at resolving scrolling directions.
Sources.
The full sample project with 4-directions swipe navigation is shared at my Bitbucket account:
https://bitbucket.org/NxAlex/swipes-navigation-demo
Let me to know if it helps you or you have some question related to the topic! :-)
This is great ! Thanks for sharing !
ReplyDeleteThis is a really beutifull example of a clean and simple implementation.
ReplyDeleteI was able to use the demo in no time after compiling and installing on my device.
Well done!
Hi, first of all, thanks for this! I really needed something for vertical swipe. Everything works fine, but I've a question: I'd like to remove the "elastic effect" of page when scrolling the last fragment (i.e. swiping down top fragment), is it complicated? Can you please suggest how to start modifying Vertical Pager source? Many thanks.
ReplyDeleteHow can i reload the fragments ?
ReplyDeleteThis is awesome! thank you
ReplyDeleteHow can I snap horizontally to central fragment?
ReplyDeleteThank you man. You've just become one of my role models. :))
ReplyDeleteThank You So Much !!!
ReplyDeleteThis information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training institute in chennai with placement | Best Android Training in velachery
ReplyDeleteImage recognition application provides a fast and convenient way of searching for products. TruePicShop allowing users to search the product. Take a picture of an object the app will search and redirect you to the shopping site. Shopping with Image Recognition
ReplyDeleteshopping with image recognition
shopping with image recognition
I find swipe gestures to be intuitive and efficient.
ReplyDelete