I have experienced strange behaviour with smooth scroll on listview. I have 30 items in list. When I smooth scroll via
listview.smoothScrollToPositionFromTop(29, 0);
there will be some extra space at the end of screen. Actually listview shifts up a little. Image attached below. But if I scroll manually there will be no space. What can be done to resolve this behaviour?
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
As you have said, I think it's an Android issue. It appears to be part of the animation for over scroll, it seems to get 'stuck' before completing the animation. I've had similar experience with SwipeToRefresh
, where the progress indicator doesn't fully disappear after refresh.
Anyway, you can workaround by turning off the animation
listView.setOverScrollMode(View.OVER_SCROLL_NEVER);
Unfortunately, this loses the animation even if manually scrolled. If you can live with that, I think this will fix your problem.
AbsListView#setOverScrollMode(int)
EDIT: (after original answer was accepted)
I have also found another work around. This one preserves the over scroll animation. Unfortunately, it has another drawback. If the user auto scrolls before doing anything else, the same problem of the border at the bottom exists. If the user manually scrolls to the bottom 1st, no problem from then on. If the user auto scrolls and gets the border, then manually scrolls, no more problem. So, here is the alternate method.
Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.invisible_footer, null);
listView.setOverscrollFooter(drawable);
In the drawable folder, "invisible_footer.xml"
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="2dp"
android:width="10dp"
android:viewportWidth="10"
android:viewportHeight="2">
<!-- a horizontal line -->
<path android:pathData="M 1 1 H 10"
android:strokeColor="#00000000" android:strokeWidth="1" />
</vector>
ListView.html#setOverscrollHeader()
This gives choices:
Did you try to set the height of your ListView to wrap_content
instead of match_parent
?
Even in [this tutorial] all the attributes are set to wrap_content
. So replace your ListView attributes on width and height to:
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With