Search code examples
androidandroid-listviewautoscrollandroid-scroll

Toggle autoscroll of ListView - when scrolled interactively by user


I have a real app - which you can see on the left in the below screenshot.

And I have prepared a very simple test app at GitHub, which you can see at the right:

screenshot

The real app has a ListView at the bottom, which is filled with Bluetooth-related log messages. This happens pretty often, few times a second and it is impossible for the user to scroll up and look at a particular event - because the list jumps to the bottom by itself, when new events are added to the list.

My question is: How to disable autoscrolling of the ListView - when user scrolls up and how to enable autoscrolling again, when user scrolls to the bottom?

And here is my very simple test app:

As you can see the auto-scrolling is currently enabled by XML attributes in layout:

<ListView 
    android:id="@+id/myListView"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"        
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Solution

  • Okay, I have found a perfect solution for my problem:

    <ListView 
        android:id="@+id/myListView"
        android:stackFromBottom="true"
        android:transcriptMode="normal"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    When the transcriptMode is set to normal, then:

    The list will automatically scroll to the bottom when a data set change notification is received and only if the last item is already visible on screen.

    Here is my updated test project (I've also added double tap listener to clear the ListView there)

    screenshot