gpt4 book ai didi

android - 广告使recyclerview滞后

转载 作者:行者123 更新时间:2023-12-02 04:23:38 25 4
gpt4 key购买 nike

我将来自 admob 的横幅广告放在我的 recyclerview 中,当用户滚动时它滞后于我的 recyclerview,这是我正在谈论的 recyclerview 的适配器代码。它使recyclerview滞后有什么问题?

public class NewReleasesAdapter extends RecyclerView.Adapter< RecyclerView.ViewHolder> implements BillingProcessor.IBillingHandler {
public static final String TAG = NewReleasesAdapter.class.getSimpleName();
private LayoutInflater mInflater;
private Context mContext;
private List<Release> mNewReleases;
private DatabaseHelper mDatabaseHelper;
// A game/release item view type.
private static final int GAME_ITEM_VIEW_TYPE = 0;
// The banner ad view type.
private static final int BANNER_AD_VIEW_TYPE = 1;
// Billing
private BillingProcessor mBillingProcessor;
private boolean mIsProVersion;
// Ads
private AdRequest mAdRequest;

public NewReleasesAdapter(Context context) {
mContext = context;
mInflater = LayoutInflater.from(mContext);
mDatabaseHelper = DatabaseHelper.getDatabaseHelper(mContext);

mBillingProcessor = new BillingProcessor(
mContext,
"...",
this);
mBillingProcessor.initialize();
mBillingProcessor.loadOwnedPurchasesFromGoogle();

// Is in europe
if (ConsentInformation.getInstance(mContext).isRequestLocationInEeaOrUnknown()) {
switch (ConsentInformation.getInstance(mContext).getConsentStatus()) {
case PERSONALIZED:
// Personalized ads
mAdRequest = new AdRequest.Builder().build();
break;
case NON_PERSONALIZED:
// fall though to none Personalized ads
default:
// None Personalized ads
Bundle extras = new Bundle();
// npa is short for "none personalized ads"
extras.putString("npa", "1");
mAdRequest = new AdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
.build();
break;
}
} else {
// Personalized ads
mAdRequest = new AdRequest.Builder().build();
}
}

public void setNewReleases(List<Release> newReleases) {
this.mNewReleases = newReleases;
notifyDataSetChanged();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch(viewType) {
case GAME_ITEM_VIEW_TYPE:
return new NewGameHolder(mInflater.inflate(R.layout.newly_added_game, parent, false));
case BANNER_AD_VIEW_TYPE:
return new ViewHolderAdMob(mInflater.inflate(R.layout.list_item_ad, parent, false), mAdRequest);
default:
return new NewGameHolder(mInflater.inflate(R.layout.newly_added_game, parent, false));
}
}

@Override
public int getItemViewType(int position) {
// isn't pro version
if (mIsProVersion) {
if ((position + 1) % 4 == 0) {
return BANNER_AD_VIEW_TYPE;
}
}
return GAME_ITEM_VIEW_TYPE;
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case BANNER_AD_VIEW_TYPE:
// nothing to do
break;
case GAME_ITEM_VIEW_TYPE:
// A lot of set texts
break;
}
}

@Override
public int getItemCount() {
if (mNewReleases == null) {
return 0;
}
return mNewReleases.size();
}

@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {

}

@Override
public void onPurchaseHistoryRestored() {

}

@Override
public void onBillingError(int errorCode, @Nullable Throwable error) {
// Log.d(TAG, "Error: " + errorCode + " : " + error.getMessage());
}

@Override
public void onBillingInitialized() {
mBillingProcessor.loadOwnedPurchasesFromGoogle();
if (mBillingProcessor.isPurchased("gaming_reminder_pro")) {
mIsProVersion = true;
// Toast.makeText(mContext, "You're a pro user! Thanks!", Toast.LENGTH_SHORT).show();
} else {
// Show ads
mIsProVersion = false;
}
}

public BillingProcessor getBillingProcessor() {
return mBillingProcessor;
}

public static class NewGameHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView summary;
public TextView releaseDate;
public TextView platforms;
public TextView genres;
public ImageView cover;
public Button favorite;
public ProgressBar coverProgressBar;
public RelativeLayout newReleaseLayout;

public NewGameHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.game_name);
summary = itemView.findViewById(R.id.summary);
releaseDate = itemView.findViewById(R.id.release_date);
platforms = itemView.findViewById(R.id.platforms);
genres = itemView.findViewById(R.id.genres);
cover = itemView.findViewById(R.id.cover_image);
favorite = itemView.findViewById(R.id.favorite);
coverProgressBar = itemView.findViewById(R.id.cover_progress_bar);
newReleaseLayout = itemView.findViewById(R.id.new_release);
}
}

public static class ViewHolderAdMob extends RecyclerView.ViewHolder {
public AdView mAdView;
public ViewHolderAdMob(View view, final AdRequest adRequest) {
super(view);
mAdView = view.findViewById(R.id.adView);
if (adRequest != null) {
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener(){
@Override
public void onAdLoaded() {
mAdView.setVisibility(View.VISIBLE);
}
});
}
}
}
}

一些更有用的信息:通常,此回收 View 中显示 4 个横幅广告,它们仅显示给非专业用户。我有一个删除广告的 pro iap(应用内购买)

最佳答案

最好在回收站 View 中展示原生广告,因为它在各个方面都更具可定制性。如果您必须显示横幅广告,我建议在 Activity 或 fragment 中预加载广告 View ,然后根据需要在回收站 View 中添加或显示它们。 ViewHolder不是加载广告的好地方。

你可以按照这个谷歌提供的例子

https://github.com/googleads/googleads-mobile-android-examples/tree/master/java/advanced/BannerRecyclerViewExample

关于android - 广告使recyclerview滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57029199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com