使用GestureDetector在ScrollView中选择TextView

时间:2017-02-10 18:46:08

标签: android android-layout textview android-scrollview gesturedetector

我的布局中有TextView。如果我设置:

android:textIsSelectable="true"

用户可以选择和复制文本(部分或全部 - 它是ipmortant)。但是ScrolViews GestureDetector没有检测到TextView上的任何手势(例如onFling)

   GestureDetector gesturedetector = new GestureDetector(smsDetail.this,
            new OnGestureListener() {

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

                @Override
                public void onShowPress(MotionEvent e) {

                }

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

                @SuppressWarnings("deprecation")
                @Override
                public void onLongPress(MotionEvent e) {

                }

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

                    return false;
                }

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

            , null);
    OnTouchListener gesturelistner = (v, event) -> gesturedetector.onTouchEvent(event);
    scrolloView.setOnTouchListener(gesturelistner);

如果设置

android:textIsSelectable="false"

GestureDetector效果很好。

如何使用GestureDetector和可选TextView

1 个答案:

答案 0 :(得分:0)

我的洗礼:

textView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        gesturedetector.onTouchEvent(event);
        return false;
    }
});