下面是我的代码,用于处理我的应用程序actionBar / toolbar / tabBar和nestedScrollView以及其中的ViewPager(webView)的正确行为。 ViewPager基本上是一个普通的webView。
除了我的ViewPager无法垂直滚动外,一切正常。仅滚动NestedScrollView。我想要与Facebook应用程序相同的行为,如果您滚动新闻源(webView),tabBar会垂直移动工具栏。
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".ui.activity.FragmentContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" app:theme="@style/M.Toolbar" app:popupTheme="@style/M.Toolbar.PopupTheme" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways" > </android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="scrollable" android:visibility="gone" android:background="@color/background" app:background="@color/background" /> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_height="match_parent" android:layout_width="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:background="@android:color/transparent" android:fillViewport="true" > <com.m.ui.util.EdgeScrollViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" android:visibility="visible" > </com.m.ui.util.EdgeScrollViewPager> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout>
所以为了能够滚动我的viewPager(webView),我添加了一个扩展webView的新类:
公共类TouchyWebView扩展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); } }
这解决了viewPager(webView)变得可滚动的问题。但是,NestedScrollView变为静态并拒绝垂直滚动tabBar /工具栏。所以我坚持滚动webView或tabBar工具栏。如何同时使viewPager(webView)和NestedScrollView(tabBar / toolbar)可滚动?
我的webView
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white"> <com.m.ui.base.TouchyWebView android:id="@+id/web_webview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" android:visibility="visible" /> <LinearLayout android:id="@+id/web_loadingview" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" android:layout_gravity="center"> <ProgressBar style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminate="true" android:visibility="visible" android:layout_gravity="center"/> </LinearLayout> </FrameLayout>
问候
它适用于我,将此行添加到XML文件中的NestedScrollview。
android:fillViewport="true"
然后设置webview height wrap_content。
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
请尝试以下代码:
class NestedScrollWebView constructor(
context: Context,
attrs: AttributeSet): WebView(context, attrs), NestedScrollingChild2 {
private var lastMotionY = 0
private var nestedYOffset = 0
private val scrollOffset = IntArray(2)
private val scrollConsumed = IntArray(2)
private val childHelper = NestedScrollingChildHelper(this)
init {
isNestedScrollingEnabled = true
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event == null) return false
val motionEvent = MotionEvent.obtain(event)
val currentY = event.y.toInt()
when (event.action) {
MotionEvent.ACTION_DOWN -> {
nestedYOffset = 0
lastMotionY = currentY
startNestedScroll(View.SCROLL_AXIS_VERTICAL)
}
MotionEvent.ACTION_MOVE -> {
var deltaY = lastMotionY - currentY
if (dispatchNestedPreScroll(0, deltaY, scrollConsumed, scrollOffset)) {
deltaY -= scrollConsumed[1]
motionEvent.offsetLocation(0f, scrollOffset[1].toFloat())
nestedYOffset += scrollOffset[1]
}
lastMotionY = currentY - scrollOffset[1]
val oldY = scrollY
val newScrollY = Math.max(0, oldY + deltaY)
val dyConsumed = newScrollY - oldY
val dyUnconsumed = deltaY - dyConsumed
if (dispatchNestedScroll(0, dyConsumed, 0, dyUnconsumed, scrollOffset)) {
lastMotionY -= scrollOffset[1]
motionEvent.offsetLocation(0f, scrollOffset[1].toFloat())
nestedYOffset += scrollOffset[1]
}
motionEvent.recycle()
}
MotionEvent.ACTION_POINTER_DOWN,
MotionEvent.ACTION_UP,
MotionEvent.ACTION_CANCEL -> stopNestedScroll()
else -> { }
}
return super.onTouchEvent(event)
}
override fun startNestedScroll(axes: Int, type: Int) = childHelper.startNestedScroll(axes, type)
override fun stopNestedScroll(type: Int) = childHelper.stopNestedScroll(type)
override fun hasNestedScrollingParent(type: Int) = childHelper.hasNestedScrollingParent(type)
override fun dispatchNestedPreScroll(dx: Int, dy: Int, consumed: IntArray?, offsetInWindow: IntArray?, type: Int) =
childHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow, type)
override fun dispatchNestedScroll(dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, offsetInWindow: IntArray?, type: Int) =
childHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow, type)
}
并在布局中只留下WebView:
<com.widget.NestedScrollWebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
我遇到了类似的问题并用setFillViewport (true)
解决了我的一段代码
NestedScrollView scrollView = (NestedScrollView) findViewById (R.id.nest_scrollview);
scrollView.setFillViewport (true);
在片段里面我有一个listView,我坐在android:nestedScrollingEnabled="true"
..修复了滚动问题