在FragmentPagerAdapter prevent的WebView重新载入?FragmentPagerAdapter、prevent、WebView

2023-09-04 11:45:41 作者:含着眼泪笑、最痛

我有用于显示约6片,所有这一切都从Web服务器加载自己的数据FragmentPagerAdapter。其中一个选项卡包含的WebView加载图像从我的服务器。生成图像的服务器端的成本很高,因此我想,以减少调用的次数来重新加载的WebView。对于非web视图选项卡,我已经能够拯救我的状态(这些,只是一个简单的数组),并还原它们的标签打通刷卡。

I have a FragmentPagerAdapter used to show about 6 tabs, all of which load their data from a web server. One of the tabs contains a WebView that loads an image from my server. The server side costs of generating the image are high, and thus I want to reduce the number of calls to reload the WebView. For the non-WebView tabs, I have been able to save my state (for those, just a simple array) and restore them as tabs get swiped through.

问题:

的WebView重新载入每次我刷回它使用FragmentPagerAdapter,导致高重载时间和高负荷我的Web服务器上。

解决方案考虑:

使用ViewPager.setOffscreenPageLimit() 这是问题的,因为这将迫使要加载更多的标签,即使它们永远不会被观看。这是不必要的昂贵我的服务器上。 使用WebView.saveState()和WebView.restoreState() 该文档已被更新,以清楚显示状态在这里不再保留,因此这不再是这种情况下非常有用。 设置我的活动有:机器人:configChanges =keyboardHidden |方向|屏幕尺寸 这适用于旋转的情况下,但不影响ViewPager / FragmentPagerAdapter刷卡通过标签的情况下。 Use ViewPager.setOffscreenPageLimit() This is problematic because it will force more tabs to be loaded, even if they are never going to be viewed. This is needlessly expensive on my server. Use WebView.saveState() and WebView.restoreState() The documentation has been updated to make it clear display state is no longer maintained here, so this is no longer useful for this scenario. Set my activity to have: android:configChanges="keyboardHidden|orientation|screenSize" This works for the rotation case, but doesn't affect the ViewPager/FragmentPagerAdapter swiping through tabs case.

这听起来像旧的行为WebView.saveState()本来是完美的......

It sounds like the old behavior of WebView.saveState() would have been perfect...

推荐答案

这个问题似乎是,你是消灭自己的结果。

The problem appears to be that you are wiping out your own results.

当你需要一个新的的WebView 上的配置改变,你并不需要一个新的的WebView 否则。而且,如果你已经拥有了的WebView ,你不必告诉它做任何事情。

While you need a new WebView on a configuration change, you do not need a new WebView otherwise. And, if you already have the WebView, you do not have to tell it to do anything.

所以,我想尝试这样的:

So, I'd try this:

稳守的WebView 您在 onCreateView()在片段的数据成员 仅在膨胀 onCreateView布局(),如果该数据成员是 只有叫使用loadURL()如果你充气的布局 Hold onto the WebView that you create in onCreateView() in a data member of the fragment Only inflate the layout in onCreateView() if that data member is null Only call loadUrl() if you inflated the layout

(如果你正在创建的的WebView Java中的code,将吹大布局和创建的WebView 通过它的构造函数)

(if you are creating the WebView in Java code, replace "inflate the layout" with "create the WebView via its constructor")

如果该片段的内容大于的WebView ,你还需要守住根认为,你在数据成员膨胀,这样你就可以返回从 onCreateView()

If the contents of the fragment is more than the WebView, you will also need to hold onto the root view that you inflated in a data member, so you can return that from onCreateView().