android滑动监听 上下滑动 左右滑动

1.获取布局文件或视图View

  1. 获取布局文件
    这种方式的滑动感应范围是整个布局。
private void getTouchView(){
	// 获取布局文件
	LayoutInflater inflater = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	view = inflater.inflate(R.layout.main, null);
	// 设置滑动监听
	setOnLayoutTouchListener();
}
  1. 获取视图View
    这种方式的滑动感应范围是View区域。
private void getTouchView(){
	rv_informations = rootView.findViewById(R.id.rv_informations);
	// 设置滑动监听
	setOnViewTouchListener();
}

2.设置滑动监听事件

  1. 获取布局文件
private void setOnLayoutTouchListener(){
	view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        posX = event.getX();
                        posY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        curPosX = event.getX();
                        curPosY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        if ((curPosX - posX > 0) && (Math.abs(curPosX - posX) > 25)){
                            Log.v(TAG,"向左滑动");
                        }
                        else if ((curPosX - posX < 0) && (Math.abs(curPosX-posX) > 25)){
                            Log.v(TAG,"向右滑动");
                        }
                        if ((curPosY - posY > 0) && (Math.abs(curPosY - posY) > 25)){
                            Log.v(TAG,"向下滑动");
                        }
                        else if ((curPosY - posY < 0) && (Math.abs(curPosY-posY) > 25)){
                            Log.v(TAG,"向上滑动");
                        }
                        break;
                }
                return true;
            }
        });
}
  1. 获取视图View
private void setOnViewTouchListener(){
	rv_informations.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        posX = event.getX();
                        posY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        curPosX = event.getX();
                        curPosY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        if ((curPosX - posX > 0) && (Math.abs(curPosX - posX) > 25)){
                            Log.v(TAG,"向左滑动");
                        }
                        else if ((curPosX - posX < 0) && (Math.abs(curPosX-posX) > 25)){
                            Log.v(TAG,"向右滑动");
                        }
                        if ((curPosY - posY > 0) && (Math.abs(curPosY - posY) > 25)){
                            Log.v(TAG,"向下滑动");
                        }
                        else if ((curPosY - posY < 0) && (Math.abs(curPosY-posY) > 25)){
                            Log.v(TAG,"向上滑动");
                        }
                        break;
                }
                return true;
            }
        });
}

3.调试截图

android滑动监听 上下滑动 左右滑动_第1张图片

你可能感兴趣的:(Android)