Reputation: 2386
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
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
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