5

我被告知使用 FragmentStateAdapter 的 createFragment 覆盖来按索引获取当前片段。

这就是我的适配器的样子。

class AddRestaurantPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
    FragmentStateAdapter(fragmentManager, lifecycle) {
    private var fragments = listOf<Fragment>(
        FragmentA.newInstance(),
        FragmentB.newInstance(),
        FragmentC.newInstance(),
        FragmentD.newInstance()
    )

    override fun getItemCount() = 4
    override fun createFragment(position: Int) =
        fragments[position]

}

这工作正常,直到我们在这个活动/片段上强制一个 onSaveInstance/OnRestoreInstance。

ViewPager2 重新加载它上面已经存在的片段,我们需要一种访问它的方法。FragmentStateAdapter 类中的以下 LOC 证明了这一点

private void ensureFragment(int position) {
        long itemId = getItemId(position);
        if (!mFragments.containsKey(itemId)) {
            // TODO(133419201): check if a Fragment provided here is a new Fragment
            Fragment newFragment = createFragment(position);
            newFragment.setInitialSavedState(mSavedStates.get(itemId));
            mFragments.put(itemId, newFragment);
        }
    }

在活动/片段重新创建之后,您会发现 this If 块永远不会执行,因为 mFragments 已经包含在收集活动之前添加的片段。

所以我想知道应该如何提取viewpager2中已经存在的片段

4

1 回答 1

0

如果你看看这个方法,

/**
 * @param holder that has been bound to a Fragment in the {@link #onBindViewHolder} stage.
 */
@SuppressWarnings("WeakerAccess") // to avoid creation of a synthetic accessor
void placeFragmentInViewHolder(@NonNull final FragmentViewHolder holder) {
    Fragment fragment = mFragments.get(holder.getItemId());
    if (fragment == null) {
        throw new IllegalStateException("Design assumption violated.");
    }
    FrameLayout container = holder.getContainer();
    View view = fragment.getView();

    /*
    possible states:
    - fragment: { added, notAdded }
    - view: { created, notCreated }
    - view: { attached, notAttached }

    combinations:
    - { f:added, v:created, v:attached } -> check if attached to the right container
    - { f:added, v:created, v:notAttached} -> attach view to container
    - { f:added, v:notCreated, v:attached } -> impossible
    - { f:added, v:notCreated, v:notAttached} -> schedule callback for when created
    - { f:notAdded, v:created, v:attached } -> illegal state
    - { f:notAdded, v:created, v:notAttached } -> illegal state
    - { f:notAdded, v:notCreated, v:attached } -> impossible
    - { f:notAdded, v:notCreated, v:notAttached } -> add, create, attach
     */

    // { f:notAdded, v:created, v:attached } -> illegal state
    // { f:notAdded, v:created, v:notAttached } -> illegal state
    if (!fragment.isAdded() && view != null) {
        throw new IllegalStateException("Design assumption violated.");
    }

    // { f:added, v:notCreated, v:notAttached} -> schedule callback for when created
    if (fragment.isAdded() && view == null) {
        scheduleViewAttach(fragment, container);
        return;
    }

    // { f:added, v:created, v:attached } -> check if attached to the right container
    if (fragment.isAdded() && view.getParent() != null) {
        if (view.getParent() != container) {
            addViewToContainer(view, container);
        }
        return;
    }

    // { f:added, v:created, v:notAttached} -> attach view to container
    if (fragment.isAdded()) {
        addViewToContainer(view, container);
        return;
    }

    // { f:notAdded, v:notCreated, v:notAttached } -> add, create, attach
    if (!shouldDelayFragmentTransactions()) {
        scheduleViewAttach(fragment, container);
        mFragmentManager.beginTransaction()
                .add(fragment, "f" + holder.getItemId())
                .setMaxLifecycle(fragment, STARTED)
                .commitNow();
        mFragmentMaxLifecycleEnforcer.updateFragmentMaxLifecycle(false);
    } else {
        if (mFragmentManager.isDestroyed()) {
            return; // nothing we can do
        }
        mLifecycle.addObserver(new LifecycleEventObserver() {
            @Override
            public void onStateChanged(@NonNull LifecycleOwner source,
                    @NonNull Lifecycle.Event event) {
                if (shouldDelayFragmentTransactions()) {
                    return;
                }
                source.getLifecycle().removeObserver(this);
                if (ViewCompat.isAttachedToWindow(holder.getContainer())) {
                    placeFragmentInViewHolder(holder);
                }
            }
        });
    }
}

tag就是"f" + holder.getItemId()

@Override
    public long getItemId(int position) {
        return position;
    }

默认情况下getItemId(int postion)返回位置。但是如果你重写这个方法。

int postionOfTheFragmentYouWantToGet=0;
Fragment page = getSupportFragmentManager().findFragmentByTag("f" + postionOfTheFragmentYouWantToGet);
于 2021-01-24T14:21:13.143 回答