Skip to content Skip to sidebar Skip to footer

Android Smooth Vertical Scroll On Touch

I have a view that's inside a frame layout. I am tracking the ontouch event for the view and I want the view to scroll vertically in a smooth scrolling fashion up and down as I mov

Solution 1:

try this one (you can add some checks at the top/bottom or fling as well):

classScrollingTextViewextendsTextView {
    private GestureDetector mDetector;

    publicScrollingTextView(Context context) {
        super(context);
        StringBuildersb=newStringBuilder();
        for (inti=0; i < 30; i++) {
            sb.append("Line #").append(i).append("\n");
        }
        setTextSize(24);
        setText(sb);
        setTextColor(0xffeeeeee);
        mDetector = newGestureDetector(mListener);
    }

    privateOnGestureListenermListener=newSimpleOnGestureListener() {
        @OverridepublicbooleanonScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            scrollBy(0, (int) distanceY);
            returntrue;
        }
    };

    @OverridepublicbooleanonTouchEvent(MotionEvent event) {
        mDetector.onTouchEvent(event);
        returntrue;
    }
}

to test it add the following in Activity.onCreate:

Viewv=newScrollingTextView(this);
setContentView(v);

Solution 2:

I've observed that dinamycally changing properties such as margins, isn't as smooth as I'd like. Instead of that I use TranslateAnimations; the trick is applying animations during the scrolling, and changing the real margins on touch up.

Post a Comment for "Android Smooth Vertical Scroll On Touch"