我有什么:
现在我有一个 ScrollView 作为 parent 。在这个 ScrollView 中,我使用了一个加载 URL 然后在其中显示文本的 WebView。
这是我的 xml:
<ScrollView
android:id="@+id/parentScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Heading" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/webview" />
</RelativeLayout>
</ScrollView>
我想要的:
我想在其中滚动 webView。当我触摸 webView 时,不幸的是调用了父 ScrollView 。
我还必须保留父 ScrollView ,但除此之外,当我触摸 webView 时,我想在其中滚动 WebView 内容。
我该怎么做?
最佳答案
创建自定义触摸拦截 Webview
CustomWebview.java
package com.mypackage.common.custom.android.widgets
public class CustomWebview extends WebView {
public CustomWebview(Context context) {
super(context);
}
public CustomWebview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomWebview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onTouchEvent(MotionEvent event){
requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(event);
}
}
在layout.xml
<com.package.custom.widgets.CustomWebview
android:id="@+id/view_extra"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
关于android - 在 ScrollView 中滚动 Webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12567472/