Android解决ScrollView中的滑动效果导致GestureDetector中的OnFling不能正常工作问题

原由:在Activity中使用了ScrollView以后,GestureDetector,手势事件不能正常工作,但移到ScrollView外面以后再手势操作,则又能正常工作,这里给出解决方案 覆写 dispatchTouchEvent 函数
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
mGestureDetector.onTouchEvent(ev);
return super.dispatchTouchEvent(ev);
}
看完整实例:
package com.example.gesturedetector;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnGestureListener {

private GestureDetector mGestureDetector;
private Handler handler;
private String result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetector = new GestureDetector(this, this);

handler = new Handler();
new Thread() {
public void run() {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.baidu.com");
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
InputStream in = httpResponse.getEntity().getContent();
try {
result = readString(in);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.post(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(result);
}
});
}
}.start();

}

protected String readString(InputStream in) throws Exception {
byte[] data = new byte[1024];
int length = 0;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
while ((length = in.read(data)) != -1) {
bout.write(data, 0, length);
}
return new String(bout.toByteArray(), "GBK");
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
mGestureDetector.onTouchEvent(ev);
return super.dispatchTouchEvent(ev);
}

@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
// Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}

@Override
public boolean onFling(MotionEvent startEvent, MotionEvent endEvent,
float velocityX, float velocityY) {
// TODO Auto-generated method stub
if (startEvent.getY() - endEvent.getY() > 100) {
Toast.makeText(this, "手势向上滑动", Toast.LENGTH_SHORT).show();
return true;
} else if (startEvent.getY() - endEvent.getY() < -100) {
Toast.makeText(this, "手势向下滑动", Toast.LENGTH_SHORT).show();
return true;
} else if (startEvent.getX() - endEvent.getX() > 100) {
Toast.makeText(this, "手势向左滑动", Toast.LENGTH_SHORT).show();
return true;
} else if (startEvent.getX() - endEvent.getX() < -100) {
Toast.makeText(this, "手势向右滑动", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}

@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
// Toast.makeText(this, "onLongPress ", Toast.LENGTH_SHORT).show();
}

@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
// Toast.makeText(this, "onScroll", Toast.LENGTH_SHORT).show();
return false;
}

@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
// Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
}

@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
// Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return false;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}
注意这里用到了 HttpClient 获取网络数据,具体使用方法及权限问题,可参照文章 Android中使用HttpClient实现HTTP通信效果

你可能感兴趣的:(Android解决ScrollView中的滑动效果导致GestureDetector中的OnFling不能正常工作问题)