滚动时AppBarLayout子级不会折叠

编程入门 行业动态 更新时间:2024-10-10 11:22:23
本文介绍了滚动时AppBarLayout子级不会折叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个AppBarLayout,在子级中包含一个水平的RecyclerView. AppBarLayout的同级项是ViewPager(包含垂直的RecyclerView).

I have a AppBarLayout which contains an horizontal RecyclerView among its children. The sibling of the AppBarLayout is a ViewPager (which contains a vertical RecyclerView).

这是XML:

<android.support.design.widget.CoordinatorLayout android:id="@+id/coordinatorLayout" xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto" xmlns:tools="schemas.android/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ui.search.SearchResultFragment"> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> <android.support.design.widget.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:elevation="@dimen/d2" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <LinearLayout android:id="@+id/header_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@color/white" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/back_image_view" android:layout_width="?attr/actionBarSize" android:layout_height="?attr/actionBarSize" android:scaleType="center" android:src="@drawable/ic_backarrow_gray"/> <android.support.v7.widget.RecyclerView android:id="@+id/query_terms_recycler_view" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_weight="1"/> <ImageView android:id="@+id/search_image_view" android:layout_width="?attr/actionBarSize" android:layout_height="?attr/actionBarSize" android:scaleType="center" android:src="@drawable/ic_search_gray"/> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/related_terms_recycler_view" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/search_term_bg" app:layout_scrollFlags="scroll|enterAlways"/> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="@dimen/d8" android:background="@color/tab_and_nav_bar"/> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout>

我已经有了另一个具有AppBarLayout的UI,该UI可以通过Toolbar成功响应滚动.代码几乎相同,但是在这种情况下,出现了一些错误,因为RecyclerView不会折叠/隐藏.

I already have another UI with AppBarLayout which is able to respond to scroll successfully with a Toolbar . The code is pretty much the same, but in this case something is wrong because the RecyclerView is not collapsing / hiding.

推荐答案

非常感谢@m vai的提示

我解决了自定义AppBarLayout行为的问题 这是代码:

I solved this problem customising the behaviour of the AppBarLayout Here is the code:

private void setAppBarLayoutBehaviour() { AppBarLayout.Behavior behavior = new AppBarLayout.Behavior() { @Override public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) { // Trigger the following events if it is a vertical scrolling return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes); } @Override public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed); // If I slowly reach the top, without fling, show the RecyclerView int[] firstVisiblePositions = ((StaggeredGridLayoutManager) ((RecyclerView) target).getLayoutManager()).findFirstCompletelyVisibleItemPositions(null); for (int position : firstVisiblePositions) { if (position == 0) { showRelatedTerms(); break; } } } @Override public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) { if (velocityY > 500) { // Hide the RecyclerView } else if (velocityY < -500) { // Show the recyclerView } return true; } }; ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).setBehavior(behavior); }

而且我没有在XML中应用任何scrollFlags.

And I didn't apply any scrollFlags in the XML.

更多推荐

滚动时AppBarLayout子级不会折叠

本文发布于:2023-11-23 04:21:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620069.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AppBarLayout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!