2

I have a RecycleView . I updated its adapter, and call notifydatasetchanged()

. I want to wait until the list finishes drawing

Peter
  • 1,069
  • 2
  • 13
  • 24
  • then call getLastVisiblePosition() in new Handler() – Divyesh Patel May 25 '17 at 09:51
  • 1
    please refer to [this solution](https://stackoverflow.com/questions/29173588/how-do-i-check-when-my-listview-has-finished-redrawing), it should resolve your problem – TaoBit May 25 '17 at 09:53
  • the issue is i want to insert items in between the recycleview items , but before i insert i do some computation with the adapter size and all, sometimes the adapter is not set yet completed before the sequence of insert is performed. so it just causes a deep lag in the code and all just turn to be a mess . – Peter May 25 '17 at 10:02
  • @DivyeshPatel i havnt used a handler anywer before with a list insert function , will it cause any harm to smoothness of the app? have you tries it any were?? u sure of this usage ? – Peter May 25 '17 at 10:04
  • Try doing your computations inside `recyclerView.post(runnable)` – natario May 25 '17 at 10:10
  • I think you might be trying to solve the wrong problem here. What is it that you are trying to achieve? You can call `getItemCount()` to get the adapter size. `getItemCount` is implemented by you. There is no need to wait for the list to finish drawing before you call `getItemCount` usually. – Weizhi May 25 '17 at 10:23

2 Answers2

6

just add this:

recycler.post(() -> {
    do something...
});

done.

wenmin92
  • 106
  • 1
  • 4
1

try this one.

 recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        //At this point the layout is complete and the 
                        //dimensions of recyclerView and any child views are known.
                    }
                });
Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29