我有一个带有 CollapsingToolbarLayout
的 NestedScrollView
,我想让它顺利运行。
我的问题是,当从内容向上滚动时,我的折叠工具栏不会自动展开,而是在到达顶部时被阻止。然后我需要再次向上滚动以展开工具栏。
我想实现从自动展开我的 CollapsingToolbarLayout
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/product_detail_main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:apptools="http://schemas.android.com/tools"
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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:foregroundGravity="bottom|right"
android:foregroundTintMode="add"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:fitsSystemWindows="true"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
/*
CONTENT
*/
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
我发现了一些类似的问题,但没有一个答案对我有用。
最佳答案
LinearLayout 里面有什么样的内容?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
/*
CONTENT
*/
</LinearLayout>
如果其中一个内容是recyclerview,collapsing toolbar不响应滚动的原因是NestedScrollView
没有调用AppBarLayout.Behavour
当它从 recyclerview 接收到滚动事件时类。也就是说,当在 recyclerview 中发生滚动时,recyclerview 将滚动进度分派(dispatch)/传递给 NestedScrollView
。 NestedScrollView
接收滚动事件但不对其执行任何操作。
在 nestedscrollview 类中
@overide
public void onNestedpreScroll(View target,int dx, int dy, int[] consumed){
//Do nothing
}
为了克服这个问题并使 appbarlayout 在滚动 recyclerview 时展开/折叠,只需创建一个自定义类来扩展 NestedScrollView 并覆盖上述方法并调用方法 dispatchNestedPreScroll()
通知滚动事件的 appbarlayout并让它做出回应。
public class CustomNestedScrollView extends NestedScrollView{
public CustomNestedScrollView(Context context) {
super(context);
}
public CustomNestedScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@overide
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed){
dispatchNestedPreScroll(dx,dy,consumed,null);
}
}
然后在你的layout.xml中使用这个类
<com.my.package.CustomNestedScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
/*
CONTENT
*/
</LinearLayout>
关于android - 使用 NestedScrollView 折叠工具栏布局不会平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36981994/