【发布时间】: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