我想知道是否有人试图在回收商视图中整合Facebook受众群体网络原生广告?
对于Facebook受众群体网络原生广告,有没有像mopub recycler view adapter https://github.com/mopub/mopub-android-sdk/wiki/Native-Ads-with-Recycler-View这样的简单解决方案?
答案 0 :(得分:3)
我使用recylerview实现了facebook nativead。我在这做什么:
(1)定义包含原生广告的自定义布局
(2)通过NativeAdsManager
在onCreate()中预加载原生广告(3)在适配器中覆盖onCreateViewHolder()以插入广告的视图和普通项目视图:
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
if (i == 4) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.native_ad, null);
}
else {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.normal_item, null);
}
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
(4)在适配器中覆盖onBindViewHolder()以绑定原生广告数据:
@Override
public void onBindViewHolder(ViewGroup viewGroup, int i) {
if (i == 4) {
customViewHolder.adTitleView.setText(nativeAd.getTitle()));
...
}
else {
customViewHolder.textView.setText(normalItem.getTitle()));
...
}
}
希望这会帮助你开始。
答案 1 :(得分:3)
我创建了一个用于在适配器中加载Facebook Native Ad的库。
https://github.com/ldt116/FBNativeAdAdapter
您可以查看示例
MyAdapter adapter = new MyAdapter();
FBNativeAdAdapter fbAdapter = FBNativeAdAdapter.Builder.with(PLACEHOLDER_ID, adapter).build();
RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setAdapter(fbAdapter);
rv.setLayoutManager(new LinearLayoutManager(this));
欢迎任何想法或意见
答案 2 :(得分:1)
以下是Facebook本身的示例: NativeAdRecyclerAdapter
package com.facebook.samples.AdUnitsSample.adapters;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.facebook.ads.AdOptionsView;
import com.facebook.ads.MediaView;
import com.facebook.ads.NativeAd;
import com.facebook.ads.NativeAdLayout;
import com.facebook.ads.NativeAdsManager;
import com.facebook.samples.AdUnitsSample.R;
import com.facebook.samples.AdUnitsSample.models.RecyclerPostItem;
import java.util.ArrayList;
import java.util.List;
public class NativeAdRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<RecyclerPostItem> mPostItems;
private List<NativeAd> mAdItems;
private NativeAdsManager mNativeAdsManager;
private Activity mActivity;
private static final int AD_DISPLAY_FREQUENCY = 5;
private static final int POST_TYPE = 0;
private static final int AD_TYPE = 1;
public NativeAdRecyclerAdapter(Activity activity,
List<RecyclerPostItem> postItems, NativeAdsManager
nativeAdsManager) {
mNativeAdsManager = nativeAdsManager;
mPostItems = postItems;
mAdItems = new ArrayList<>();
mActivity = activity;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == AD_TYPE) {
NativeAdLayout inflatedView = (NativeAdLayout) LayoutInflater.from(parent.getContext())
.inflate(R.layout.native_ad_unit, parent, false);
return new AdHolder(inflatedView);
} else {
View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout
.recycler_post_item, parent, false);
return new PostHolder(inflatedView);
}
}
@Override
public int getItemViewType(int position) {
return position % AD_DISPLAY_FREQUENCY == 0 ? AD_TYPE : POST_TYPE;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder.getItemViewType() == AD_TYPE) {
NativeAd ad;
if (mAdItems.size() > position / AD_DISPLAY_FREQUENCY) {
ad = mAdItems.get(position / AD_DISPLAY_FREQUENCY);
} else {
ad = mNativeAdsManager.nextNativeAd();
if (!ad.isAdInvalidated()) {
mAdItems.add(ad);
} else {
Log.w(NativeAdRecyclerAdapter.class.getSimpleName(), "Ad is invalidated!");
}
}
AdHolder adHolder = (AdHolder) holder;
adHolder.adChoicesContainer.removeAllViews();
if (ad != null) {
adHolder.tvAdTitle.setText(ad.getAdvertiserName());
adHolder.tvAdBody.setText(ad.getAdBodyText());
adHolder.tvAdSocialContext.setText(ad.getAdSocialContext());
adHolder.tvAdSponsoredLabel.setText(R.string.sponsored);
adHolder.btnAdCallToAction.setText(ad.getAdCallToAction());
adHolder.btnAdCallToAction.setVisibility(
ad.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
AdOptionsView adOptionsView =
new AdOptionsView(mActivity, ad, adHolder.nativeAdLayout);
adHolder.adChoicesContainer.addView(adOptionsView, 0);
List<View> clickableViews = new ArrayList<>();
clickableViews.add(adHolder.ivAdIcon);
clickableViews.add(adHolder.mvAdMedia);
clickableViews.add(adHolder.btnAdCallToAction);
ad.registerViewForInteraction(
adHolder.nativeAdLayout,
adHolder.mvAdMedia,
adHolder.ivAdIcon,
clickableViews);
}
} else {
PostHolder postHolder = (PostHolder) holder;
//Calculate where the next postItem index is by subtracting ads we've shown.
int index = position - (position / AD_DISPLAY_FREQUENCY) - 1;
RecyclerPostItem postItem = mPostItems.get(index);
postHolder.tvPostContent.setText(postItem.getPostContent());
}
}
@Override
public int getItemCount() {
return mPostItems.size() + mAdItems.size();
}
private static class PostHolder extends RecyclerView.ViewHolder {
TextView tvPostContent;
PostHolder(View view) {
super(view);
tvPostContent = view.findViewById(R.id.tvPostContent);
}
}
private static class AdHolder extends RecyclerView.ViewHolder {
NativeAdLayout nativeAdLayout;
MediaView mvAdMedia;
MediaView ivAdIcon;
TextView tvAdTitle;
TextView tvAdBody;
TextView tvAdSocialContext;
TextView tvAdSponsoredLabel;
Button btnAdCallToAction;
LinearLayout adChoicesContainer;
AdHolder(NativeAdLayout adLayout) {
super(adLayout);
nativeAdLayout = adLayout;
mvAdMedia = adLayout.findViewById(R.id.native_ad_media);
tvAdTitle = adLayout.findViewById(R.id.native_ad_title);
tvAdBody = adLayout.findViewById(R.id.native_ad_body);
tvAdSocialContext = adLayout.findViewById(R.id.native_ad_social_context);
tvAdSponsoredLabel = adLayout.findViewById(R.id.native_ad_sponsored_label);
btnAdCallToAction = adLayout.findViewById(R.id.native_ad_call_to_action);
ivAdIcon = adLayout.findViewById(R.id.native_ad_icon);
adChoicesContainer = adLayout.findViewById(R.id.ad_choices_container);
}
}
}