我有4個片段,使用view-pager和tabs在主要活動中顯示新聞。但是我的代碼僅適用於第一個片段,即標籤1Back按鈕在WebView中無法按預期工作 - Android
在主要活動中,我有
@Override
public void onBackPressed() {
WebView mWebView = (WebView) findViewById(R.id.webview);
if (mWebView.getVisibility() == View.VISIBLE) {
mWebView.setVisibility(View.GONE);
mWebView.loadUrl("about:blank");
}
else {
super.onBackPressed();
}
return;
}
當按下後退按鈕,它通過採取用戶返回到主佈局隱藏web視圖那裏。它適用於選項卡1
但在片段2,3,4 - 當我按回按鈕退出應用程序。我使用相同的代碼作爲片段1
我發現 - mWebView.getVisibility()== View.VISIBLE沒有得到真正的 - 即使在網絡視圖是碎片2,3,4
可見片段1個代碼:
public class WSJFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<News>> {
.........
public WSJFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.newsview, container, false);
............
NewsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Find the current earthquake that was clicked on
News currentNews = mAdapter.getItem(position);
String newsUrl = currentNews.getUrl();
progress.setVisibility(View.VISIBLE);
mWebView.setVisibility(View.VISIBLE);
mWebView.loadUrl(newsUrl);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(mWebView, url);
progress.setVisibility(View.GONE);
}
});
}
});
return rootView;
}
........
}
https://github.com/mrafz/W3News
WebView mWebView =(WebView)findViewById(R.id.webview);應該像這樣WebView mWebView =(WebView)inflaterView.findViewById(R.id.webview); –
rootView = inflater.inflate(R.layout.newsview,container,false); mWebView =(WebView)rootView.findViewById(R.id.webview);我喜歡這個。這不是問題 – ams92
好吧,但在OnBackPressed()您綁定Webview再次,而不是使用全球變量 –