android – 由于Admob广告,Recyclerview在滚动上滞后

前端之家收集整理的这篇文章主要介绍了android – 由于Admob广告,Recyclerview在滚动上滞后前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在RecyclerView中加载广告广告已成功加载,但在滚动时会出现这么多延迟

以下是我在Adapter的OnBindViewHolder方法中编写的代码

如何解决这个滞后?

@H_403_6@((DealHolder) holder).adcardView.post(new Runnable() { @Override public void run() { final NativeExpressAdView adView = new NativeExpressAdView(((DealHolder) holder).itemView.getContext()); final int adWidth = ((DealHolder) holder).adcardView.getWidth() - ((DealHolder) holder).adcardView.getPaddingLeft() - ((DealHolder) holder).adcardView.getPaddingRight(); final int adHeight = ((DealHolder) holder).adcardView.getHeight() - ((DealHolder) holder).adcardView.getPaddingBottom() - ((DealHolder) holder).adcardView.getPaddingTop(); final float scale = ((DealHolder) holder).adcardView.getResources().getDisplayMetrics().density; AdSize adSize = new AdSize((int) (adWidth / scale),(int) (adHeight / scale)); adView.setAdSize(adSize); adView.setAdUnitId(((DealHolder) holder).adcardView.getContext().getString(R.string.test_adunit_id)); AdRequest request = new AdRequest.Builder().addTestDevice("jjhjhbjhjhjhjhjhjhjh").build(); adView.loadAd(request); adView.setAdListener(new AdListener() { @Override public void onAdLoaded() { super.onAdLoaded(); } @Override public void onAdFailedToLoad(int i) { super.onAdFailedToLoad(i); } }); ((DealHolder) holder).adcardView.removeAllViews(); ((DealHolder) holder).adcardView.addView(adView); } });

解决方法

改进:

>当用户尝试滚动时,请勿尝试加载Adview.为此,使您的适配器recylerView滚动识别,然后检查是否scroll_state == RecylerView.Scroll_IDLE,然后只加载您的AdView.>如果您的所有adView具有相同的宽度和高度,则每次用户滚动时都不会计算它,请将其缓存在变量中.>不要动态添加删除NativeExpressAdView,而是在item_view中使用XML版本.您可以根据条件隐藏和显示.在XML视图中设置所有可能的必需属性.

猜你在找的Android相关文章