【问题标题】:Lags in multiple layouts RecyclerView多个布局 RecyclerView 的滞后
【发布时间】:2016-01-27 20:11:01
【问题描述】:

我有两个布局的RecyclerView。第一个描述主要项目,第二个是facebooknative Ad。它可以工作,但每次 positionNativeViewAd 显示在显示屏上时 - 我们可以看到微滞后。

recycler_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="vertical"
android:padding="8dp"
android:id="@+id/list_item">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/food_picture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/blank2" />


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/test_name_food"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#242424"
            android:paddingTop="4dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="8dp"
        android:paddingBottom="8dp"
        android:paddingRight="8dp"
        android:paddingTop="4dp">
        <TextView
            android:id="@+id/ingr_count"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/test_name_ingr"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_weight="1"
            android:textAlignment="center"
            android:layout_gravity="center_vertical"
            android:gravity="center" />
        <!--
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:layout_weight="33">

                    <ImageView
                        android:id="@+id/ic_ingredient"
                        android:layout_width="24dp"
                        android:layout_height="24dp"
                        android:src="@drawable/ic_ingredient"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        android:paddingRight="8dp" />
                    <ImageView
                        android:id="@+id/ic_time"
                        android:layout_width="24dp"
                        android:layout_height="24dp"
                        android:src="@drawable/ic_time"
                        android:layout_gravity="center_vertical|right"
                        android:layout_weight="1"
                        android:paddingLeft="8dp" />
        </LinearLayout>
        -->



        <TextView
            android:id="@+id/look"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Просмотреть"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_gravity="center"
            android:textAlignment="center"
            android:gravity="center"
            android:textColor="#4CAF50"
            android:textStyle="bold"
            android:typeface="normal"
            android:layout_weight="1" />

    </LinearLayout>


</LinearLayout>

additional.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"
android:id="@+id/ad_test2"
android:background="@drawable/blank2">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/blank2"
    android:id="@+id/blank_holder"/>

如果您需要,这里是RecyclerAdapter.class https://gist.github.com/burnix/d0b32f0dc4b525ec3aa7 的要点,但我想,layouts 的一个有问题。

那么我该如何解决我的问题呢?

附:如果layouts 甚至具有相同的height,滞后不会消失。

【问题讨论】:

    标签: android android-layout android-recyclerview facebook-audience-network


    【解决方案1】:

    我对 Stackoverflow 非常不满。似乎只有大三学生才会在这里上网回答诸如“为什么我在 onClick 中有 NPE?”之类的问题...

    这是解决方案。希望它会有用: 要解决 Recycler 中的滞后问题: 在适配器中:

       Picasso.with(context)
              .load(...)
              .tag("resume_tag")
              .into(...);
    

    EndlessScrollListener 类中

       @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        final Picasso picasso = Picasso.with(context);
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            picasso.resumeTag("resume_tag");
        } else {
            picasso.pauseTag("resume_tag");
        }
    
    }
    

    在类中,调用 RecyclerView:

        if (offset == 0)
                                adapter.notifyItemRangeInserted(0, foodDataList.size());  //or adapter.notifyDataSetChanged()
                            else
                                adapter.notifyItemRangeChanged(foodDataList.size() - jsonArray.length(), foodDataList.size());   
    

    【讨论】:

    • 嗨@peter!很好的答案,答案的前半部分确实在一定程度上减少了延迟,为了获得更好的性能,我将 Picasso 的配置更改为RGB_565,这将图像质量降低到 2 字节/像素,而不是默认的 4 字节/像素。 (这个配置被 Glide 使用——谷歌推荐的图像处理库)。另外,我完全没有得到你答案的第二部分。你在这里说的是哪个偏移量?你能详细说明一下吗?
    猜你喜欢
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 2023-04-06
    • 2019-05-08
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多