RecyclerView与大型列表的严重滞后

时间:2019-05-11 07:12:45

标签: android android-recyclerview

当适配器中有50多个项目时,我在recyclerview上面临严重的滚动延迟。该片段正在跳过帧,并且UI / UX受到严重影响。我正在从网上动态填充列表,这是延迟加载。

See the problem

我具有以下recyclerview配置:

    myRecyclerView=getActivity().findViewById(R.id.myRecyclerView);
    myListAdapter.setHasStableIds(true);
    myListAdapter=new myListAdapter(myRecyclerView,getContext(),R.id.webMRFrame);
    myRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    myRecyclerView.setHasFixedSize(false);

    myRecyclerView.setItemViewCacheSize(200);
    myRecyclerView.setDrawingCacheEnabled(true);
    myRecyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    myRecyclerView.setNestedScrollingEnabled(false);
    myRecyclerView.setAdapter(myListAdapter);

RecyclerView布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent"
android:layout_width="match_parent">

<FrameLayout
android:id="@+id/myFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bittentech.hindinews.Fragments.HomeFragment">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/mySwipeRefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/myNestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="true"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <!-- TODO: Update blank fragment layout -->
            <android.support.v7.widget.RecyclerView
                android:id="@+id/myRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <ProgressBar
                android:id="@+id/myProgress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/myRecyclerView" />


        </RelativeLayout>

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



</FrameLayout>

</LinearLayout>

我的Viewholder项:

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

    <android.support.v7.widget.CardView
        android:id="@+id/myCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/dimen_8x"
            android:orientation="vertical">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/dimen_8x"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/myImage"
                    android:layout_width="150dp"
                    android:layout_height="100dp"
                    app:srcCompat="@mipmap/ic_launcher" />

                <TextView
                    android:id="@+id/myTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="@dimen/dimen_8x"
                    android:fontFamily="sans-serif-condensed"
                    android:includeFontPadding="false"
                    android:text="Sample Title"
                    android:textColor="@color/defaultContentColor"
                    android:textSize="18dp" />

            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimen_4x"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/mySource"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/dimen_8x"
                    android:textColor="@color/colorPrimaryDark" />

                <TextView
                    android:id="@+id/myDate"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"

                    android:layout_weight="1"
                    android:textSize="@dimen/dimen_12x" />

            </LinearLayout>

        </LinearLayout>

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

    <View
        android:layout_below="@id/myCardView"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/black"/>
</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

将Recyclerview放在NestedScrollView内时,为每个项目调用RecyclerAdapter的OnBindViewHolder方法并一次绑定所有元素,这是一项昂贵的任务。因此,如果您不需要NestedScrollView的功能,则将其删除并将RecyclerView用作SwipeRefreshLayout的直接子代。

答案 1 :(得分:0)

删除这些行:

libpython3.*.so

答案 2 :(得分:0)

添加

            android:nestedScrollingEnabled="true"

到XML文件中的RecyclerView