Android :Changing tabs on Fling Gesture Event- not working for ListActivity inside TabActivity
I have a tabactivity with three tabs. One of the tab contains ListActivity and two others contains simple Activity. And i have implemented OnGestureListener to change the tab on fling event.
The onTouchEvent
and 'onFling' events are not being called for the tab with ListActivity . But working fine for Simple Activity( without lists).
Here is my Code :
Main Activity class :
public class GestureTest extends TabActivity implements OnGestureListener {
private TabHost tabHost;
private GestureDetector gestureScanner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testmainlayoutforgesture);
gestureScanner = new GestureDetector(this);
addTabs();
}
private void addTabs() {
Intent i1 = new Intent().setClass(this, SimpleListActivity.class);
Intent i2 = new Intent().setClass(this, SimpleActivity.class);
Intent i3 = new Intent().setClass(this, SimpleActivity.class);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("First").setIndicator("First").setContent(i1));
tabHost.addTab(tabHost.newTabSpec("Second").setIndicator("Second").setContent(i2));
tabHost.addTab(tabHost.newTabSpec("Third").setIndicator("Third").setContent(i3));
tabHost.setCurrentTab(0);
}
@Override
public boolean onTouchEvent(MotionEvent me) {
return gestureScanner.onTouchEvent(me);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float dispX = e2.getX() - e1.getX();
float dispY = e2.getY() - e1.getY();
if (Math.abs(dispX) >= 200 && Math.abs(dispY) <= 100) {
// swipe ok
if (dispX > 0) {
// L-R swipe
System.out.println("L-R swipe");
changeLtoR();
} else {
// R-L swipe
System.out.println("R开发者_开发知识库-L swipe");
}
}
return true;
}
private void changeLtoR() {
int curTab = tabHost.getCurrentTab();
int nextTab = ((curTab + 1) % 3);
tabHost.setCurrentTab(nextTab);
}
//other interface methods
}
SimpleListActivity.java:
public class SimpleListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> list1Strings = new ArrayList<String>();
list1Strings.add("List 11");
list1Strings.add("List 12");
list1Strings.add("List 13");
list1Strings.add("List 14");
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
}
}
SimpleActivity.java :
public class SimpleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simplelayouttest);
}
}
testmainlayoutforgesture.xml layout :
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
simplelayouttest.xml layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/helloTv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello Hello" />
</LinearLayout>
I could not figure out what is wrong here.
Try adding this to your TabActivity:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (gestureScanner != null) {
if (gestureScanner.onTouchEvent(ev))
return true;
}
return super.dispatchTouchEvent(ev);
}
Implementing this, will tell your device that the gestureScanner consumed the onFling event.
EDIT Again:
You will also need to change your onFling-method a bit to this:
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH, then dismiss the swipe.
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// Swipe from right to left.
// The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE) and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// do stuff
return true;
}
// Swipe from left to right.
// The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE) and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// do stuff
return true;
}
return false;
}
}
Another possibility could be if your ListView does not fill the screen and the 'empty' space is not clickable and you fling on this 'empty' space the touch event won't be registered and dispatched. Does it work if you fling on top of the ListView?
精彩评论