首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebView TextSelection在添加MotionEvent后消失

WebView TextSelection在添加MotionEvent后消失
EN

Stack Overflow用户
提问于 2016-10-27 07:13:36
回答 1查看 61关注 0票数 1

大家好,我正在创建一个epubreader并将书加载到一个webview中,我使webview滚动水平添加了一些css规则,现在我添加了按屏幕宽度向左和右滑动的页面,我使用这个类获得了屏幕以检测左、右滑动功能。

代码语言:javascript
复制
public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener(Context context) {
    gestureDetector = new GestureDetector(context, new GestureListener());
}

public void onSwipeLeft() {
}

public void onSwipeRight() {
}

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

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_DISTANCE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        float distanceX = e2.getX() - e1.getX();
        float distanceY = e2.getY() - e1.getY();
        if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
            if (distanceX > 0)
                onSwipeRight();
            else
                onSwipeLeft();
            return true;
        }
        return false;
    }

 }
}

在我的主要活动中,像这样实现

代码语言:javascript
复制
   webView.setOnTouchListener(new OnSwipeTouchListener(this) {
                        @Override
                        public void onSwipeLeft() {
                            // Whatever
                            if(TotalPages>PresentPage){
                            PresentPage++;
                            forward.start();
                            webView.scrollTo(PresentPage*ScreenWidth, 0);

                            }
                            else{
                                Toast.makeText(getApplicationContext(), "The End", 100).show();
                            }
                        }
                        @Override
                        public void onSwipeRight() {

                            if(PresentPage>0)
                            {
                            PresentPage--;
                            back.start();
                            webView.scrollTo(PresentPage*ScreenWidth, 0);
                            }
                        }
                    });

现在,手势左和右工作良好,但默认的文本选择功能(即长按压选择文本)已经消失,我想要默认的文本选择与滑动检测,我如何获得它?提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-15 08:46:05

使用这个类来解决它

代码语言:javascript
复制
public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;
private static final int MAX_CLICK_DURATION = 1000;
private static final int MAX_CLICK_DISTANCE = 15;
static Context mcontext;
private long pressStartTime;
private float pressedX;
private float pressedY;
private boolean stayedWithinClickDistance;

public OnSwipeTouchListener(Context context) {
    gestureDetector = new GestureDetector(context, new GestureListener());
    this.mcontext=context;
}

public void onSwipeLeft() {
}

public void onSwipeRight() {
}
public void newTouch(){

}
public boolean onTouch(View v, MotionEvent event) {
      gestureDetector.onTouchEvent(event);

     if(event.getAction()== MotionEvent.ACTION_MOVE)
     {
          if (stayedWithinClickDistance && distance(pressedX, pressedY, event.getX(), event.getY()) > MAX_CLICK_DISTANCE) {
              stayedWithinClickDistance = false;
          }
         return true;
     }

     else if (event.getAction()== MotionEvent.ACTION_DOWN) {
         pressStartTime = System.currentTimeMillis();                
         pressedX = event.getX();
         pressedY = event.getY();
         stayedWithinClickDistance = true;

         return v.onTouchEvent(event);
     }
     else if(event.getAction()== MotionEvent.ACTION_UP) {

         long pressDuration = System.currentTimeMillis() - pressStartTime;
         if (pressDuration < MAX_CLICK_DURATION && stayedWithinClickDistance) {
            newTouch();
         }

         return v.onTouchEvent(event);
     }
     else{
         return v.onTouchEvent(event);
     }

}
private static float distance(float x1, float y1, float x2, float y2) {
    float dx = x1 - x2;
    float dy = y1 - y2;
    float distanceInPx = (float) Math.sqrt(dx * dx + dy * dy);
    return pxToDp(distanceInPx);
}

private static float pxToDp(float px) {
    return px / mcontext.getResources().getDisplayMetrics().density;
}


private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_DISTANCE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        float distanceX = e2.getX() - e1.getX();
        float distanceY = e2.getY() - e1.getY();
        if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
            if (distanceX > 0)
                onSwipeRight();
            else
                onSwipeLeft();
            return true;
        }
        return false;
    }

}


}

以及主要活动

代码语言:javascript
复制
 webView.setOnTouchListener(new OnSwipeTouchListener(this) {
                        @Override
                        public void onSwipeLeft() {
                            // Whatever
                             // for swiping left action
                            }

                        @Override
                        public void onSwipeRight() {
                            // TODO Auto-generated method stub

                            //for swiping right action

                        }
                        @Override
                        public void newTouch() {

                           // for single tap action

                        }

                    });

在此之后,您仍然有默认的文本选择选项。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40278390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档