Webview Inside Nestedscrollview Cannot Be Scrolled
Solution 1:
By the usage of NestedScrollingChild Interface, we can implements the NestedScrollView properties in the webview. And customize the scrolling functionality under the onTouchEvent() method.
publicclassNestedWebViewextendsWebViewimplementsNestedScrollingChild {
privateint mLastY;
privatefinalint[] mScrollOffset = newint[2];
privatefinalint[] mScrollConsumed = newint[2];
privateint mNestedOffsetY;
private NestedScrollingChildHelper mChildHelper;
publicNestedWebView(Context context) {
this(context, null);
}
publicNestedWebView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.webViewStyle);
}
publicNestedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mChildHelper = newNestedScrollingChildHelper(this);
setNestedScrollingEnabled(true);
}
@OverridepublicbooleanonTouchEvent(MotionEvent ev) {
booleanreturnValue=false;
MotionEventevent= MotionEvent.obtain(ev);
finalintaction= MotionEventCompat.getActionMasked(event);
if (action == MotionEvent.ACTION_DOWN) {
mNestedOffsetY = 0;
}
inteventY= (int) event.getY();
event.offsetLocation(0, mNestedOffsetY);
switch (action) {
case MotionEvent.ACTION_MOVE:
inttotalScrollOffset=0;
intdeltaY= mLastY - eventY;
// NestedPreScrollif (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
totalScrollOffset += mScrollOffset[1];
deltaY -= mScrollConsumed[1];
event.offsetLocation(0, -mScrollOffset[1]);
mNestedOffsetY += mScrollOffset[1];
}
returnValue = super.onTouchEvent(event);
// NestedScrollif (dispatchNestedScroll(0, mScrollOffset[1], 0, deltaY, mScrollOffset)) {
totalScrollOffset += mScrollOffset[1];
event.offsetLocation(0, mScrollOffset[1]);
mNestedOffsetY += mScrollOffset[1];
mLastY -= mScrollOffset[1];
}
mLastY = eventY - totalScrollOffset;
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@OverridepublicvoidsetNestedScrollingEnabled(boolean enabled) {
mChildHelper.setNestedScrollingEnabled(enabled);
}
@OverridepublicbooleanisNestedScrollingEnabled() {
return mChildHelper.isNestedScrollingEnabled();
}
@OverridepublicbooleanstartNestedScroll(int axes) {
return mChildHelper.startNestedScroll(axes);
}
@OverridepublicvoidstopNestedScroll() {
mChildHelper.stopNestedScroll();
}
@OverridepublicbooleanhasNestedScrollingParent() {
return mChildHelper.hasNestedScrollingParent();
}
@OverridepublicbooleandispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed,
int[] offsetInWindow) {
return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
}
@OverridepublicbooleandispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
}
@OverridepublicbooleandispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
}
@OverridepublicbooleandispatchNestedPreFling(float velocityX, float velocityY) {
return mChildHelper.dispatchNestedPreFling(velocityX, velocityY);
}
}
Declare NestedWebView instead of declaring webview inside the NestedScrollView.For example,
<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" />
Instead of declaring Webview you can initialize as NestedWebView inside your Activity
private NestedWebView mShopWebView;
mShopWebView = (NestedWebView) findViewById(R.id.url_load_webview);
Solution 2:
Got this one working with changing the height attribute of the underlying Webview.
For NestedScrollView use attribute
android:fillViewport="true"
and for WebView use android:layout_height="wrap_content"
Full Answer here
Solution 3:
I faced a similar problem but I solved it on multiple Android versions by using the following custom WebView class which manages the touch events:
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
*/publicclassCustomWebViewextendsWebView {
publicCustomWebView(Context context) {
super(context);
}
publicCustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
publicCustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@OverridepublicbooleanonTouchEvent(MotionEvent event) {
//Check pointer index to avoid -1 (error)if (MotionEventCompat.findPointerIndex(event, 0) == -1) {
returnsuper.onTouchEvent(event);
}
if (event.getPointerCount() >= 2) {
requestDisallowInterceptTouchEvent(true);
} else {
requestDisallowInterceptTouchEvent(false);
}
returnsuper.onTouchEvent(event);
}
@OverrideprotectedvoidonOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
requestDisallowInterceptTouchEvent(true);
}
}
Solution 4:
Just make NestedScrollView fillViewport = "false" and WebView layout_height = "wrap_content"
<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"/>
Solution 5:
Answer :
Just change webView's height match_parent
to wrap_content
.
When you do height = "match_parent"
then webview Not scroll in ScrollView
android:id="@+id/read_full_content_wv"
android:layout_width="match_parent"
android:layout_height="wrap_content"// just change height of webView
/> ```
Post a Comment for "Webview Inside Nestedscrollview Cannot Be Scrolled"