Stealth Rabbi
Stealth Rabbi

Reputation: 10346

Swipe to switch tab selection

I have an application that is a Tab activity with various tabs that load an Activity. I would like to add swipe gesturing to switch between tabs, on both the tab control itself and each activity loaded in a tab. Is something like this possible? I'm not sure at what "depth" the gesture is listened to when you're making gestures on an Activity hosted in the tab control, or if the inner activities would have to pass data to the Tab Host.

Upvotes: 5

Views: 8048

Answers (2)

das_weezul
das_weezul

Reputation: 6142

You can use a gesture detector. As far as I know it listens to gestures "globally". To restrict it to the tab you could check whether the gesture has occurred inside the boundaries of the tab.

GestureDetector

Detects various gestures and events using the supplied MotionEvents. The GestureDetector.OnGestureListener callback will notify users when a particular motion event has occurred. This class should only be used with MotionEvents reported via touch (don't use for trackball events). To use this class:

  • Create an instance of the GestureDetector for your View In the nTouchEvent(MotionEvent) method ensure you call
  • onTouchEvent(MotionEvent). The methods defined in your callback will be executed when the events occur

Source: http://developer.android.com/reference/android/view/GestureDetector.html

Swipe Gesture

This page shows how to recognize a swipe-gesture using the GestureDetector: http://smartandroidians.blogspot.com/2010/04/swipe-action-and-viewflipper-in-android.html

Upvotes: 2

Yoni Samlan
Yoni Samlan

Reputation: 38065

It's certainly possible; you'll need to write a custom layout and override onTouchEvent and onInterceptTouchEvent to decide whether to dispatch gestures through to the children . See HorizontalPager's source for details on how you could go about doing that. You actually may just want to use HorizontalPager straight up (there's a demo on simulating tabs with it), as I think the scrolling content makes it much clearer that you can swipe between tabs.

Upvotes: 3

Related Questions