首页 > 解决方案 > PublishedAdView 使 Scrollview 滞后

问题描述

PublishedAdView在 Scrollview 中创建了一些代码,如下所示

<ScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/ads_AnchorsAds"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <!--Some code -->

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="blocksDescendants"
                android:orientation="vertical">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <!-- some code -->


                    <RelativeLayout
                        android:id="@+id/relativeLayout"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/item_image">

                        <!-- some code -->

                        <LinearLayout
                            android:id="@+id/content_list"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:descendantFocusability="blocksDescendants"
                            android:orientation="vertical"
                            android:visibility="visible">

                            <com.google.android.gms.ads.doubleclick.PublisherAdView
                                android:id="@+id/ads_TopMediumRectangle"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:layout_marginTop="12dp"
                                android:paddingBottom="12dp"
                                android:visibility="gone"
                                app:adSize="MEDIUM_RECTANGLE"
                                app:adUnitId="@string/ads_TopMediumRectangle" />


                            <!-- Some Code -->

                            <com.google.android.gms.ads.doubleclick.PublisherAdView
                                android:id="@+id/ads_InsideMediumRectangle"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:layout_marginTop="4dp"
                                android:layout_marginBottom="4dp"
                                android:visibility="gone"
                                app:adSize="MEDIUM_RECTANGLE"
                                app:adUnitId="@string/ads_InsideMediumRectangle" />


                        </LinearLayout>
                    </RelativeLayout>
                </androidx.constraintlayout.widget.ConstraintLayout>
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>

一切正常,但这些 PublishedAdView 使我的滚动视图性能如此缓慢。我在文档中以正常方式将此广告加载到onCreateActivity 中。有没有办法克服它?我没主意了。这就是我的活动的样子

@Override
    protected void onCreate(Bundle savedInstanceState) {
        DesignUtil.configureNoAppBarTheme(ReadActivity.this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);

mPublisherAdViewInside = findViewById(R.id.ads_InsideMediumRectangle);
        mPublisherAdViewInside.loadAd(new PublisherAdRequest.Builder().build());
        mPublisherAdViewInside.setAdListener(new TribunAdListener(mPublisherAdViewInside));

}

标签: androidadmobscrollviewandroid-scrollviewadview

解决方案


我不知道这是否能解决您的问题。但我建议重构 ScrollView,使其只有 1 个孩子和其中的广告。你有很多无用的线性和相对布局,它们是嵌套的,可能会导致性能问题。

在 Google I/O 2013(为 Android 编写自定义视图)的一次演讲中,Romain Guy 澄清了导致每个人都开始使用 RelativeLayouts 的误解。一个RelativeLayout 总是必须做两次测量传递。总的来说,只要您的视图层次结构简单,它就可以忽略不计。但是,如果您的层次结构很复杂,那么进行额外的度量传递可能会相当昂贵。此外,如果您嵌套 RelativeLayouts,您将获得指数测量算法。

看看这是否有帮助,现在只尝试 1 个 Addview,并使用 Android Profiler 进行调试: https ://developer.android.com/studio/profile/android-profiler

这可能会帮助您找出问题所在。

另一种选择,如果不是,我会建议使用具有多种类型视图的 RecyclerView,而不是滚动视图。

在 RecyclerView 中使用类似的东西:

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when (viewType) {
            0 -> {
                val itemView = LayoutInflater.from(parent.context).inflate(R.layout.call_list_item, parent, false)
                return MyViewHolder(itemView)
            }
            else -> {
                val itemView = LayoutInflater.from(parent.context).inflate(R.layout.call_list_item, parent, false)
                return MyViewHolder(itemView)
            }
        }
    }

推荐阅读