How to check if my ListView has ended?

I have a ListView . I updated its adapter and called notifydatasetchanged() . I want to wait for the list to finish drawing, and then call getLastVisiblePosition () on the list to check the last item.

Calling getLastVisiblePosition() immediately after notifydatasetchanged() does not work, because the list has not yet finished drawing.

+8
android listview
source share
1 answer

Hope this can help:

  • Setting addOnLayoutChangeListener in the list
  • Call .notifyDataSetChanged ();
  • This will disable OnLayoutChangeListener after completion
  • Remove Listener
  • Execute the code when updating ( getLastVisiblePosition() )

     mListView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { mListView.removeOnLayoutChangeListener(this); Log.e(TAG, "updated"); } }); mAdapter.notifyDataSetChanged(); 
+26
source share

All Articles