Vertical fling scrolling of text line in Android

前端 未结 2 642
野的像风
野的像风 2021-02-06 06:50

I have implemented the editor code from the Android Note Pad sample code. Now I would like to add the ability to vertically fling scrolls the lines of text. An example of what I

2条回答
  •  鱼传尺愫
    2021-02-06 07:48

    This is a simpler version, which is basically the same at the scoller level. It's not perfect but gives another way of looking at it.

    final TextView textview = ((TextView) VIEW.findViewById(R.id.text));
    final Scroller scroller = new Scroller(CONTEXT);
    
    textview.setText(TEXT);
    textview.setMovementMethod(new ScrollingMovementMethod());
    textview.setScroller(scroller);
    textview.setOnTouchListener(new View.OnTouchListener() {
    
        // Could make this a field member on your activity
        GestureDetector gesture = new GestureDetector(CONTEXT, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                scroller.fling(0, textview.getScrollY(), 0, (int)-velocityY, 0, 0, 0, (textview.getLineCount() * textview.getLineHeight()));
                return super.onFling(e1, e2, velocityX, velocityY);
            }
    
        });
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gesture.onTouchEvent(event);
            return false;
        }
    });
    

提交回复
热议问题