【问题标题】:AppBarLayout not collapsing during programmatically scrollAppBarLayout 在以编程方式滚动期间不折叠
【发布时间】:2015-10-15 07:21:02
【问题描述】:

我有一个带有片段的 ViewPager,其中包含一个 RecyclerViews。 CollapsingTollbarLayout 位于 ViewPager 上方。除了在 RecyclerView 上以编程方式滚动之外,一切工作正常。然后 AppBarLayout 或 CollapsingToolbarLayout 没有响应。

这是我的基本布局 xml:

<?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:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_behavior="de.wackernagel.playball.ui.FlingBehaviour">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:id="@+id/collapsingToolbarLayout"
        app:contentScrim="?attr/colorPrimary"
        app:titleEnabled="false"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:contentDescription="@null"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/showdown"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/doubleActionBarSize"
            android:minHeight="?attr/actionBarSize"
            android:gravity="top"
            app:titleMarginTop="14dp"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            android:layout_gravity="bottom"
            app:tabTextColor="#BEFFFFFF"
            app:tabSelectedTextColor="#FFFFFFFF"
            app:tabContentStart="@dimen/keyline_2"
            app:tabMode="scrollable" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

我的目标是在像普通触摸滚动一样以编程方式滚动时折叠 CollapsingToolbarLayout。

我是这样做的

Activity get Fragment at current viewpager position
    Fragment get LayoutManager from RecyclerView
         LayoutManager scrollToPosition x

滚动行为在 RecyclerView 上有效,但 CollapsingToolbarLayout 没有响应。

编辑

这是我的片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<de.wackernagel.playball.views.EmptyAwareRecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="8dp" />

<TextView
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"/>

【问题讨论】:

  • 我写了一篇关于在检测到滚动时触发折叠的帖子,需要使用 dpad(使用程序滚动)novoda.com/blog/fixing-hiding-appbarlayout-android-tv
  • @ataulm 谢谢你的提示。它为我指明了正确的方向。
  • @ataulm 顺便说一句,您检测程序滚动( !recyclerView.isInTouchMode() )的技术不起作用。由于缺少触摸屏,这可能仅适用于电视。
  • 不,由于其他原因它不起作用:P isInTouchMode 如果最后一次用户交互是通过触摸而不是键盘、dpad、远程等,则返回 true。

标签: android android-recyclerview androiddesignsupport android-collapsingtoolbarlayout


【解决方案1】:

这个问题很容易解决,原因是getNestedScrollingParentForType(type) in NestedScrollingChildHelper#dispatchNestedScroll 返回null (support lib version = 27.0.2) 对于非触摸滚动,所以滚动不是派出。因此,在以编程方式滚动之前应该执行以下操作:

if (!recyclerView.hasNestedScrollingParent(ViewCompat.TYPE_NON_TOUCH)) {
    recyclerView.startNestedScroll(View.SCROLL_AXIS_VERTICAL, ViewCompat.TYPE_NON_TOUCH);
}

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,当 NestedScrollView 以编程方式滚动到底部时,AppBarLayout 不会折叠。这导致 Android 在展开 AppBarLayout 时错误计算 NestedScrollView 的底部。当它被折叠时,它会正确地滚动到底部,但是当它展开时,它不会滚动到底部的量等于 AppBarLayout 的高度。我通过在尝试滚动之前立即将 setExpanded 设置为 false 找到了一个简单的解决方法。

    //scroll to the end of NestedScrollView
    AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout);
    appBarLayout.setExpanded(false, true);
    scrollView.post(() -> scrollView.fullScroll(View.FOCUS_DOWN));
    

    ...或者如果不使用 lambda 表达式(即

    //scroll to the end of NestedScrollView
    AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout);
    appBarLayout.setExpanded(false, true);
    scrollView.post(new Runnable() {
        @Override
        public void run() {
            scrollView.fullScroll(View.FOCUS_DOWN);     
        }
    });
    

    【讨论】:

      【解决方案3】:

      <android.support.v4.view.ViewPager
          android:id="@+id/htab_viewpager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior" />
      
      <android.support.design.widget.AppBarLayout
          android:id="@+id/htab_appbar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:fitsSystemWindows="true"
          app:layout_behavior="example.com.final.TabParallax.FlingBehavior"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
      
          <android.support.design.widget.CollapsingToolbarLayout
              android:id="@+id/htab_collapse_toolbar"
              android:layout_width="match_parent"
              android:layout_height="360dp"
              android:fitsSystemWindows="true"
              app:contentScrim="?attr/colorPrimary"
              app:layout_scrollFlags="scroll|exitUntilCollapsed">
      
              <FrameLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_horizontal"
                  android:padding="0dp">
      
                  <ImageView
                      android:id="@+id/htab_header"
                      android:layout_width="match_parent"
                      android:layout_height="359dp"
                      android:background="@drawable/channel"
                      android:fitsSystemWindows="true"
                      android:scaleType="centerCrop"
                      app:layout_collapseMode="parallax" />
      
              </FrameLayout>
      
          </android.support.design.widget.CollapsingToolbarLayout>
      
          <android.support.design.widget.TabLayout
              android:id="@+id/htab_tabs"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="@color/colorAccent1"
              android:fitsSystemWindows="true"
              android:gravity="bottom"
              app:tabGravity="center"
              app:tabIndicatorColor="@android:color/white"
              app:tabMode="scrollable"
              app:tabSelectedTextColor="@android:color/white"
              app:tabTextColor="@android:color/white"
              />
      </android.support.design.widget.AppBarLayout>
      

      【讨论】:

      • 请添加描述
      • 我认为你需要将你的 tablayout 放在 CollapsingToolbarLayout 之外
      猜你喜欢
      • 2017-01-14
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      相关资源
      最近更新 更多