I've just started writing a custom view in Android (for the first time) and I've realised that I need to implement a scrolling feature.
The custom view also uses a header containing some text (which should stay fixed and not scroll).
I've read through the documentation on GestureDetector.SimpleOnGestureListener and Scroller. I also read the documentation on Animating a Scroll Gesture but I found the examples difficult to understand. I've also looked at other questions on Stack Overflow which helped a little.
Using what I understood from the documentation with the Stack Overflow answer as a reference to guide me, I have added the following to my custom view:
Variables and fields:
private OverScroller mScroller; private final GestureDetector mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // Note 0 as the x-distance to prevent horizontal scrolling scrollBy(0, (int) distanceY); return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { final int maxScrollX = 0; // wholeViewHeight is height of everything that is drawn int wholeViewHeight = calculateWholeHeight(); int visibleHeight = getHeight(); final int maxScrollY = wholeViewHeight - visibleHeight; mScroller.forceFinished(true); mScroller.fling(0, // No startX as there is no horizontal scrolling getScrollY(), 0, // No velocityX as there is no horizontal scrolling - (int) velocityY, 0, maxScrollX, 0, maxScrollY); invalidate(); return true; } @Override public boolean onDown(MotionEvent e) { if (!mScroller.isFinished()) { mScroller.forceFinished(true); } return true; } }); Initialization of mScroller:
// Called from the constructor private void init() { mScroller = new OverScroller(getContext(), new FastOutLinearInInterpolator()); ... } Stuff in onDraw():
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); ... if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); } } Stuff in onTouchEvent():
@Override public boolean onTouchEvent(MotionEvent event) { return mGestureDetector.onTouchEvent(event); } The result of these additions is a custom view which can scroll vertically (and not horizontally), however there are a few issues:
- I can scroll further past what is drawn
- There is no edge glow effect as I reach the end of the custom view (I mean like a
RecyclerVieworScrollView) - All of the custom view scrolls as opposed to just a certain part of it
- I don't fully understand what is going on
Could someone explain how scrolling works in a custom view and how to implement it properly with these features?