Skip to content

Instantly share code, notes, and snippets.

@sreereddymenon
Forked from creativepsyco/BBChatViewHost.java
Last active September 3, 2015 18:08
Show Gist options
  • Save sreereddymenon/7d93ebe5c2994737c34f to your computer and use it in GitHub Desktop.
Save sreereddymenon/7d93ebe5c2994737c34f to your computer and use it in GitHub Desktop.
How to scroll Android Listview to the bottom always
public void scrollBottom() {
// scroll bottom requires posting a runnable to the listView
// in the case of items loading by themselves dynamically
// there may be sync issues and scrolling won't be proper
m_bNeedScrollToBottom = false;
if (m_listView.getLastVisiblePosition()-m_listView.getFirstVisiblePosition() <= m_listView.getCount())
m_listView.setStackFromBottom(false);
else
m_listView.setStackFromBottom(true);
BBUILoop.getInstance().delayPost(new Runnable() {
@Override
public void run() {
int nCount = mItemHostSection.getCount();
BBAppLogger.i("Want Scroll bottom:%d", nCount);
if (nCount > 0 && m_listView != null) {
m_listView.clearFocus();
m_listView.setSelection(m_listView.getAdapter().getCount() - 1);
// This seems to work
m_listView.post(new Runnable() {
@Override
public void run() {
m_listView.setSelection(m_listView.getAdapter().getCount() - 1);
}
});
}
}
}, 400);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment