在viewflippers上滑动冲突

时间:2015-09-04 08:27:03

标签: android

我在我的活动中使用了三个viewflippers,每个都从服务器加载图片。他们三个是动画的,运作良好。关键是我也试图刷它们。我实现了一个手势检测器,这里是代码:

package com.erc.AndroidViewFlipper;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class AndroidViewFlipperActivity extends Activity {

    ViewFlipper page,page2;

    Animation animFlipInForeward;
    Animation animFlipOutForeward;
    Animation animFlipInBackward;
    Animation animFlipOutBackward;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        page = (ViewFlipper)findViewById(R.id.flipper);
        page2 = (ViewFlipper)findViewById(R.id.flipper2);

        animFlipInForeward = AnimationUtils.loadAnimation(this, R.anim.flipin);
        animFlipOutForeward = AnimationUtils.loadAnimation(this, R.anim.flipout);
        animFlipInBackward = AnimationUtils.loadAnimation(this, R.anim.flipin_reverse);
        animFlipOutBackward = AnimationUtils.loadAnimation(this, R.anim.flipout_reverse);

        page.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.flipin));
        page.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.flipout));
        page.startFlipping();
        page.setAutoStart(true);

        page2.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.flipin));
        page2.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.flipout));
        page2.startFlipping();
        page2.setAutoStart(true);



    }

    private void SwipeRight(){
        page.setInAnimation(animFlipInBackward);
        page.setOutAnimation(animFlipOutBackward);
        page.showPrevious();
    }

    private void SwipeLeft(){
        page.setInAnimation(animFlipInForeward);
        page.setOutAnimation(animFlipOutForeward);
        page.showNext();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return gestureDetector.onTouchEvent(event);
    }

    SimpleOnGestureListener simpleOnGestureListener 
    = new SimpleOnGestureListener(){

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

            float sensitvity = 50;
            if((e1.getX() - e2.getX()) > sensitvity){
                SwipeLeft();
            }else if((e2.getX() - e1.getX()) > sensitvity){
                SwipeRight();
            }

            return true;
        }

    };

    GestureDetector gestureDetector
    = new GestureDetector(simpleOnGestureListener);
}

问题出在我触摸屏幕的任何地方,第一个viewflipper都会滑动。我想要的是,当我触摸特定的视图翻转器时,只有被触摸的视图翻转器才会滑动。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

您错误地将同一ViewFlipper个实例分配给pagepage2个变量。在以下代码段中,两个语句都使用相同的视图ID R.id.flipper

    page = (ViewFlipper)findViewById(R.id.flipper);
    page2 = (ViewFlipper)findViewById(R.id.flipper);

[UPDATE]

此外,您需要更新SwipeRight()SwipeLeft()simpleOnGestureListener,如下所示(还添加了一些方法)。代码现在测试是否在ViewFlipper中开始一个fling手势,然后滑动那个翻板。

private void SwipeRight(ViewFlipper vf) { 
    vf.setInAnimation(animFlipInBackward);
    vf.setOutAnimation(animFlipOutBackward);
    vf.showPrevious();
} 

private void SwipeLeft(ViewFlipper vf) { 
    vf.setInAnimation(animFlipInForeward);
    vf.setOutAnimation(animFlipOutForeward);
    vf.showNext();
}

SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() { 

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

        ViewFlipper vf = getTouchedViewFlipper(e1);
        if (vf == null) {
            return true;
        }

        float sensitivity = 50.0; 
        if ((e1.getX() - e2.getX()) > sensitivity) { 
            SwipeLeft(vf); 
        } else if ((e2.getX() - e1.getX()) > sensitivity) { 
            SwipeRight(vf); 
        } 

        return true; 
    } 

};

private ViewFlipper getTouchedViewFlipper(MotionEvent ev) {
    ViewFlipper vf = checkTouchedViewFlipper(page, ev);
    if (vf == null) {
      vf = checkTouchedViewFlipper(page2, ev);
    }
    return vf;
}

private ViewFlipper checkTouchedViewFlipper(ViewFlipper vf, MotionEvent ev) {
    Rect vfRect;
    vf.getHitRect(vfRect);
    return (vfRect.contains((int)ev.getX(), (int)ev.getY())) ? vf : null;
}