gpt4 book ai didi

android - 查看页面 : recovering from configuration change

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:59 26 4
gpt4 key购买 nike

我很难从 ViewPager 的配置更改中恢复过来。

我想要的是保存当前寻呼机位置,以便在配置更改后可以看到相同的页面。为此,我保存了 pager.getCurrentItem() 并使用 pager.setCurrentItem 恢复它。只是这不起作用:setCurrentItem 实际上尝试重新创建该页面而不是选择它!即使页面已经创建,也是如此。

现在我不确定在实现支持它的适配器时是否遗漏了什么,但是 setCurrentItemViewPager 认为它已完全构建时是否存在缺陷?但是,查看该函数的代码对我来说毫无意义;它似乎总是尝试重新创建一个页面,除非根本没有任何项目:

void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) {
// these are the only checked that make this method return
if (mAdapter == null || mAdapter.getCount() <= 0) {
setScrollingCacheEnabled(false);
return;
}
if (!always && mCurItem == item && mItems.size() != 0) {
setScrollingCacheEnabled(false);
return;
}

// ... removed some init code

mCurItem = item;
populate();

// ...
}

如您所见,populate() 总是被调用,除非根本没有项目,或者您尝试选择的项目已被选中。是什么赋予了?这对我来说没有意义。

最佳答案

setCurrentItem actually attempts to recreate that page instead of selecting it! And that's even though the page has already been created.

不完全是,至少就我阅读代码的方式而言是这样。

As you can see, populate() is always called, except when there are either no items at all, or the item you're trying to select was already selected.

populate() 将调用 addNewItem(),它调用 PagerAdapter 上的 instantiateItem()PagerAdapterFragmentPagerAdapter 实现将使用现有 fragment (如果可用),否则它会调用 getItem() 以便您自己创建页面。因此,如果您对页面 fragment 使用 setRetainInstance(true),并且您正在使用 FragmentPagerAdapter,则它们应该被重用。

FragmentStatePagerAdapter 看起来更复杂,如果您滚动自己的 PagerAdapter 无 fragment ,则由您来实现缓存机制。

关于android - 查看页面 : recovering from configuration change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11122379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com