35

I want to know when BaseAdapter.notifyDataSetChanged() finish and then do sonething. My code:

BaseAdapter.notifyDataSetChanged();  
mScroller.startScroll(oldX, 0, dx, 0);

mScroller scrolls before notifyDataSetChanged() finishes. I want mScroller to scroll to oldX after notifyDataSetChanged() finishes.

Please help me, thank you.

earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
hiepnh
  • 437
  • 1
  • 8
  • 9
  • 11
    watch [this](http://stackoverflow.com/questions/6942582/smoothscrolltoposition-after-notifydatasetchanged-not-working-in-android) question. possible duplicate. – grine4ka Nov 22 '12 at 07:07
  • may be https://stackoverflow.com/a/20997828/5255006 will help you – Krunal Kapadiya Jul 26 '17 at 08:00

4 Answers4

10

Instead of extending, BaseAdapter supports registering a DataSetObserver where you can listen for the onChanged event.

The notifyDataSetChanged method will cause this event to fire.

adapter.registerDataSetObserver(new DataSetObserver() {
    @Override
    public void onChanged() {
        // ...
    }
});

...

adapter.notifyDataSetChanged();
Cord Rehn
  • 1,119
  • 14
  • 22
  • 1
    Unfortunately the method `onChanged()` is called mutiple times and not only when the view layout has finished. – PJ_Finnegan Oct 09 '20 at 15:21
2

I had the same exact problem, try this out it worked perfectly for me. You are Overriding the method notifyDataSetChanged(). You do this in your Adapter class. Just copy the code i posted and replace the line where I setSelection() with what ever you need done.

@Override
public void notifyDataSetChanged()
{
    super.notifyDataSetChanged();

    parentGlobal.setSelection(this.getCount() - 1);  //This is how we start Activity fully scrolled to bottom
}

With that said, in case you are wondering, "parentGlobal" is the ListView that I set the Adapter to in my Activity. In the constructor of the Adapter, I passed in the ListView and then made it "global". I apologize if "global" isn't a Java term, I come from a C++ world.

//Goes in Activity:

lv_main = (ListView) findViewById(R.id.listView_conversation);
    adapter_main = new Adapter_Conversation(ConversationActivity.this, R.layout.layout_list_conversation,
            list_main_UI, lv_main, main_list_item_margin_px);
    lv_main.setAdapter(adapter_main);

//Goes in Adapter

public Adapter_Conversation(AppCompatActivity activity, @LayoutRes int resource,
                            ArrayList<Adapter_GetSet_Conversation> list_conversation, ListView lv_main,
                            int item_margin_px)
  • nowadays: 'notifyDataSetChanged()' cannot override 'notifyDataSetChanged()' in 'androidx.recyclerview.widget.RecyclerView.Adapter'; overridden method is final – danyapd Aug 03 '23 at 13:02
1

See the following (more modern) implementation with a kotlin extension function:

fun <VH : ViewHolder> RecyclerView.Adapter<VH>.notifyDataSetChanged(
    completion: () -> Unit
) {
    val adapter = this
    val observer = object : RecyclerView.AdapterDataObserver() {
        override fun onChanged() {
            super.onChanged()
            adapter.unregisterAdapterDataObserver(this)
            completion.invoke()
        }
    }
    registerAdapterDataObserver(observer)
    notifyDataSetChanged()
}
Werner Altewischer
  • 10,080
  • 4
  • 53
  • 60
-1

I was having the same problem and I end up doing this-

 BaseAdapter.notifyDataSetChanged();  
 new Handler().postDelayed(new Runnable() {
      @Override
              public void run() {
                 mScroller.startScroll(oldX, 0, dx, 0);
                //write scroll code here
               }
       },50);  //scroll after 50ms
karanatwal.github.io
  • 3,613
  • 3
  • 25
  • 57
  • 2
    I used to do that too, problem I had was that it's timing driven and not event driven so you have to guess the right timing and then it could easily change depending on which device is running it since newer devices can be much faster than some of the older devices. – JamisonMan111 May 12 '18 at 07:50
  • yes there can be alot of hacks tricks to do , also below 1 second is not much noticeable time for animating. – karanatwal.github.io May 12 '18 at 14:13