【问题标题】:layout NestedScrollView with RecyclerView results in huge decrease in performance使用 RecyclerView 布局 NestedScrollView 会导致性能大幅下降
【发布时间】:2017-06-14 05:51:59
【问题描述】:

我在我的应用程序中使用 NestedScrollView,以便在 RecyclerView 上滚动时隐藏工具栏。问题是当我将 RecyclerView 放在 NestedScrollView 中时,RecyclerView 的性能非常糟糕,加载时间长,滚动时卡顿。

这是我的带有 RecyclerView 的片段的 xml 实现。

<?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="match_parent">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:elevation="0dp"
    android:visibility="gone"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:layout_scrollFlags="scroll|enterAlways"/>

<android.support.v4.widget.NestedScrollView
    android:isScrollContainer="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar"
    android:background="@color/light_gray"
    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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:clickable="true" />

</android.support.v4.widget.NestedScrollView>

</RelativeLayout>

这是一个片段进入 ViewPager。这是 ViewPager 的另一个布局:

<?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.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:elevation="0dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        android:elevation="0dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways">

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="left|center_vertical"
            android:layout_weight="1"
            android:gravity="left|center_vertical"
            android:text="@string/app_name"
            android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
            android:textColor="@color/white"
            android:textSize="18sp"/>

    </android.support.v7.widget.Toolbar>

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

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:visibility="gone"
        app:tabIndicatorColor="@android:color/white"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@android:color/white"
        app:tabIndicatorHeight="3dp"
        app:tabTextColor="@color/white"/>

</android.support.v4.view.ViewPager>

<ProgressBar
    android:id="@+id/eventsProgress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/>

<FrameLayout
    android:id="@+id/search_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    />

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

删除 NestedScrollView 并单独保留 RecyclerView 会带来更好的性能,但我没有隐藏工具栏。

我做错了吗?

【问题讨论】:

  • 你在某处使用CoordinatorLayout吗?这个布局是另一个布局的一部分还是独立的?
  • 是的,对不起。此布局适用于进入 ViewPager 的片段。我使用 ViewPager 添加了更多信息和布局。

标签: android


【解决方案1】:

当您在滚动视图中放置回收器视图时,默认情况下滚动视图滚动不起作用,而不是回收器视图滚动。 要修复它,您可以尝试 android:nestedScrollingEnabled="true" 获取 recyclerView。 希望这能解决您的问题。

【讨论】:

  • 我已经在片段中调用了 setNestedScrollingEnabled(false)
  • 设置为真...:P
【解决方案2】:

是的,试试CoordinatorLayoutAppBar 换成Toolbar。通过这种方式使用正确的滚动行为,您可以在列表滚动时隐藏/折叠工具栏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2022-01-07
    • 2012-04-17
    • 1970-01-01
    • 2011-09-10
    相关资源
    最近更新 更多