Android Viewpager saving data and views(Android Viewpager 保存数据和视图)
问题描述
希望有人可以帮助我解决我在 Viewpagers 和保存数据方面遇到的小问题/困惑.
Hopefully someone can help me with a slight problem/confusion I have with Viewpagers and saving data.
问题:
在我拥有的四个视图中滚动时,第一个视图有两个微调器,两个显示字符串或选定项目的文本视图.如果我滚动到第三页并返回第二页,则第一个视图中的数据将丢失.因此需要保存数据.
When scrolling across the four views I have, the first view has two spinners, two textviews that display a string or a selected item. if I scroll on to the third page and back to the second page, the data in the first view is lost. hense needing to save the data.
这是否可以在以下两个例程中完成?(最好的猜测是)如果是这样,需要说明什么样的命令?
Would this be done in the two routines stated below? (best guess is it would be) if so, what sort of commands need to be stated?
代码:
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
额外信息:viewpager 正在使用
EXTRA INFO: the viewpager is being used in
public Object instantiateItem(View collection, int position) {
}
完整的方法列表如下:
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public void startUpdate(View arg0) {
}
推荐答案
您可以修改 ViewPager 而不是保存这些页面的状态,以便它缓冲所有页面,而不是在滚动时销毁它们并重新创建它们.
Instead of saving the state of those pages you could modify your ViewPager so it buffers all of your pages instead of destroying them and recreating them when you scroll.
例如,如果您有 4 页:
So for example if you have 4 pages:
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setOffscreenPageLimit(3); // the number of "off screen" pages to keep loaded each side of the current page
pager.setAdapter(new PageAdapter(context));
现在您的页面将保持活跃"
Now your pages will be kept 'alive'
但请小心使用此技术,因为它会增加内存消耗.传递给 setOffscreenPageLimit() 的数字是当前页面每一侧要保留的页面数.在这种情况下,我们总共有 4 个页面,所以最坏的情况是当我们位于页面集的最边缘时 - 我们有当前可见的页面,然后剩余的 3 个页面在屏幕之外的一侧因此被保留在记忆中.但是,对于更大的数据集,这可能意味着内存中保留了 7 个页面(当前页面加上任一侧的 3 个页面).
Be careful with this technique however because it will increase memory consumption. The number passed to setOffscreenPageLimit() is the number of pages each side of the current page to retain. In this case we have 4 pages in total so the worst case scenario is when we are at the far edge of of the page set - we have the currently visible page and then the remaining 3 pages are off screen to one side and are thus kept in memory. However, with a larger data set it could potentially mean 7 pages are kept in memory (current page plus 3 either side).
通常更优化的解决方案是正确处理卸载和重新加载页面状态并重新渲染它们.
Generally the more optimal solution would be to properly deal with unloading and reloading the page state and re-render them.
希望对你有帮助
这篇关于Android Viewpager 保存数据和视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android Viewpager 保存数据和视图
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01