设置GestureDetector所有子视图 [英] Set GestureDetector to all child views

查看:361
本文介绍了设置GestureDetector所有子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想补充一个GestureDetector到活动中的所有视图(查看组),而无需手动分配到每一个单一的视图。现在onFling()刷在背景时才会被激活,但不能在如刷卡时,按钮1。

 包com.app.example;

进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.GestureDetector;
进口android.view.MotionEvent;
进口android.view.GestureDetector.SimpleOnGestureListener;
进口android.view.View;
进口android.view.View.OnTouchListener;
进口android.widget.Button;
进口android.widget.LinearLayout;

公共类为ExampleActivity延伸活动{
    上下文mContext = NULL;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        mContext =这一点;

        的LinearLayout父=新的LinearLayout(mContext);
        parent.setOrientation(LinearLayout.VERTICAL);
        parent.setBackgroundColor(Color.RED);

        按钮按钮1 =新的按钮(mContext);
        button1.setText(按钮1);
        按钮按钮2 =新按钮(mContext);
        button2.setText(按钮2);

        parent.addView(按钮1);
        parent.addView(按钮2);

        最后GestureDetector gestureDetector =新GestureDetector(
                新MyGestureDetector());

        parent.setOnTouchListener(新OnTouchListener(){
            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                gestureDetector.onTouchEvent(事件);

                返回true;
            }
        });

        的setContentView(父);
    }
}

类MyGestureDetector扩展SimpleOnGestureListener {

    @覆盖
    公共布尔onFling(MotionEvent E1,E2 MotionEvent,
            浮velocityX,浮动velocityY){

        //从右到左
        如果(e1.getX() -  e2.getX()大于10&安培;&安培; Math.abs(velocityX)→20){
            Log.i(onFling,右到左);

            返回false;

        // 从左到右
        }否则如果(e2.getX() -  e1.getX()大于10&安培;&安培; Math.abs(velocityX)→20){
            Log.i(onFling,左到右);

            返回false;
        }

        返回false;
    }

}
 

解决方案

您可以使用的 GestureOverlayView 看到一个 previus计算器后如何使用它。

I would like to add a GestureDetector to all views (view groups) of an activity without assigning it manually to every single view. Right now onFling() is only activated when swiping over the background but not when swiping on e.g. button1.

package com.app.example;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class ExampleActivity extends Activity {
    Context mContext = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;

        LinearLayout parent = new LinearLayout(mContext);
        parent.setOrientation(LinearLayout.VERTICAL);
        parent.setBackgroundColor(Color.RED);

        Button button1 = new Button(mContext);
        button1.setText("Button 1");
        Button button2 = new Button(mContext);
        button2.setText("Button 2");

        parent.addView(button1);
        parent.addView(button2);

        final GestureDetector gestureDetector = new GestureDetector(
                new MyGestureDetector());

        parent.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                gestureDetector.onTouchEvent(event);

                return true;
            }
        });

        setContentView(parent);
    }
}

class MyGestureDetector extends SimpleOnGestureListener {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
            float velocityX, float velocityY) {

        // right to left
        if(e1.getX() - e2.getX() > 10 && Math.abs(velocityX) > 20) {
            Log.i("onFling", "right to left");

            return false;

        // left to right
        }  else if (e2.getX() - e1.getX() > 10 && Math.abs(velocityX) > 20) {
            Log.i("onFling", "left to right");

            return false; 
        }

        return false;
    }

}

解决方案

You can use a GestureOverlayView see a previus stackoverflow post on how to use it.

这篇关于设置GestureDetector所有子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆