广告使recyclerview滞后

时间:2019-07-14 16:29:28

标签: android android-recyclerview

我将来自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);
                    }
                });
            }
        }
    }
}

更多有用的信息:此recyclerview中通常显示4个横幅广告,并且仅面向非专业用户显示。我有一个Pro Iap(在应用内购买)可以删除广告

1 个答案:

答案 0 :(得分:1)

最好在回收站视图中显示原生广告,因为它可以通过各种方式进行自定义。如果必须显示横幅广告,建议您将广告视图预加载到活动或片段中,然后根据需要在回收者视图中添加或显示它们。 ViewHolder不是加载广告的好地方。

您可以按照google提供的示例

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