Reputation: 163
I am developing app with chat and I want my ListView
to scroll to bottom when new message been posted by user and when user was at the bottom of the list and there are new messages recieved. I am using this ListView
:
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp"
android:stackFromBottom="true"
android:paddingBottom="9dp"
android:clipToPadding="false"/>
And this method to force scroll when needed:
private void scrollListViewToBottom() {
listView.setSelection(adapter.getCount() - 1);
}
But when chat contains element (big text or image) with height bigger than screen height it is scrolling to the top of it. I need my ListView
to scroll exactly to the bottom, not to the last element.
I tried to use listView.scrollTo(0, listView.getBottom())
method but the result is very strange - it is scrolling sometimes to the last message + half-screen gap and sometimes to the place where I cannot see any messages.
Any ideas? Thanks.
Upvotes: 3
Views: 8676
Reputation: 2535
You can use android:stackFromBottom="true"
for populating list from bottom to top and add android:transcriptMode="alwaysScroll"
for auto scrolling your listview.
You just have to call notifyDataSetChanged()
and your list will be auto scrolled.
For handling scrolling to absolute bottom in case of large blocks, you can add list items with height of 1 px below every list item so that you will always be directed to end of list no matter what you item's height is.
Upvotes: 14