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

来源:互联网 发布:华为智能遥控软件 编辑:程序博客网 时间:2024/05/04 12:39
原由:在Activity中使用了ScrollView以后,GestureDetector,手势事件不能正常工作,但移到ScrollView外面以后再手势操作,则又能正常工作,这里给出解决方案覆写 dispatchTouchEvent 函数
@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubmGestureDetector.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;@Overrideprotected 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 blocke.printStackTrace();}} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}handler.post(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubTextView 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");}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubmGestureDetector.onTouchEvent(ev);return super.dispatchTouchEvent(ev);}@Overridepublic boolean onDown(MotionEvent arg0) {// TODO Auto-generated method stub// Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();return false;}@Overridepublic boolean onFling(MotionEvent startEvent, MotionEvent endEvent,float velocityX, float velocityY) {// TODO Auto-generated method stubif (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;}@Overridepublic void onLongPress(MotionEvent arg0) {// TODO Auto-generated method stub// Toast.makeText(this, "onLongPress ", Toast.LENGTH_SHORT).show();}@Overridepublic 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;}@Overridepublic void onShowPress(MotionEvent arg0) {// TODO Auto-generated method stub// Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();}@Overridepublic boolean onSingleTapUp(MotionEvent arg0) {// TODO Auto-generated method stub// Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();return false;}@Overridepublic 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通信效果
1 0
原创粉丝点击