Android 布局嵌套ScrollView和RecyclerView会导致性能大幅下降
我在应用程序中使用NestedScrollView,以便在循环视图上滚动时隐藏工具栏。问题是,当我将RecyclerView放在NestedScrollView中时,RecyclerView的性能非常差,需要很长时间才能加载,滚动时会出现口吃 下面是我用RecyclerView实现的片段的xmlAndroid 布局嵌套ScrollView和RecyclerView会导致性能大幅下降,android,Android,我在应用程序中使用NestedScrollView,以便在循环视图上滚动时隐藏工具栏。问题是,当我将RecyclerView放在NestedScrollView中时,RecyclerView的性能非常差,需要很长时间才能加载,滚动时会出现口吃 下面是我用RecyclerView实现的片段的xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android
<?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可以获得更好的性能,但是我没有隐藏工具栏
我做错什么了吗?是的,请使用
AppBar
为工具栏
尝试协调布局。通过这种方式,使用正确的滚动行为,您可以在滚动列表时隐藏/折叠工具栏。当您将回收器视图放在滚动视图中时,默认情况下滚动视图滚动不适用于回收器视图滚动。
要修复此问题,您可以尝试使用android:nestedScrollingEnabled=“true”进行recyclerView。
希望这能解决您的问题。您是否在某个地方使用了
协调器布局
?这个布局是另一个布局的一部分还是独立的?是的,对不起。此布局适用于进入ViewPager内部的片段。我已经用ViewPager添加了更多信息和布局。我已经在片段中调用setNestedScrollingEnabled(false)将其设置为true…:P