我在我的应用程序中使用了WebView,它看到滚动不正常,当我尝试向下滚动页面时没有下降而反之亦然,但当我尝试触摸屏幕并反向下车时“触摸向上滚动“它正在工作!!,在此图像gif1,gif2中进一步澄清
码
public class VisitSiteFragment extends android.app.Fragment {
private View view;
private ProgressBar webViewProgressBar;
private WebView webView;
String url;
public VisitSiteFragment() {
// required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_visit_site,container,false);
if (getArguments() != null) {
url = this.getArguments().getString("url");
Log.e("URL",url);
}
webViewProgressBar = (ProgressBar) view.findViewById(R.id.webViewProgressBar);
webView = (WebView) view.findViewById(R.id.webView);
webView.setVisibility(View.VISIBLE);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setUseWideViewPort(true);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view,int newProgress) {
webViewProgressBar.setProgress(newProgress);
super.onProgressChanged(view,newProgress);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view,String url,Bitmap favicon) {
super.onPageStarted(view,url,favicon);
webViewProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view,String url) {
super.onPageFinished(view,url);
webViewProgressBar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
});
webView.loadUrl(url);
return view;
}
}
XML
我看到有人问这个问题here,但问题的答案没有帮助
最佳答案
经过WebView设置的多次尝试后,我通过禁用滚动嵌入WebView的问题解决了这个问题,如本answer所述,并使用ScrollView代替它
编辑后的代码
webView = (WebView) view.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setUseWideViewPort(true);
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v,MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
webView.setScrollContainer(false);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view,url);
webViewProgressBar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
});