“This is the 15th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

The difference between FragmentPagerAdapter and FragmentStateAdapter

With FragmentPagerAdapter, the Fragment object stays in memory all the time, so you can’t have too much data. FragmentPagerAdapter works with pages that have only a small number of pages, such as tabs.

FragmentStatePagerAdapter

Consider using FragmentStatePagerAdapte when the page has a lot of data

Simple interpretation of source code

OnCreateViewHolder () method

public final FragmentViewHolder onCreateViewHolder (@NonNull ViewGroup parent,int viewType){
    return FragmentViewHolder.create(parent);
}
static FragmentViewHolder create (ViewGroup parent){
    FrameLayout container = new FrameLayout(parent.getContext());
    container.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    container.setId(ViewCompat.generateViewId());
    container.setSaveEnabled(false);
    return new FragmentViewHolder(container);
}
Copy the code

OnCreateViewHolder () creates a FrameLayout with the width and height of MATCH_PARENT. Note that the PagerAdapter is not the Fragment rootView;

onBindViewHolder()

public final void onBindViewHolder(final @NonNull FragmentViewHolder holder, int position) {
    final long itemId = holder.getItemId();
    final int viewHolderId = holder.getContainer().getId();
    final Long boundItemId = itemForViewHolder(viewHolderId); // item currently bound to the VH
    if(boundItemId ! =null&& boundItemId ! = itemId) { removeFragment(boundItemId);525 mItemIdToViewHolder.remove(boundItemId);
    } mItemIdToViewHolder.put(itemId, viewHolderId);
    // This may overwrite existing entries
    // Ensure that the target Fragment is not empty, meaning that ensureFragment(position) can be created in advance;
    /** Special case when {@link RecyclerView} decides to keep the {@link container} *
     attached to the window, but not to the view hierarchy (i.e. parent is null) */
    final FrameLayout container = holder.getContainer();
    // If ItemView is already added to Window and parent is not null, viewHoder binding is triggered;
    if (ViewCompat.isAttachedToWindow(container)) {
        if(container.getParent() ! =null) {
            throw new IllegalStateException("Design assumption violated.");
        }
        container.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) {
                if(container.getParent() ! =null) {
                    container.removeOnLayoutChangeListener(this); // Bind Fragment to ViewHolderplaceFragmentInViewHolder(holder); }}}); }// Recycle garbage
     Fragments gcFragments(a); 
}
Copy the code

OnBindViewHolder () first retrieves the Fragment corresponding to the current position, which means that the preloaded Fragment object is created ahead of time. If the current hold. itemView has been added to the screen and is laid out and parent is not empty, the Fragment is bound to a ViewHodler; Each call will be gc, mainly to avoid users modify the data source resulting in garbage objects;

onViewAttachedToWindow()

public final void onViewAttachedToWindow ( 
@NonNull final FragmentViewHolder holder){
    placeFragmentInViewHolder(holder);
    gcFragments();
}
Copy the code

The onViewAttachedToWindow() method calls onViewAttachedToWindow to bind the Fragment to hodler

onViewRecycled()

public final void onViewRecycled (@NonNull FragmentViewHolder holder){
    final int viewHolderId = holder.getContainer().getId();
    final Long boundItemId = itemForViewHolder(viewHolderId);
    // The project currently bound to VH
    if(boundItemId ! =null) { removeFragment(boundItemId); mItemIdToViewHolder.remove(boundItemId); }}Copy the code

Fragment removal is only triggered when onViewRecycled()

Fragments lazy loading

Lazy loading effect

setoffscreenPageLimit(0); 
Copy the code

SetUserVisibleHint is not available in your Fragment when offscreenPageLimit>0. The setUserVisibleHint is not available in your Fragment when offscreenPageLimit>0. So, lazy fragments that work for ViewPager won’t work here;

When offscreenPageLimit>0, there is no difference between handling the off-screen Fragment and the visible Fragment, so you can’t use setUserVisibleHint to determine whether to display it or not.