已发布的AdView使Scrollview滞后



我在 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>

一切正常,但是这些已发布的广告视图使我的滚动视图性能如此缓慢。我在文档中以正常方式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));
}

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

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

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

这可能有助于您找出问题所在。

另一种选择,如果没有,我建议使用具有多种类型视图的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)
}
}
}

最新更新