view实现手势滑动

监听一个view向左还是向右的滑动主要用到GestureDetector;

@Layout(R.layout.right_fragment_menu)
public class RightFragment extends BaseFragment {
	@BindView(id=R.id.linout_right,canClick=true)
	private LinearLayout linout_right; 
	private GestureDetector mGestureDetector;
	private SlidingMenu _SlidingMenu;
	
	public RightFragment(SlidingMenu _SlidingMenu){
		this._SlidingMenu=_SlidingMenu;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		//findView();
	}
	
	private void findView(){
		mGestureDetector = new GestureDetector(getActivity(), new MyOnGestureListener());
	 linout_right.setOnTouchListener(new OnTouchListener() {
			
		@Override
		public boolean onTouch(View arg0, MotionEvent event) {
			 //Log.i(getClass().getName(), "onTouch-----" + getActionName(event.getAction()));
			mGestureDetector.onTouchEvent(event);
	        // 一定要返回true,不然获取不到完整的事件
	        return true;
		}
	});}
	@Override
	protected void onClick(View v) {
		
	}

    class MyOnGestureListener extends SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

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

            float moveX = e1.getX() - e2.getX();

            // 左滑
            if (moveX > 150 && moveX < 5000) {
              // mDesignClothesBackground
              // .setBackgroundResource(idClothesBackground[0]);
            	System.out.println("向左滑动");
            }
            // 右滑
            else if (moveX < -150 && moveX > -5000) {
              // mDesignClothesBackground
              // .setBackgroundResource(idClothesBackground[1]);
            	System.out.println("向右滑动");
            	if(_SlidingMenu!=null){
            		_SlidingMenu.showContent();
            	}
            }
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return false;
        }
    }

}


你可能感兴趣的:(onFling,gesturedetector)