Android 使用NestedScrollView折叠工具栏布局时无法平滑滚动
我有一个Android 使用NestedScrollView折叠工具栏布局时无法平滑滚动,android,android-layout,android-nestedscrollview,Android,Android Layout,Android Nestedscrollview,我有一个嵌套的滚动视图,带有折叠工具栏布局,我希望它能够顺利工作 我的问题是,当从内容向上滚动时,我的折叠工具栏不会自动展开,但当它到达顶部时会被阻止。然后我需要再次向上滚动以展开工具栏 我想实现从自动展开我的折叠工具栏布局的内容平滑向上滚动 这是我的密码: <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout android:id="@+id/pr
嵌套的滚动视图
,带有折叠工具栏布局
,我希望它能够顺利工作
我的问题是,当从内容向上滚动时,我的折叠工具栏不会自动展开,但当它到达顶部时会被阻止。然后我需要再次向上滚动以展开工具栏
我想实现从自动展开我的折叠工具栏布局的内容平滑向上滚动
这是我的密码:
<?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>
要克服此问题并使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>
/*
内容
*/
我通过SmoothAppBarLayout解决了这个问题
链接: 在应用程序级别build.gradle中添加此依赖项
compile "me.henrytao:smooth-app-bar-layout:25.3.1.0"
将NestedScrollView的子级标记为clickable和focusable true。这是XML布局
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable .../>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorLayoutPlaceDetail"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/adViewPlaceDetail"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<me.henrytao.smoothappbarlayout.SmoothAppBarLayout
android:id="@+id/appBarLayout"
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"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!--app:layout_scrollInterpolator="@android:anim/accelerate_decelerate_interpolator"-->
<ImageView
android:id="@+id/imgOfPlace"
android:layout_width="match_parent"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/placeholder"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.CollapsingToolbarLayout>
</me.henrytao.smoothappbarlayout.SmoothAppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:overScrollMode="never"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayoutInNestedScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<YOUR CONTENT VIEWS>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adViewPlaceDetail"
android:layout_width="0dp"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="..."
ads:layout_constraintBottom_toBottomOf="parent"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
在
.java
类中设置了RecyclerView
适配器之后添加以下代码:
ViewCompat.setNestedScrollingEnabled(View-name, false);
尝试在
CoordinatorLayout
中设置android:fitsystemwindows=“true”
。不,不起作用。找到解决方案了吗?@MitulVarmora您可以向我们提供布局,以便了解问题所在。您应该解释更多内容并格式化代码,谢谢
ViewCompat.setNestedScrollingEnabled(View-name, false);