如何在 recyclerview 的不同位置加载不同的广告 android

How to load different ads at different position in recylerview android

我在 recyclerview 的项目中植入了广告。我的要求是在不同的位置加载不同的广告,例如 facebook 或其他 android 应用程序展示,但我无法在不同的位置加载不同的广告,每个位置都加载相同的广告。

这是我用于加载广告的方法。

public void loadAd(final Context context,
                   final AppInstallAdViewHolder viewHolder) {

    synchronized (mSyncObject) {

        if ((mAdLoader != null) && mAdLoader.isLoading()) {
           Log.d("MainActivity.LOG_TAG", "AppInstallAdFetcher is already loading an ad.");
            return;
        }

        // If an ad previously loaded, reuse it instead of requesting a new one.
        if (viewHolder.mAppInstallAd != null) {
            viewHolder.populateView(context,viewHolder.mAppInstallAd);
            return;
        }

        NativeAppInstallAd.OnAppInstallAdLoadedListener appInstallAdListener =
                new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
                    public void onAppInstallAdLoaded(NativeAppInstallAd ad) {
                        viewHolder.mAppInstallAd = ad;
                        viewHolder.populateView(context,viewHolder.mAppInstallAd);
                    }
                };

        if (mAdLoader == null) {
            mAdLoader = new AdLoader.Builder(context, ConstantVariables.NATIVE_AD_UNIT_ID)
                            .forAppInstallAd(appInstallAdListener)
                    .withAdListener(new AdListener() {
                        @Override
                        public void onAdFailedToLoad(int errorCode) {
                            mViewHolder.hideView();
                        }
                    }).build();
        }
        mAdLoader.loadAd(new PublisherAdRequest.Builder()
                .build());
    }
}

非常感谢!!!

不要那样做。

如果您将多个具有相同 ID 的广告放在一个页面上,您的 admob 帐户将被禁止。

您可以通过创建多个广告 ID 来实现。但为此您需要创建大量广告 ID 才能在 RecyclerView.

中显示

The number of ads on a single screen should not exceed one

查看Ad Placement

下的官方文档

首先,覆盖 getItemViewType 方法:

@Override
public int getItemViewType(int position) {

    int id_of_your_type;
     //Implement your logic here
    return id_of_your_type;
}

然后您可以简单地使用 onCreateViewHolder 的第二个参数并在视图和广告之间切换: 您应该为每个视图创建一个支架。

 @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v=null;
          RecyclerView.ViewHolder holder = null;
            switch(viewType){
                case TYPE_Ad1:
                    v = LayoutInflater.from(viewGroup.getContext())
                            .inflate(R.layout.layout1, viewGroup, false);
                 holder = new TYPE_Ad1_Holder();                
                 break;
                case TYPE_2:
                    v = LayoutInflater.from(viewGroup.getContext())
                            .inflate(R.layout.layout2, viewGroup, false);

                    holder = new Type2_Holder();                
                    break;
                case TYPE_Ad2:
                    v = LayoutInflater.from(viewGroup.getContext())
                            .inflate(R.layout.layout3, viewGroup, false);

                    holder = new TYPE_Ad2_Holder();                
                    break;
            }
        return holder;
    }