【问题标题】:Collapsing toolbar scrolls down on flick折叠工具栏在轻弹上向下滚动
【发布时间】:2017-11-07 21:45:59
【问题描述】:

当我执行非常快速的混蛋时,CollapsingToolbarLayout 出现问题。 隐藏 appBar 和 tablayout 也会发生同样的事情。

我需要工具栏在滚动时隐藏,而 CollapsingToolbar 需要折叠。当滚动不是那么快时,它也能正常工作。

但是当它很快时 - appBar 似乎在那之后立即折叠和展开。但它还没有扩大。


请看视频。

CollapsingToolbarLayout

appBar & tablayout


看来this答案是我需要的,但它不起作用。


我的 CollapsingToolbarLayout 的 xml:

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorLightGray"
    android:clickable="true"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    >
  <android.support.design.widget.AppBarLayout
      android:id="@+id/app_bar_layout"
      android:layout_width="match_parent"
      android:layout_height="200dp"
      android:fitsSystemWindows="true"
      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:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
        >
      <ImageView
          android:id="@+id/ivSectionCover"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"
          android:scaleType="centerCrop"
          app:layout_collapseMode="parallax"
          />
      <ImageView
          android:layout_width="match_parent"
          android:layout_height="130dp"
          android:layout_gravity="bottom"
          android:alpha="0.5"
          android:scaleType="fitXY"
          app:layout_collapseMode="pin"
          app:srcCompat="@drawable/background_gradient"
          />
      <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"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
          />
    </android.support.design.widget.CollapsingToolbarLayout>
  </android.support.design.widget.AppBarLayout>
  <TextView
      android:id="@+id/tvEmptyList"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:text="@string/books_empty_list"
      android:textColor="@color/colorGray"
      android:textSize="18sp"
      android:visibility="gone"
      />
  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginBottom="?attr/actionBarSize"
      android:clickable="true"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      >
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:fillViewport="true"
        >
      <RelativeLayout
          android:id="@+id/rlContent"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:clickable="true"
          >
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvBooksSection"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginTop="4dp"
            android:clickable="true"
            />
      </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>
    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@drawable/toolbar_dropshadow"
        />
  </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 你找到解决办法了吗?我在 CollapsingToolbarLayout 中也有一个 recyclerview。当我向上滚动时,recyclerview 会用 jerk 执行操作
  • @Nik 请看我的回答,希望对您有所帮助

标签: android android-layout scroll toolbar android-collapsingtoolbarlayout


【解决方案1】:

不幸的是,我还没有找到解决方案,但我通过添加以下内容减少了混蛋的频率:

CoordinatorLayout.LayoutParams params =
    (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if (behavior != null) {
  behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
    @Override public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
      return false;
    }
  });
}
params.setBehavior(behavior);

我也像这样编辑了布局:

<android.support.design.widget.AppBarLayout
  android:id="@+id/app_bar_layout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:fitsSystemWindows="true"
  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:background="@color/colorLightGray"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    app:contentScrim="@color/colorLightGray"
    app:expandedTitleMarginEnd="64dp"
    app:expandedTitleMarginStart="48dp"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    >
  <ImageView
      android:id="@+id/ivSectionCover"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      android:scaleType="centerCrop"
      android:src="@color/colorLightGray"
      app:layout_collapseMode="parallax"
      />
  <ImageView
      android:layout_width="match_parent"
      android:layout_height="170dp"
      android:layout_gravity="bottom"
      android:alpha="0.5"
      android:scaleType="fitXY"
      app:layout_collapseMode="pin"
      app:srcCompat="@drawable/background_gradient"
      />
  <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|snap"
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
      />
</android.support.design.widget.CollapsingToolbarLayout>

【讨论】:

    【解决方案2】:

    尝试删除属性 Android:fitsSystemWindows="true" 或将其更改为 false。看起来布局在状态栏后面。 Fit系统窗口将允许布局超越状态栏甚至导航栏。

    【讨论】:

      猜你喜欢
      • 2015-10-28
      • 2016-01-26
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多