Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

smoothScrollToPosition after notifyDataSetChanged not working in android

I've got a custom Adapter for a ListView setup and working ok. On a button click something is changed in the underlying data, so a notifyDataSetChanged is required to refresh the ListView. Fine. But I also want the ListView to scroll to the line where the change occurred. For this I call smoothScrollToPosition immediately after the notifyDataSetChanged. And that is not working.

If I do not call notifyDataSetChanged then the scroll works, so the notify blocks the scroll. I'm probably having a fight with events being processed in the future. Can anybody give me a pointer on what is going wrong?

like image 354
tbeernot Avatar asked Aug 04 '11 13:08

tbeernot


1 Answers

Use the post() method to wait for the list to finish updating after you call notifyDataSetChanged():

adapter.notifyDataSetChanged();
list.post( new Runnable() {
    @Override
    public void run() {
    //call smooth scroll
    }
  });
like image 114
dmon Avatar answered Nov 10 '22 01:11

dmon