滚动在 WebView 上反向工作
the scrolling is working by reverse on WebView
我在我的应用程序中使用了 WebView,它发现滚动无法正常工作,当我尝试向下滚动页面时,页面没有向下滚动,反之亦然,但是当我尝试触摸屏幕并下车时反过来 "touch with scroll up" 它正在工作!!,更多说明在此图像中 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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VisitSiteFragment"
android:orientation="vertical"
android:background="@color/white"
>
<ProgressBar
android:id="@+id/webViewProgressBar"
android:layout_width="match_parent"
android:layout_height="8dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:theme="@style/progressBarBlue"
android:layout_gravity="top"
android:layout_marginTop="-3dp"
android:progress="20"
/>
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</WebView>
</LinearLayout>
我看到有人问这个问题,但是那个问题的答案没有帮助
经过多次 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, 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,它发现滚动无法正常工作,当我尝试向下滚动页面时,页面没有向下滚动,反之亦然,但是当我尝试触摸屏幕并下车时反过来 "touch with scroll up" 它正在工作!!,更多说明在此图像中 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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VisitSiteFragment"
android:orientation="vertical"
android:background="@color/white"
>
<ProgressBar
android:id="@+id/webViewProgressBar"
android:layout_width="match_parent"
android:layout_height="8dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:theme="@style/progressBarBlue"
android:layout_gravity="top"
android:layout_marginTop="-3dp"
android:progress="20"
/>
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</WebView>
</LinearLayout>
我看到有人问这个问题
经过多次 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, 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);
}
});