我试图隐藏我的片段,当它不在屏幕上时所以我使用setUserVisibleHint
来隐藏我的片段但是我收到了错误
GamesFragment中的" myUIUpdate(View)无法应用于()"
我该如何解决?
这是我的活动:
import android.os.Bundle;
import android.support.annotation.Nullable;
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;
private WebView myWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, null);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
myUIUpdate(view);
}
void myUIUpdate(View view) {
String url = "http://gsmarena.com";
myWebView = (WebView)this.getView().findViewById(R.id.webViewGames);
myWebView.setWebChromeClient(new myWebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
progress = (ProgressBar) this.getView().findViewById(R.id.progressBar);
myWebView.loadUrl(url);
progress.setMax(100);
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;
}
});
}
private class myWebViewClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
GamesFragment.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
boolean isResumed = false;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser); {
if (isVisibleToUser && isResumed()) {
myUIUpdate();
isResumed = true;
}
}}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
}
准确地说,错误出现在这一行:
boolean isResumed = false;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser); {
if (isVisibleToUser && isResumed()) {
// here is the error.
myUIUpdate();
isResumed = true;
}
}}
我尝试过更改
myUIUpdate();
到
myUIUpdate(View);
当我尝试这样做时,它会说"表达预期"。
答案 0 :(得分:0)
如果您想访问片段视图,可以使用Fragment.getView(
)并将其传递给myUiUpdate