【问题标题】:Scrolling is not smooth while using 3 recyclerviews inside nestedscrollview in android在android的nestedscrollview中使用3个recyclerviews时滚动不流畅
【发布时间】:2016-07-19 10:54:16
【问题描述】:

我在一个布局文件中有三个 recyclerviews,它们具有水平对齐的 layoutmanagers,这些 recyclerviews 在 nestedscrollview 内,我也将三个 recyclerviews 的嵌套滚动设置为 false。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/frameLayout"
    android:background="@drawable/background">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="5dp"
                        android:text="FREE MOVIES"
                        android:textStyle="bold"
                        android:textSize="18sp"
                        android:textColor="@color/colorAccent"
                         />
                    <view
                        android:id="@+id/recycler_view"
                        class="android.support.v7.widget.RecyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:text="PREMIUM MOVIES"
                    android:textColor="@color/colorAccent"
                    android:textStyle="bold"
                    android:textSize="18sp" />

                <view
                    android:id="@+id/recycler_view1"
                    class="android.support.v7.widget.RecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true" />

            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tvTrending"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:text="PACKAGES"
                    android:textColor="@color/colorAccent"
                    android:textStyle="bold"
                    android:textSize="18sp" />

                <view
                    android:id="@+id/recycler_view2"
                    class="android.support.v7.widget.RecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:dividerHeight="10.0sp" />
            </LinearLayout>

        </LinearLayout>

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

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

</FrameLayout>

这是我从 MainActivity 的 viewpager 中调用的片段的布局。有这样的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.newitventure.musicnepal.MainActivity">

    <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/tabanim_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


            <include
                android:id="@+id/tool_bar"
                layout="@layout/toolbar" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMaxWidth="0dp"
                android:background="@color/colorPrimaryDark"
                app:tabGravity="fill"
                app:tabTextAppearance="@style/MyCustomTextAppearance"
                app:tabMode="scrollable" />

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

        <android.support.v4.view.ViewPager
            android:id="@+id/tabanim_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="50dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />


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

    <LinearLayout
        android:id="@+id/gad_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        android:visibility="visible">

    </LinearLayout>
</RelativeLayout>

问题是尝试在此布局中滚动时滚动不流畅。尝试垂直和水平滚动时变得非常滞后。这可能是什么问题?

【问题讨论】:

  • 对不起,如果我不明白,我不确定这里的相关性,顶部的 xml 是位于 ViewPager 内的底部的子视图?
  • 是的,在底部的 viewpager 中,我正在加载一个以顶部为布局的片段。

标签: android android-layout android-viewpager android-recyclerview android-coordinatorlayout


【解决方案1】:

这解决了我的问题:

mRecyclerView.setNestedScrollingEnabled(false);

详情:

Recyclerview inside ScrollView not scrolling smoothly

【讨论】:

    【解决方案2】:

    好的,

    我从给出的代码中看到了三种可能性

    1) 您在视图的根目录中使用了 RelativeLayout。虽然RelativeLayout 在很多方面都很棒,但它也有它的缺点。由于 RelativeLayout 是如何构建的,它会测量自己两次以及其中的所有子级。因此,对于当前显示的每一帧,Android 需要重新计算每个视图两次。

    2) 你的视图确实有一个“相当”深的结构,我认为你可以摆脱在 TextView 和 recycleview 对周围的 3 个线性布局中的每一个。

    3) 在显示回收站视图时,您在 bindView 方法中做了大量工作。

    现在所有这三个都可能很重要,并在使其变得滞后方面发挥作用。 我可能会从 2 开始(我认为仅此一项并不能完全解决它,但它应该会提高代码的可读性和速度) 然后我会检查 3 (如果您粘贴适配器的代码,如果那里有明确的内容,我可以查看它)。如果这仍然无济于事,请尝试 1(这可能很难,因为 RelativeLayout 非常适合结构化代码)

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多