Android的Viewpager保存数据和观点观点、数据、Android、Viewpager

2023-09-04 05:05:46 作者:稳坐江山

您好计算器的世界,

希望有人可以帮我一个小问题/混淆我有Viewpagers和保存数据。

Hopefully someone can help me with a slight problem/confusion I have with Viewpagers and saving data.

问题:

当在四个观点我有滚动,第一种观点有两个微调,二textviews即显示一个字符串或所选项目。如果我滚动到第三页并回到第二页,在第一视图中的数据丢失。 HENSE需要保存数据。

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?

code:

@Override   
public void restoreState(Parcelable arg0, ClassLoader arg1)    
{


}

@Override
public Parcelable saveState() {
    return null;
}

额外的信息: 该viewpager在使用

EXTRA INFO: the viewpager is being used in

公开对象instantiateItem(查看收集,INT位置){....等

public Object instantiateItem(View collection, int position) { ....etc

methords的完整列表如下

the complete list of methords are as follows

    @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) {
    // TODO Auto-generated method stub

}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
    // TODO Auto-generated method stub

}

@Override
public void startUpdate(View arg0) {
    // TODO Auto-generated method stub

}

请帮我关于这个。

推荐答案

不需要将这些页面的状态,你可以修改你的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.

希望这有助于

 
精彩推荐
图片推荐