Compaq LE2202x
Compaq LE2202x

Reputation: 2386

How to detect if ListView items are enough to trigger scrolling

I've having a chat app, if the ListView items are not enough to scroll then the row should be at the top of the list. If the items are enough to trigger scroll then the list should scroll always scroll down to show the latest conversation like in the iMessage setup.

I tried adding in the XML:

android:stackFromBottom="true"

but this would cause the ListView to scroll down ALWAYS. I only want to scroll down if the items are enough to cause scrolling, if not then it should scroll up.

I would use this if I could detect the enabling of scroll:

myListView.post(new Runnable() {
    @Override
    public void run() {
        // Select the last row so it will scroll into view...
        myListView.setSelection(myListAdapter.getCount() - 1);
    }
});

So how would I detect if the ListView items are enough to trigger scroll?

Upvotes: 2

Views: 1056

Answers (2)

Compaq LE2202x
Compaq LE2202x

Reputation: 2386

I fixed this by placing this code listView.setSelection(rowAdapter.getCount()); after listView.setAdapter(rowAdapter);. When chat messages are only few, it is scrolled up and when the messages are enough to trigger scroll then it is scrolled to the bottom. Just like I want it.

Upvotes: 0

kidnan1991
kidnan1991

Reputation: 366

You could try this to detect the scrollable

if (yourListView.getLastVisiblePosition() == yourListView.getAdapter().getCount() -1)
{
 //It is scrolled all the way down here
}

to check the last visible item is still smaller the item number :)

Upvotes: 1

Related Questions