Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android listview extra space at the end when scroll

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?

Image 1

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>
like image 627
Alaattin KAYRAK Avatar asked Mar 02 '17 10:03

Alaattin KAYRAK


2 Answers

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:

  1. Lose over scroll animation
  2. Retain animation but risk user seeing bottom border 1 time
like image 114
Gary99 Avatar answered Sep 18 '22 09:09

Gary99


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"/>
like image 36
Zoe stands with Ukraine Avatar answered Sep 22 '22 09:09

Zoe stands with Ukraine