我有一个使用WebView加载Web内容的活动.当我想用图像实现Flexible Space时会出现问题.我可以展开和折叠工具栏,但是当工具栏已经折叠时,滚动条就会卡在那里.我无法滚动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>
你对此有什么解决方案吗?
问候,
埃尔默
public class FullContentActivity extends AppCompatActivity { ... @Override protected void onCreate(Bundle savedInstance){ ... WebView webView = (WebView) findViewById(R.id.read_full_content_wv); webView.setWebViewClient(new MyBrowser()); webView.loadData(extra.get(1).toString(),"text/html","utf-8"); ... } ... private class MyBrowser extends WebViewClient { @Override public void onPageStarted(WebView view,String url,Bitmap favicon){ NestedScrollView nsv = (NestedScrollView) findViewById(R.id.nsv_fc); nsv.scrollTo(0,0); } } }
并且仍然被困住了.
编辑2
仅供参考:我试过华硕Zenfone 6 – KitKat 4.4.2
KitKat是否有可能无法正确加载?
编辑3:我对这个问题的最佳解决方案
在解决之后,我想我无法获得使用CollapsingToolbarLayout结合WebView的灵活空间体验,因此我将WebView更改为TextView并使用Html.fromHtml()来读取内容.除非Google会更新或修复某些功能,否则我们可以将CollapsingToolbarLayout和WebView结合在一起.
解决方法
通过使用NestedScrollingChild接口,我们可以在webview中实现NestedScrollView属性.并在onTouchEvent()方法下自定义滚动功能.
public class NestedWebView extends WebView implements NestedScrollingChild { private int mLastY; private final int[] mScrollOffset = new int[2]; private final int[] mScrollConsumed = new int[2]; private int mNestedOffsetY; private NestedScrollingChildHelper mChildHelper; public NestedWebView(Context context) { this(context,null); } public NestedWebView(Context context,AttributeSet attrs) { this(context,attrs,android.R.attr.webViewStyle); } public NestedWebView(Context context,AttributeSet attrs,int defStyleAttr) { super(context,defStyleAttr); mChildHelper = new NestedScrollingChildHelper(this); setNestedScrollingEnabled(true); } @Override public boolean onTouchEvent(MotionEvent ev) { boolean returnValue = false; MotionEvent event = MotionEvent.obtain(ev); final int action = MotionEventCompat.getActionMasked(event); if (action == MotionEvent.ACTION_DOWN) { mNestedOffsetY = 0; } int eventY = (int) event.getY(); event.offsetLocation(0,mNestedOffsetY); switch (action) { case MotionEvent.ACTION_MOVE: int deltaY = mLastY - eventY; // NestedPreScroll if (dispatchNestedPreScroll(0,deltaY,mScrollConsumed,mScrollOffset)) { deltaY -= mScrollConsumed[1]; mLastY = eventY - mScrollOffset[1]; event.offsetLocation(0,-mScrollOffset[1]); mNestedOffsetY += mScrollOffset[1]; } returnValue = super.onTouchEvent(event); // NestedScroll if (dispatchNestedScroll(0,mScrollOffset[1],mScrollOffset)) { event.offsetLocation(0,mScrollOffset[1]); mNestedOffsetY += mScrollOffset[1]; mLastY -= mScrollOffset[1]; } break; case MotionEvent.ACTION_DOWN: returnValue = super.onTouchEvent(event); mLastY = eventY; // start NestedScroll startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: returnValue = super.onTouchEvent(event); // end NestedScroll stopNestedScroll(); break; } return returnValue; } // Nested Scroll implements @Override public void setNestedScrollingEnabled(boolean enabled) { mChildHelper.setNestedScrollingEnabled(enabled); } @Override public boolean isNestedScrollingEnabled() { return mChildHelper.isNestedScrollingEnabled(); } @Override public boolean startNestedScroll(int axes) { return mChildHelper.startNestedScroll(axes); } @Override public void stopNestedScroll() { mChildHelper.stopNestedScroll(); } @Override public boolean hasNestedScrollingParent() { return mChildHelper.hasNestedScrollingParent(); } @Override public boolean dispatchNestedScroll(int dxConsumed,int dyConsumed,int dxUnconsumed,int dyUnconsumed,int[] offsetInWindow) { return mChildHelper.dispatchNestedScroll(dxConsumed,dyConsumed,dxUnconsumed,dyUnconsumed,offsetInWindow); } @Override public boolean dispatchNestedPreScroll(int dx,int dy,int[] consumed,int[] offsetInWindow) { return mChildHelper.dispatchNestedPreScroll(dx,dy,consumed,offsetInWindow); } @Override public boolean dispatchNestedFling(float velocityX,float velocityY,boolean consumed) { return mChildHelper.dispatchNestedFling(velocityX,velocityY,consumed); } @Override public boolean dispatchNestedPreFling(float velocityX,float velocityY) { return mChildHelper.dispatchNestedPreFling(velocityX,velocityY); } }
声明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" />
您可以在Activity中初始化为NestedWebView,而不是声明Webview
private NestedWebView mShopWebView; mShopWebView = (NestedWebView) findViewById(R.id.url_load_webview);