ViewFlipper 不接收触摸事件

发布于 2024-11-30 00:10:11 字数 4433 浏览 1 评论 0原文

我正在开发一个 Android 项目,其中使用 ViewFlipper 来翻转子项。为此,我使用了定制的脚蹼。在此我重写了 onTouchEvent() 方法。 鳍状肢包含每个子项中的图像,但当我尝试翻转它时,它没有收到任何触摸事件。当我重写 ViewGroup 方法 onInterceptTouchEvent() 并返回 true 时,翻转完成,但由于这个我的 onClickEvent()<未收到每个图像上的 /code>。

我不明白问题出在哪里。当我从 onInterceptTouchEvent() 返回 false 时,将收到点击事件,但不会收到触摸事件。

我缺少什么?

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;
        System.out.println("I am in MyFlipper() counstructor...");
    }

    public MyViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        viewFlipper=this;
        System.out.println("I am in MyFlipper() counstructor...");
        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;    // Here if true then Flipping done.
                        // And if false then click event done. 
    }

}

I am working on an Android project in which I use a ViewFlipper for flipping childs. For this I used a customized flipper. In this I override the onTouchEvent() method.
The flipper contain images in each child but when I try to flip it doesn't receive any touch event. When I override the ViewGroup method onInterceptTouchEvent() and return true, then flipping is done but due to this my onClickEvent() on each image is not received.

I am not getting where the problem is. When I return false from onInterceptTouchEvent() then the click event is being received but not the touch event.

What am I missing?

Code for the 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;
        System.out.println("I am in MyFlipper() counstructor...");
    }

    public MyViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        viewFlipper=this;
        System.out.println("I am in MyFlipper() counstructor...");
        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;    // Here if true then Flipping done.
                        // And if false then click event done. 
    }

}

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

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

发布评论

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

评论(1

瀞厅☆埖开 2024-12-07 00:10:11

我有类似的问题。
这就是我所做的。我的视图翻转器中有一个列表视图,我重写了列表视图的 setOnTouchListener() 并从那里对视图翻转器进行了动画处理。这对我有用。

尝试在您的情况下覆盖 ImageViewsetOnTouchListener()

I had a similar problem.
Here is what i did. I had a listview in my view flipper and i override the setOnTouchListener() of my listview and animated the view flipper from there. It worked for me.

Try overriding the setOnTouchListener() of ImageView in your case.

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