Is there a callback for when RecyclerView has finished showing its items after I've set it with an adapter?

前端 未结 3 1348
心在旅途
心在旅途 2020-12-02 12:56

Background

I\'ve made a library that shows a fast-scroller for RecyclerView (here, in case anyone wants), and I want to decide when to show and when to hide the fa

3条回答
  •  自闭症患者
    2020-12-02 13:46

    I'm using the 'addOnGlobalLayoutListener' for this. Here is my example:

    Definition of an interface to perform the action required after the load:

    public interface RecyclerViewReadyCallback {
      void onLayoutReady();
    }
    

    on the RecyclerView, I trigger the onLayoutReady method when the load is ready:

    mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
      if (recyclerViewReadyCallback != null) {
        recyclerViewReadyCallback.onLayoutReady();
      }
      recyclerViewReadyCallback = null;
    });
    

    Note: The set to null is necessary to prevent the method from being called multiple times.

提交回复
热议问题