Android:更改 Fling 手势事件上的选项卡 - 不适用于 TabActivity 内的 ListActivity
我有一个带有三个选项卡的选项卡活动。其中一个选项卡包含 ListActivity,另外两个选项卡包含简单的 Activity。我已经实现了 OnGestureListener 来更改 fling 事件的选项卡。
使用 ListActivity 不会为选项卡调用 onTouchEvent
和 'onFling' 事件。但对于简单活动(没有列表)工作得很好。
这是我的代码:
Main Activity 类:
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 布局:
<?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 布局:
<?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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将其添加到您的 TabActivity 中:
实现此操作将告诉您的设备,gestureScanner 消耗了 onFling 事件。
再次编辑:
您还需要将您的 onFling 方法稍微更改为:
另一种可能是,如果您的 ListView 没有填满屏幕,并且“空”空间不可点击,并且您猛击在这个“空”空间中,触摸事件不会被注册和调度。如果你在 ListView 上滑动,它会起作用吗?
Try adding this to your TabActivity:
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:
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?