Skip to content Skip to sidebar Skip to footer

Vertical Fling Scrolling Of Text Line In Android

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 wa

Solution 1:

I have developed the answer to my question through blood, sweat and tears. I will post it here in hope that it will help someone else. The following methods are placed within the LinedEditText class from the Android Note Pad sample code.

============================

publicvoidInitScroller(Context context) {
        mScroller = newScroller(context);       // Get a scroller object
        mScrollY = 0 ;                          // Set beginning of program as top of screen.
        mMinScroll = getLineHeight ()/2;            // Set minimum scroll distance
        mFlingV = 750;                         // Minimum fling velocity

    }

 @OverridepublicbooleanonTouchEvent(MotionEvent event) {
     super.onTouchEvent(event);

  if (mVelocityTracker == null) {                       // If we do not have velocity tracker
         mVelocityTracker = VelocityTracker.obtain();   // then get one
     }
     mVelocityTracker.addMovement(event);               // add this movement to itfinalintaction= event.getAction();  // Get action typefinalfloaty= event.getY();          // Get the displacement for the actionswitch (action) {

     case MotionEvent.ACTION_DOWN:          // User has touched screenif (!mScroller.isFinished()) {     // If scrolling, then stop now
             mScroller.abortAnimation();
         }
         mLastMotionY = y;                  // Save start (or end) of motion
         mScrollY = this.getScrollY();              // Save where we ended up
         mText.setCursorVisible (true);
         didMove = false;

         break;

     case MotionEvent.ACTION_MOVE:          // The user finger is on the move
         didMove = true;
         finalintdeltaY= (int) (mLastMotionY - y);  // Calculate distance moved since last report
         mLastMotionY = y;                             // Save the start of this motionif (deltaY < 0) {                              // If user is moving finger up screenif (mScrollY > 0) {                        // and we are not at top of textintm= mScrollY - mMinScroll;         // Do not go beyond top of textif (m < 0){
                     m = mScrollY; 
                 }elsem= mMinScroll;

              scrollBy(0, -m);                           // Scroll the text up
             }
         } elseif (deltaY > 0) {                           // The user finger is moving upintmax= getLineCount() * getLineHeight () - sHeight;   // Set max up valueif (mScrollY < max-mMinScroll){
                     scrollBy(0, mMinScroll);           // Scroll up
                 }
             }
         postInvalidate();
         break;

     case MotionEvent.ACTION_UP:                       // User finger lifted upfinalVelocityTrackervelocityTracker= mVelocityTracker;      // Find out how fast the finger was moving
         velocityTracker.computeCurrentVelocity(mFlingV);          
         intvelocityY= (int) velocityTracker.getYVelocity();

         if (Math.abs(velocityY) > mFlingV){                                // if the velocity exceeds thresholdintmaxY= getLineCount() * getLineHeight () - sHeight;        // calculate maximum Y movement
             mScroller.fling(0, mScrollY, 0, -velocityY, 0, 0, 0, maxY);    // Do the filng
         }else{
             if (mVelocityTracker != null) {                                // If the velocity less than threshold
                 mVelocityTracker.recycle();                                // recycle the tracker
                 mVelocityTracker = null;
             }
         }
         break;
     }

     mScrollY = this.getScrollY();              // Save where we ended upreturntrue ;                                 // Tell caller we handled the move event
 }



 publicvoidcomputeScroll() {                  // Called while flinging to execute a fling stepif (mScroller.computeScrollOffset()) {      
         mScrollY = mScroller.getCurrY();       // Get where we should scroll to 
         scrollTo(0, mScrollY);                 // and do it
         postInvalidate();                      // the redraw the sreem
     }
 }

Solution 2:

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.

finalTextViewtextview= ((TextView) VIEW.findViewById(R.id.text));
finalScrollerscroller=newScroller(CONTEXT);

textview.setText(TEXT);
textview.setMovementMethod(newScrollingMovementMethod());
textview.setScroller(scroller);
textview.setOnTouchListener(newView.OnTouchListener() {

    // Could make this a field member on your activityGestureDetectorgesture=newGestureDetector(CONTEXT, newGestureDetector.SimpleOnGestureListener() {
        @OverridepublicbooleanonFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            scroller.fling(0, textview.getScrollY(), 0, (int)-velocityY, 0, 0, 0, (textview.getLineCount() * textview.getLineHeight()));
            returnsuper.onFling(e1, e2, velocityX, velocityY);
        }

    });

    @OverridepublicbooleanonTouch(View v, MotionEvent event) {
        gesture.onTouchEvent(event);
        returnfalse;
    }
});

Post a Comment for "Vertical Fling Scrolling Of Text Line In Android"