【问题标题】:PublishedAdView makes Scrollview laggPublishedAdView 使 Scrollview 滞后
【发布时间】:2019-08-30 14:24:30
【问题描述】:

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

<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 使我的滚动视图性能如此缓慢。我在documentation 中以正常方式在onCreate 的活动中加载此广告。有没有办法克服它?我没主意了。这就是我的活动的样子

@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));

}

【问题讨论】:

    标签: android admob scrollview android-scrollview adview


    【解决方案1】:

    我不知道这是否能解决您的问题。但我建议重构 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)
                }
            }
        }
    

    【讨论】:

    • 实际上每个RelativeLayout、ConstraintLayout等里面都有一堆代码(我记为“一些代码”),我只是做了这样的简单解释。但我不能删除那些布局,它对于其他用途非常重要。同样在我的情况下,不适合使用 recyclerview。因为我要显示小部件的动态变化(可能它们有不同的顺序)。如果我做一些实验,比如删除所有 AdView,我的滚动视图可以顺利运行。但我无法出于工作目的删除这些 Adviews
    猜你喜欢
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多