我有以下布局:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<ImageView
android:layout_width="match_parent"
android:layout_height="160dp"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="52dp"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="com.example.CardViewBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="@+id/fixedBanner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:id="@+id/card1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- random content -->
</FrameLayout>
<FrameLayout
android:id="@+id/card2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</layout>
从 fixedBanner
或 card1
向下滚动(手指向上)时,appBarLayout
首先折叠,然后 nestedScrollView
> 向下滚动。但是,如果从 recyclerView
向下滚动,recyclerView
就会开始滚动。我希望 nestedScrollView
在 recyclerView
之前先向下滚动。
我尝试使用在 nestedScrollView
上设置的自定义 CardViewBehavior
,如果 appBarLayout
未完全折叠且有,它会覆盖 onNestedPreScroll 以消耗滚动增量仍然可以在 nestedScrollView
中滚动。
但是,如果我在 recyclerView
上滑动得足够快,recyclerView
就会在 nestedScrollView
完全滚动到底部之前开始滑动。我尝试在 CardViewBehavior
中重写 onNestedPreFling
和 onNestedFling
,但是当 RecyclerView 开始自行 fling 时,这两个方法似乎从未被调用。
如何确保 nestedScrollView
在 recyclerView
开始滚动之前滚动到底部?
最佳答案
扩展NestedScrollView
并直接重写onNestedPreScroll
方法。
(在 https://www.androiddesignpatterns.com/2018/01/experimenting-with-nested-scrolling.html 上找到解决方案)
关于android - 拦截RecyclerView在NestedScrollView中向下滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56446819/