onFling无法触发

 

今天使用 ViewFlipper 来实现手势的时候 onFling的事件怎么也触发不了 .

 

我的XML中含有Gridview,没有scrollview,也无法触发onfling,后来加上以下代码

 @Override
 public boolean dispatchTouchEvent(MotionEvent ev) {
                 myGestureDetector.onTouchEvent(ev);
                 gridview.onTouchEvent(ev);
                 return super.dispatchTouchEvent(ev);

         }

能够捕获到touch event事件

 

后来找到原因 ,原来是加入ScrollView后就没法响应了。

再后来看到说用dispatchTouchEvent(MotionEvent ev) 的方法,果然可以,不过帖子没说具体怎么办,查了些文档,试验了几个方法,果然找到了一个,和大家分享下。
具体原因我也不是很明白,可能是因为ScrollView抢占了MotionEvent 事件,所以GestureDetector
捕获不到任何的touch event,自然就不会相应了,方法是@override
public boolean dispatchTouchEvent(MotionEvent ev) {
                mGestureDetector.onTouchEvent(ev);
                scroll.onTouchEvent(ev);
                return super.dispatchTouchEvent(ev);

        }

然后发送MotionEvent分别给你的GestureDetector和ScrollView,而不是dispatchMotionEvent,我猜测dispatch后会被ScrollView先取走,从而GestureDetector就无法响应了,所以这里就都派发出去

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public class MainActivity extends AppCompatActivity { Button view_button; private Context mContext; private ViewFlipper vflp_help; private int[] resId = {R.drawable.img01,R.drawable.img02, R.drawable.img03}; private final static int MIN_MOVE = 200; //最小距离 private MyGestureListener mgListener; private GestureDetector mDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view_button=findViewById(R.id.view_button); view_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } }); mContext = MainActivity.this; //实例化SimpleOnGestureListener与GestureDetector对象 mgListener = new MyGestureListener(); mDetector = new GestureDetector(this, mgListener); vflp_help = (ViewFlipper) findViewById(R.id.vflp_help); //动态导入添加子View for(int i = 0;i < resId.length;i++){ vflp_help.addView(getImageView(resId[i])); } } //重写onTouchEvent触发MyGestureListener里的方法 @Override public boolean onTouchEvent(MotionEvent event) { return mDetector.onTouchEvent(event); } //自定义一个GestureListener,这个是View类下的,别写错哦!!! private class MyGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) { if(e1.getX() - e2.getX() > MIN_MOVE){ vflp_help.setInAnimation(mContext,R.anim.right_in); vflp_help.setOutAnimation(mContext, R.anim.right_out); vflp_help.showNext(); }else if(e2.getX() - e1.getX() > MIN_MOVE){ vflp_help.setInAnimation(mContext,R.anim.left_in); vflp_help.setOutAnimation(mContext, R.anim.left_out); vflp_help.showPrevious(); } return true; } } private View getImageView(int resId){ ImageView img = new ImageView(this); img.setBackgroundResource(resId); return img; }}
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值