我创建了一个应用程序,其中我有3个选项卡,可以使用片段显示网页。每次我从选项卡1切换到选项卡2然后再返回选项卡1,选项卡1将重新加载。我搜索了谷歌和解决方案,我发现它没有工作。到目前为止,我已经尝试过这个:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mWebView.restoreState(savedInstanceState);
}
而且:
<activity ...
android:launchMode="singleInstance"
android:alwaysRetainTaskState="true">
这是我的主要活动
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.tech.slidechat.adaptor.SlidingTabLayout;
import com.tech.slidechat.adaptor.TabsPagerAdapter;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends ActionBarActivity {
SlidingTabLayout tabs;
ViewPager viewPager;
TabsPagerAdapter adapter;
Toolbar toolbar;
CharSequence Titles[]={ "tab1", "tab2", "tab3" };
int Numboftabs =3;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
adapter = new TabsPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
// Assigning ViewPager View and setting the adapter
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
@Override
public int getDividerColor(int i) {
return 0;
}
});
tabs.setViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的Webview片段之一:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class GamesFragment extends Fragment {
private ProgressBar progress;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, container, false);
String url = "http://myurl.com";
WebView myWebView = (WebView) rootView.findViewById(R.id.webViewGames);
myWebView.setWebChromeClient(new myWebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
progress = (ProgressBar) rootView.findViewById(R.id.progressBar);
progress.setMax(100);
myWebView.loadUrl(url);
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView View, String url) {
View.loadUrl(url);
GamesFragment.this.progress.setProgress(0);
return true;
}
});
myWebView.setOnKeyListener(new android.view.View.OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
return rootView;
}
private class myWebViewClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
GamesFragment.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
}
一个有趣的事情是,无论何时我使用Tab1和Tab2,它们都没有重新加载但是当我切换到Tab3然后返回到Tab1然后只重新加载。
任何帮助都会深深感激。
答案 0 :(得分:3)
你能尝试添加:
viewPager.setOffscreenPageLimit(2);
根据文档,setOffscreenPageLimit是:
设置应保留在页面两侧的页数 处于空闲状态的视图层次结构中的当前页面。页面超出此范围 在需要时,将从适配器重新创建限制。
...
您应该保持此限制,特别是如果您的网页具有复杂的&gt;布局。此设置默认为1.
默认值为1,因此当您转到选项卡3时,当前选项卡左侧有两个选项卡,因此将重新创建选项卡1。