Pan*_*nos 32 android scroll scrollview webview
在我的应用程序中,我有一个ScrollView,其中包含一些线性视图,一些textview和一个Webview,然后是其他线性布局等.问题是WebView不会滚动.Scroll仅侦听ScrollView.有什么建议??
<ScrollView >
<TextView />
<WebView /> <-- this does not scroll
<TextView />
</ScrollView >
Run Code Online (Sandbox Code Playgroud)
Pan*_*nos 82
这是解决方案.在网上找到.我已经将WebView子类化,我正在使用该requestDisallowInterceptTouchEvent(true);方法来允许我的webview处理滚动事件.
TouchyWebView.java
package com.mypackage.common.custom.android.widgets
public class TouchyWebView extends WebView {
public TouchyWebView(Context context) {
super(context);
}
public TouchyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchyWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onTouchEvent(MotionEvent event){
requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(event);
}
}
Run Code Online (Sandbox Code Playgroud)
并在layout.xml中
<com.mypackage.common.custom.android.widgets.TouchyWebView
android:id="@+id/description_web"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)
Dip*_*dra 11
@panos提供的解决方案有效但与ScrollView一起使用时仍然存在问题.以下增强版本克服了该问题.
public class TouchyWebView extends WebView {
public TouchyWebView(Context context) {
super(context);
}
public TouchyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchyWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//Check is required to prevent crash
if (MotionEventCompat.findPointerIndex(event, 0) == -1) {
return super.onTouchEvent(event);
}
if (event.getPointerCount() >= 2) {
requestDisallowInterceptTouchEvent(true);
} else {
requestDisallowInterceptTouchEvent(false);
}
return super.onTouchEvent(event);
}
@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
requestDisallowInterceptTouchEvent(true);
}
}
Run Code Online (Sandbox Code Playgroud)
此外,您可能希望为TouchyWebView进行以下设置.
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
Run Code Online (Sandbox Code Playgroud)
除了一个例外,Panos 解决方案对我来说就足够了......我的固定高度(200dp)WebView可能是空的,或者可能已经加载了很多内容。所以它可能是也可能不是“本身”可滚动的。Panos 解决方案MotionEvent总是消耗s,因此当WebView为空并且用户触摸WebView并尝试滚动时,WebView将不会滚动(因为没有内容)并且可滚动的父级也会导致WebView“吞咽” MotionEvent- 所以什么也没有发生。我if为预期行为添加了小声明:
@Override
public boolean onTouchEvent(MotionEvent event) {
if(computeVerticalScrollRange() > getMeasuredHeight())
requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(event);
}
Run Code Online (Sandbox Code Playgroud)
WebView为空且不可垂直滚动时computeVerticalScrollRange() == getMeasuredHeight()WebView内容长于其高度(可滚动)时computeVerticalScrollRange() > getMeasuredHeight()| 归档时间: |
|
| 查看次数: |
36130 次 |
| 最近记录: |