Make skipping ListView in Android

I am trying to make my list a bounce. To explain myself, I want ListView to have the same behavior as an iOs object view object. At the top and bottom of the list, the user can scroll through the list by checking his finger.

This behavior existed on Samsung Android 2.2 devices (e.g. Galaxy Tab GT1000).

On most tested devices, the list now works differently when scrolling, it displays a blue line, which becomes brighter when you swipe your finger.

I found a BounceListView like this:

public class BounceListView extends ListView { private static final int MAX_Y_OVERSCROLL_DISTANCE = 200; private Context mContext; private int mMaxYOverscrollDistance; public BounceListView(Context context) { super(context); mContext = context; initBounceListView(); } public BounceListView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; initBounceListView(); } public BounceListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; initBounceListView(); } private void initBounceListView() { //get the density of the screen and do some maths with it on the max overscroll distance //variable so that you get similar behaviors no matter what the screen size final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); final float density = metrics.density; mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE); } @Override protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) { //This is where the magic happens, we have replaced the incoming maxOverScrollY with our own custom variable mMaxYOverscrollDistance; return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent); } } 

But the problem with this ListView is that it does not return to the first or last item after scrolling through the list ... It remains in the position where the list does not fill.

Does anyone have an idea to make it work?

Thanks in advance!

+4
source share
1 answer

You must override onOverScrolled , which is called to say that the list has been scrolled, and in this function, scroll the ListView back to the point you want using smoothScrollToPosition .

It looks something like this:

 @Override protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) { if(scrollY < 0) { smoothScrollToPosition(0); } else if(scrollY > MAX_SCROLL) { smoothScrollToPosition(getAdapter().getCount()); } } 

MAX_SCROLL should be determined by you using the height of your list items and the number of elements in your adapter, although it looks like you already understood this in your question, so this should not be a problem.

+3
source

Source: https://habr.com/ru/post/1434184/


All Articles