【问题标题】:CoordinatorLayout scroll handling breaks downCoordinatorLayout 滚动处理崩溃
【发布时间】:2016-08-12 00:09:03
【问题描述】:

我有一个包含工具栏和 RecyclerView 的 CoordinatorLayout。我已经配置了布局,以便在用户滚动 RecyclerView 时工具栏会缩小并隐藏。这工作正常。除非 RecyclerView 的单元格(行)本身包含水平滚动的 RecyclerView。当我点击这样的单元格并向上滚动时,工具栏不会隐藏。似乎 CoordinatorLayout 处理这些单元格的滚动事件并以某种方式感到困惑。有没有办法解决这个问题?

这是我的布局:

<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.support.design.widget.AppBarLayout
      android:id="@+id/main.appbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

      <android.support.v7.widget.Toolbar
          android:id="@+id/main.toolbar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:layout_scrollFlags="scroll|enterAlways">

          <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Button 1" />

          <Button
              android:id="@+id/button2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Button 2" />
      </android.support.v7.widget.Toolbar>
  </android.support.design.widget.AppBarLayout>

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@android:color/white"
      android:paddingBottom="0pt"
      android:paddingLeft="0pt"
      android:paddingRight="0pt"
      android:paddingTop="0pt"
      app:layout_behavior="@string/appbar_scrolling_view_behavior">

      <android.support.v7.widget.RecyclerView
          android:id="@+id/recyclerView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_alignParentBottom="true"
          android:layout_alignParentEnd="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentStart="true"
          android:layout_alignParentTop="true">
      </android.support.v7.widget.RecyclerView>

  </RelativeLayout>

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

【问题讨论】:

  • 请发布您的 RecyclerView 行布局。

标签: android


【解决方案1】:

在我提出这个问题后不久,我就找到了答案。

https://stackoverflow.com/a/32975317/1036017

本质上,对于任何包含水平滚动 RecyclerView 的单元格,我们必须禁用嵌套滚动。这是此类单元格的示例布局。我们将android:nestedScrollingEnabled 属性设置为false。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="250dp">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:nestedScrollingEnabled="false">
    </android.support.v7.widget.RecyclerView>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        app:srcCompat="@drawable/left_chevron"
        android:id="@+id/leftArrow" />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        app:srcCompat="@drawable/right_chevron"
        android:id="@+id/rightArrow" />
</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-29
    • 2019-01-19
    • 1970-01-01
    • 2019-04-09
    • 2011-07-10
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多