如何在Android中的视图寻呼机中启用点击监听器?

Nan*_*ncy 7 android android-viewpager

我正在使用view pager在Android中的视图之间滑动.现在我需要为每个视图捕获tap事件.当我覆盖触摸侦听器以捕获点击事件时,滑动操作不会发生,并且屏幕保留在第一页本身.如何添加触摸侦听器以查看寻呼机?

码:

viewPager.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {

                     mDetector.onTouchEvent(event);
                     return true;   



            }});
Run Code Online (Sandbox Code Playgroud)

对于上面的代码,我能够捕获点击事件,但是滑动动作变为不可能.

Jor*_*lar 26

在这里,我给我留下一个代码片段来检测OnTouchListener上的"点击",我希望它有帮助

mImagePager.setOnTouchListener(new OnTouchListener() {
        private float pointX;
        private float pointY;
        private int tolerance = 50;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
            case MotionEvent.ACTION_MOVE:
                return false; //This is important, if you return TRUE the action of swipe will not take place.
            case MotionEvent.ACTION_DOWN:
                pointX = event.getX();
                pointY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                boolean sameX = pointX + tolerance > event.getX() && pointX - tolerance < event.getX();
                boolean sameY = pointY + tolerance > event.getY() && pointY - tolerance < event.getY();
                if(sameX && sameY){
                    //The user "clicked" certain point in the screen or just returned to the same position an raised the finger
                }
            }
            return false;
        }
    });
Run Code Online (Sandbox Code Playgroud)


Soh*_*ham 2

Nancy,您不需要手动覆盖页面滑动或触摸事件。只需将页面添加到 ViewPager,ViewPager 就会自动处理滑动操作。

但是,您必须将触摸侦听器附加到每个页面中的对象。因此,如果第 1 页有一个包含许多按钮的线性布局,并且您需要找出这些按钮何时被单击,则需要为每个按钮附加 OnClickListener。

请让我知道您的用例,以便我们更好地理解为什么您需要找出页面何时被点击!