Android NestedScrollView中的WebView无法滚动
我有一个使用Android NestedScrollView中的WebView无法滚动,android,webview,material-design,nestedscrollview,Android,Webview,Material Design,Nestedscrollview,我有一个使用WebView加载web内容的活动。当我想用图像实现灵活的空间时,问题就出现了。我可以展开和折叠工具栏,但是当工具栏已经折叠时,滚动条卡在那里。我无法在WebView中滚动内容 这是XML: <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPa
WebView
加载web内容的活动。当我想用图像实现灵活的空间时,问题就出现了。我可以展开和折叠工具栏
,但是当工具栏
已经折叠时,滚动条卡在那里。我无法在WebView
中滚动内容
这是XML:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<WebView
android:id="@+id/read_full_content_wv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.NestedScrollView>
还是被绊倒了
编辑2 仅供参考:我试过华硕Zenfone 6-KitKat 4.4.2 KitKat是否可能无法正确加载
编辑3:我对这个问题的最佳解决方案
工作之后,我认为我无法获得使用CollasingToolbarLayout和WebView组合使用灵活空间的经验,因此我将WebView更改为TextView,并使用
Html.fromHtml()
阅读内容。除非谷歌要更新或修复一些功能,以便我们可以将折叠工具栏布局和网络视图结合在一起。我知道已经有一段时间了,但我还是会试试
我也有类似的问题
我通过替换:android.support.v4.widget.NestedScrollView解决了这个问题
使用:android.widget.ScrollView
我不知道它将如何影响正在折叠的工具栏 我遇到了一个类似的问题,但我在多个Android版本上通过使用以下管理触摸事件的自定义WebView类解决了这个问题:
import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;
/**
* Created by pk on 10/16/16.
* Creates a WebView that can be used inside a ScrollView
*/
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) {
//Check pointer index to avoid -1 (error)
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);
}
}
通过使用NestedScrollingChild接口,我们可以在webview中实现NestedScrollView属性。并自定义onTouchEvent()方法下的滚动功能 声明NestedWebView,而不是在NestedScrollView中声明webview。例如
<com.nestedscrollwebviewexample.NestedWebView
android:id="@+id/nested_webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:fillViewport="true"
android:focusable="true"
android:isScrollContainer="false"
android:visibility="visible"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_scrollFlags="scroll|exitUntilCollapsed" />
这是一个改变基础Webview高度属性的例子 对于NestedScrollView,请使用属性
android:fillViewport="true"
对于WebView,请使用android:layout\u height=“wrap\u content”
完整答案只需将NestedScrollView fillViewport=“false”和WebView布局\u height=“wrap\u content”
解决方案: 只需将webView的高度
match_parent
更改为wrap_content
如果执行height=“match\u parent”
操作,则webview不会在ScrollView中滚动
android:id="@+id/read_full_content_wv"
android:layout_width="match_parent"
android:layout_height="wrap_content" // just change height of webView
/> ```
检查这个:在你展示了那个链接之后,我试过了,但仍然结巴。我创建这样的类
私有类MyBrowser扩展WebViewClient{@Override public void onPageStarted(WebView视图,字符串url,位图favicon){NestedScrollView nsv=(NestedScrollView)findViewById(R.id.nsv_fc);nsv.scrollTo(0,0);}
并在创建活动时这样调用它WebView WebView=(WebView)findViewById(R.id.read\u full\u content\u wv);setWebViewClient(新的MyBrowser());loadData(extra.get(1.toString(),“text/html”,“utf-8”)
请在您的问题中添加这些代码,并明确您尝试了什么。很抱歉给您带来不便,我已经在我的问题@LinX64中添加了新信息。现在,它看起来更好了:)我必须将签入onTouchEvent
更改为:如果(event.getPointerCount()>=1)
,那么它对我有效。非常感谢。这应该是公认的答案。我收回它,这将取消任何折叠工具栏功能。。。让它成为一个毫无意义的子类…很好的答案,它确实帮助了我代码中有一个bug:mLastY=eventY-mScrollOffset[1]如果
,则代码>在中-不能保证mLastY
会用eventY
值更新。我对此有异议。我删除了这一行,然后在ACTION\u MOVE
的顶部创建了var totalScrollOffset=0
。然后累计totalScrollOffset+=mScrollOffset[1]
内部if(dispatchNestedPreScroll)和(dispatchNestedScroll)
。然后在ACTION\u MOVE
的底部,我写了mLastY=eventY-totalScrollOffset
为什么上面提到的是一个问题?因为如果DispatchedNestedPreScroll
未完成,则mLastY
值会增加到较高值,并且此较高值会通过滚动事件进行调度,这可能会产生意外结果(我对SwiperFreshLayout有问题)。您是对的@Przemo请更新答案以帮助其他人。我在缩放“mLastY”值自动递增的位置时遇到了相同的问题。我在这方面遇到了一个问题,当我第一次尝试缩放网页时,它开始放大太多。稍后,当我重新创建相同的片段时,它工作正常。有什么解决方案吗?@akshaybhange这让我在使用m xmls进行了长时间的故障排除后工作起来。请彻底验证您的xml布局,或者这或这也可能取决于布局的复杂性。谢谢@SUD007不适合我,tge webview和nestedscrollview冻结,我可以滚动。另一方面,当我将fillviewport设置为“false”时,我可以滚动,但嵌套的_sv中的webview显示静态内容和我网站的粘性按钮不会随着滚动而下降
private NestedWebView mShopWebView;
mShopWebView = (NestedWebView) findViewById(R.id.url_load_webview);
android:fillViewport="true"
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<WebView
android:id="@+id/read_full_content_wv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:id="@+id/read_full_content_wv"
android:layout_width="match_parent"
android:layout_height="wrap_content" // just change height of webView
/> ```