gab*_*jan 31 android swipe touch-event
我的方法有问题
@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
Run Code Online (Sandbox Code Playgroud)
是更新的.任何想法为什么会这样?我正在构建一个google的api 4.0.3应用程序,我正在尝试为我的viewFliper启用滑动.但是,如果触摸更新,它就无法工作.
BTW:
public class MainActivity extends SherlockMapActivity implements ActionBar.TabListener {
Run Code Online (Sandbox Code Playgroud)
那是我活动的宣言.并检测我实施的滑动:
SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
float sensitvity = 50;
if((e1.getX() - e2.getX()) > sensitvity){
SwipeLeft();
}else if((e2.getX() - e1.getX()) > sensitvity){
SwipeRight();
}
return true;
}
};
GestureDetector gestureDetector= new GestureDetector(simpleOnGestureListener);
Run Code Online (Sandbox Code Playgroud)
感谢你!
编辑:
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ViewFlipper
android:id="@+id/ViewFlipper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<include
android:layout_height="match_parent"
layout="@layout/mymain" />
<include layout="@layout/secondmain" />
<include layout="@layout/thirdmain" />
<include layout="@layout/fourthmain" />
</ViewFlipper>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
edit2:我所有包含的布局都实现了scrollview,滚动可以捕获那些事件并处理它们.以及如何检测手势?
gab*_*jan 46
我找到了完美的解决方案.我实现了新方法:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);
Run Code Online (Sandbox Code Playgroud)
现在一切正常!
编辑:
我的最终代码:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
if (v instanceof EditText) {
View w = getCurrentFocus();
int scrcoords[] = new int[2];
w.getLocationOnScreen(scrcoords);
float x = event.getRawX() + w.getLeft() - scrcoords[0];
float y = event.getRawY() + w.getTop() - scrcoords[1];
if (event.getAction() == MotionEvent.ACTION_UP
&& (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w
.getBottom())) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus()
.getWindowToken(), 0);
}
}
boolean ret = super.dispatchTouchEvent(event);
return ret;
}
Run Code Online (Sandbox Code Playgroud)
Log*_*ic1 12
正如我在Gabrjan的帖子中所写的那样,在触摸屏幕的同时不断触发,实际上只有一种简单的方法可以获得触地事件:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
System.out.println("TOUCH DOWN!");
//set immersive mode here, or whatever...
}
return super.dispatchTouchEvent(event);
}
Run Code Online (Sandbox Code Playgroud)
这对于我来说非常有用,只要触摸屏幕的任何部分而不管哪个元素,都可以将Android置于沉浸式模式.但我不想反复设置沉浸式模式!
好的,现在我确定问题是scrollview句柄接触,所以无论如何要忽略它,但是滚动可用吗?
是的,这就是问题,当android处理每个事件从子节点到父节点的触摸事件时,首先它由ViewFlipper处理,但随后它转到ScrollView.因此,您必须实现getParent().requestDisallowInterceptTouchEvent(true)(请参阅ViewParent类),以便使所有触摸事件由ViewFlipper处理,然后只是检测手势的方向,如果水平然后翻转视图,如果没有,则将触摸事件传递给ScrollView或者只是以编程方式滚动ScrollView
编辑:您也可以在ViewFlipper和此侦听器触发器GestureDetector.onTouchEvent(event)中实现OnTouchListener,但这也需要将父视图的requestDisallowInterceptTouchEvent设置为true
| 归档时间: |
|
| 查看次数: |
29321 次 |
| 最近记录: |