Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check when my ListView has finished redrawing?

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

Calling getLastVisiblePosition() right after notifydatasetchanged() doesn't work because the list hasnt finished drawing yet.

like image 329
ha1ogen Avatar asked Mar 20 '15 18:03

ha1ogen


People also ask

How do you know when the recyclerView has finished laying down the items?

The best way that I found to know when has finished laying down the items was using the LinearLayoutManager. For example: private RecyclerView recyclerView; ... recyclerView = findViewById(R.

Which of the following is used to notify the ListView to update its display because the underlying data was changed?

For ArrayAdapter for instance, there is the notifyDataSetChanged() method which should be called after you've updated the array list which holds all your data, in order to refresh the ListView .

How do I refresh my kotlin adapter?

Pull to Refresh is used to update the data within the list in our android application. For implementing this we have to use Swipe to Refresh Layout. Using this widget when the user swipes down the list which is being displayed on the screen is updated.


1 Answers

Hopefully this can help:

  • Setup an addOnLayoutChangeListener on the listview
  • Call .notifyDataSetChanged();
  • This will fire off the OnLayoutChangeListener when completed
  • Remove the listener
  • Perform code on update (getLastVisiblePosition() in your case)

    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();
    
like image 71
Petro Avatar answered Oct 07 '22 00:10

Petro