尝试检测滑动以更改ViewFlipper中的视图

时间:2011-05-11 17:38:59

标签: android gesture swipe

我正在尝试实施滑动检测以更改ViewFlipper中的视图。我使用了从there复制的下面显示的代码。

触摸onTouch时正确触发ViewFlipper,onFling事件从不触发。

有人可以帮忙吗?检测滑动的最佳方法是什么?

感谢

public class HomeActivity extends Activity {   

    ViewFlipper viewFlipper;

    private GestureDetector gestureDetector;

    private Animation slideLeftIn, slideLeftOut, slideRightIn, slideRightOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.home);        
        LinearLayout home = (LinearLayout) findViewById(R.id.home);

        // Gesture detection
        gestureDetector = new GestureDetector(new MyGestureDetector());

        // Info popup
        ImageView btn_open_popup = (ImageView) findViewById(R.id.btn_popup);

        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);

        // Compute size according to density
        float density = getResources().getDisplayMetrics().density;
        final int width = (int) (295.0f * density);
        final int height = (int) (307.0f * density);         

        btn_open_popup.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                LayoutInflater inflater = (LayoutInflater) HomeActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                View popup_container = inflater.inflate(R.layout.popup_container,null, false);

                final PopupWindow pw = new PopupWindow(popup_container, width, height, true);

                pw.showAtLocation(findViewById(R.id.home), Gravity.CENTER, 0,0);    

                ImageView btn_close_popup = (ImageView) popup_container.findViewById(R.id.btn_close_popup);
                btn_close_popup.setAlpha(120);

                btn_close_popup.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        pw.dismiss();
                    }
                });

                // set swipe listener to viewFlipper
                viewFlipper=(ViewFlipper)popup_container.findViewById(R.id.popup_flipper);
                viewFlipper.setOnTouchListener(new OnTouchListener() {

                    public boolean onTouch(View v, MotionEvent event) {
                        if (gestureDetector.onTouchEvent(event)) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                });
            }
        });          
    }    

    class MyGestureDetector extends SimpleOnGestureListener {

        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 250;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            System.out.println(" in onFling() :: ");
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                viewFlipper.setInAnimation(slideRightIn);
                viewFlipper.setOutAnimation(slideLeftOut);
                viewFlipper.showNext();
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                viewFlipper.setInAnimation(slideLeftIn);
                viewFlipper.setOutAnimation(slideRightOut);
                viewFlipper.showPrevious();
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    }    

}

0 个答案:

没有答案