使用AppBarLayout和ViewPager(回收站视图)

问题描述 投票:0回答:1

PIC1

enter image description here

Area1是AppBarLayout,ViewPager(Recycler视图)位于AppBarLayout下方。

当我触摸Area2以缓慢或快速向上滚动时,它工作正常。但当我触摸Area1快速向上滚动时,视图会向上滚动,然后自动向下滚动。

支持:26.0.2(26.1.0 / 27.0.2)

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        app:elevation="0dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:background="@android:color/holo_green_light"
            android:gravity="center"
            android:text="TEST"
            android:textColor="@android:color/holo_orange_dark"
            android:textSize="64sp"
            app:layout_scrollFlags="scroll|enterAlways"/>
        <!--category-->
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabIndicatorHeight="4dp"
            app:tabSelectedTextColor="#000"
            app:tabTextColor="#fff"/>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

我使用支持23.4.0而不是26.0.2,布局滚动正常。

android android-coordinatorlayout android-appbarlayout
1个回答
1
投票

只有在NestedScrollView(或RecyclerView)尚未完成投掷时滚动/抛出AppBar时才会发生这种情况。

解决方案:在NestedScroll尚未停止时触摸AppBar时,扩展AppBar的默认行为并阻止对AppBar.Behavior的onNestedPreScroll()和onNestedScroll()的调用。

@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed, int type) {
    if (type == TYPE_FLING) {
        isFlinging = true;
    }
    if (!shouldBlockNestedScroll) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
    }
}

@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
    if (!shouldBlockNestedScroll) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
    }
}

然后在布局上使用它:

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    ...
    app:layout_behavior="com.mypackage.NoBounceBehavior"/>

参考完整代码:https://gist.github.com/ampatron/9d56ea401094f67196f407f82f14551a

© www.soinside.com 2019 - 2024. All rights reserved.