我想在RecyclerView中加载广告,广告已经成功加载,但是滚动时会导致很多卡顿。
以下是我在Adapter的OnBindViewHolder方法中编写的代码。
如何解决这个问题?
((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);
}
});