翻转视图翻转时不出现点击事件

发布于 2024-12-05 19:55:03 字数 3968 浏览 1 评论 0原文

我正在开发自定义的ViewFlipper,其中我通过重写flipper的OnTouch()来翻转页面。但我的问题是我无法获取添加到脚蹼中的孩子的触摸事件。并且仅在 ViewFlipper 空白部分获取直接触摸事件。那么我如何在启用子点击事件的所有页面上获取触摸事件。这是我的自定义 ViewFlipper 代码,

谢谢。

public class MyViewFlipper extends ViewFlipper {

    static final String logTag = "ViewFlipper";
    static final int MIN_DISTANCE = 30;
    private float downX, downY, upX, upY;
    Animation slideLeftIn;
    Animation slideLeftOut;
    Animation slideRightIn;
    Animation slideRightOut;
    Context context;
    ViewFlipper viewFlipper;

    public MyViewFlipper(Context context) {
        super(context);
        viewFlipper=this;
         this.context=context;
    }

    public MyViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
         viewFlipper=this;
         slideLeftIn = AnimationUtils.loadAnimation(context, R.anim.slide_left_in);
         slideLeftOut = AnimationUtils.loadAnimation(context, R.anim.slide_left_out);
         slideRightIn = AnimationUtils.loadAnimation(context, R.anim.slide_right_in);
         slideRightOut = AnimationUtils.loadAnimation(context, R.anim.slide_right_out);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
         switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                downX = event.getX();
                downY = event.getY();
                return true;
            }
            case MotionEvent.ACTION_UP: {
                upX = event.getX();
                upY = event.getY();

                float deltaX = downX - upX;
                float deltaY = downY - upY;

                // swipe horizontal?
                if (Math.abs(deltaX) > MIN_DISTANCE) {
                    // left or right
                    if (deltaX < 0) {
                        this.onLeftToRightSwipe();
                        return true;
                    }
                    if (deltaX > 0) {
                        this.onRightToLeftSwipe();
                        return true;
                    }
                } else {
                    if(Math.abs(deltaX)<15){
                        onClickEvent();
                    }
                    //Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
                    //        + " long, need at least " + MIN_DISTANCE);
                }
                // swipe vertical?
                if (Math.abs(deltaY) > MIN_DISTANCE) {
                    // top or down
                    if (deltaY < 0) {
                        this.onTopToBottomSwipe();
                        return true;
                    }
                    if (deltaY > 0) {
                        this.onBottomToTopSwipe();
                        return true;
                    }
                } else {
                    Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
                            + " long, need at least " + MIN_DISTANCE);
                }

                return true;
            }
            }
            return false;

    }

     public void onRightToLeftSwipe() {

        viewFlipper.setInAnimation(slideLeftIn);
        viewFlipper.setOutAnimation(slideLeftOut);
        viewFlipper.showNext();
    }

    public void onLeftToRightSwipe() {

        viewFlipper.setInAnimation(slideRightIn);
        viewFlipper.setOutAnimation(slideRightOut); 
        viewFlipper.showPrevious();
    }

    public void onTopToBottomSwipe() {
        Log.i(logTag, "onTopToBottomSwipe!");
        // activity.doSomething();
    }

    public void onBottomToTopSwipe() {
        Log.i(logTag, "onBottomToTopSwipe!");
        // activity.doSomething();
    }

    public void onClickEvent(){
        Toast.makeText(context, "Click",Toast.LENGTH_SHORT);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }



}

I am working on customized ViewFlipper in which I flip the pages by overriding OnTouch() of flipper. But my problem is m not able to get touch event on child which are added into flipper. And get direct touch event only on ViewFlipper empty part. So how I can get touch event on all of page with enabled child click event. Here is my custom ViewFlipper code

Thank You.

public class MyViewFlipper extends ViewFlipper {

    static final String logTag = "ViewFlipper";
    static final int MIN_DISTANCE = 30;
    private float downX, downY, upX, upY;
    Animation slideLeftIn;
    Animation slideLeftOut;
    Animation slideRightIn;
    Animation slideRightOut;
    Context context;
    ViewFlipper viewFlipper;

    public MyViewFlipper(Context context) {
        super(context);
        viewFlipper=this;
         this.context=context;
    }

    public MyViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
         viewFlipper=this;
         slideLeftIn = AnimationUtils.loadAnimation(context, R.anim.slide_left_in);
         slideLeftOut = AnimationUtils.loadAnimation(context, R.anim.slide_left_out);
         slideRightIn = AnimationUtils.loadAnimation(context, R.anim.slide_right_in);
         slideRightOut = AnimationUtils.loadAnimation(context, R.anim.slide_right_out);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
         switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                downX = event.getX();
                downY = event.getY();
                return true;
            }
            case MotionEvent.ACTION_UP: {
                upX = event.getX();
                upY = event.getY();

                float deltaX = downX - upX;
                float deltaY = downY - upY;

                // swipe horizontal?
                if (Math.abs(deltaX) > MIN_DISTANCE) {
                    // left or right
                    if (deltaX < 0) {
                        this.onLeftToRightSwipe();
                        return true;
                    }
                    if (deltaX > 0) {
                        this.onRightToLeftSwipe();
                        return true;
                    }
                } else {
                    if(Math.abs(deltaX)<15){
                        onClickEvent();
                    }
                    //Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
                    //        + " long, need at least " + MIN_DISTANCE);
                }
                // swipe vertical?
                if (Math.abs(deltaY) > MIN_DISTANCE) {
                    // top or down
                    if (deltaY < 0) {
                        this.onTopToBottomSwipe();
                        return true;
                    }
                    if (deltaY > 0) {
                        this.onBottomToTopSwipe();
                        return true;
                    }
                } else {
                    Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
                            + " long, need at least " + MIN_DISTANCE);
                }

                return true;
            }
            }
            return false;

    }

     public void onRightToLeftSwipe() {

        viewFlipper.setInAnimation(slideLeftIn);
        viewFlipper.setOutAnimation(slideLeftOut);
        viewFlipper.showNext();
    }

    public void onLeftToRightSwipe() {

        viewFlipper.setInAnimation(slideRightIn);
        viewFlipper.setOutAnimation(slideRightOut); 
        viewFlipper.showPrevious();
    }

    public void onTopToBottomSwipe() {
        Log.i(logTag, "onTopToBottomSwipe!");
        // activity.doSomething();
    }

    public void onBottomToTopSwipe() {
        Log.i(logTag, "onBottomToTopSwipe!");
        // activity.doSomething();
    }

    public void onClickEvent(){
        Toast.makeText(context, "Click",Toast.LENGTH_SHORT);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2024-12-12 19:55:03

在您的自定义 viewFlipper 中删除

onInterceptTouchEvent()

,您将返回 true 这意味着,孩子们不会收到触摸事件。

Remove

onInterceptTouchEvent()

in your custom viewFlipper, you are returning true which means , children will not get touch events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文