Java Facebook原生广告与回收者视图
我知道这个问题被问了好几次,并且得到了不同的人的回答。 我想在Recycler视图中显示Facebook原生广告,我已经创建了一个适配器,如下所示Java Facebook原生广告与回收者视图,java,android,android-recyclerview,native-ads,Java,Android,Android Recyclerview,Native Ads,我知道这个问题被问了好几次,并且得到了不同的人的回答。 我想在Recycler视图中显示Facebook原生广告,我已经创建了一个适配器,如下所示 package com.zurmati.ilock.Adapter; public class MyNativeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { Context context; List<Themes> theme
package com.zurmati.ilock.Adapter;
public class MyNativeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
List<Themes> themesList;
NativeAdsManager nativeAdsManager;
int adItems = 0;
List<NativeAd> mAdItems;
private static final int AD_DISPLAY_FREQUENCY = 3;
private static final int POST_TYPE = 0;
private static final int AD_TYPE = 1;
public MyNativeAdapter(Context context, List<Themes> themesList, NativeAdsManager nativeAdsManager) {
this.context = context;
this.themesList = themesList;
this.nativeAdsManager = nativeAdsManager;
mAdItems = new ArrayList<>();
}
这是我的onBindViewHolder类,我在其中绑定元素
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case AD_TYPE:
NativeAd ad;
if (mAdItems.size() > position / AD_DISPLAY_FREQUENCY) {
ad = mAdItems.get(position / AD_DISPLAY_FREQUENCY);
} else {
ad = nativeAdsManager.nextNativeAd();
if (ad != null) {
if (!ad.isAdInvalidated()) {
mAdItems.add(ad);
}
}
}
UnifiedFbHolder adHolder = (UnifiedFbHolder) holder;
adHolder.adChoicesContainer.removeAllViews();
if (ad != null) {
adHolder.tvAdTitle.setText(ad.getAdvertiserName());
adHolder.tvAdBody.setText(ad.getAdBodyText());
adHolder.tvAdSocialContext.setText("Sponsored");
adHolder.btnCallToAction.setText(ad.getAdCallToAction());
adHolder.btnCallToAction.setVisibility(ad.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
AdOptionsView adOptionsView = new AdOptionsView(context, ad, adHolder.nativeAdLayout);
adHolder.adChoicesContainer.addView(adOptionsView, 0);
List<View> clickAbleViews = new ArrayList<>();
clickAbleViews.add(adHolder.ivAdIcon);
clickAbleViews.add(adHolder.mvAdMedia);
clickAbleViews.add(adHolder.btnCallToAction);
ad.registerViewForInteraction(adHolder.nativeAdLayout, adHolder.mvAdMedia,
adHolder.ivAdIcon, clickAbleViews);
}
break;
case POST_TYPE:
final ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
try {
Glide.with(context).load(themesList.get(holder.getAdapterPosition()).getImage()).into(imageViewHolder.imgTheme);
} catch (Exception e) {
e.printStackTrace();
}
//Event
imageViewHolder.setListener((v, pos) -> {
EventBus.getDefault().post(new ThemesItemClickEvent(themesList.get(pos).getImage(), pos, true));
});
break;
}
}
}
这是我的主要活动代码
private List<Themes> themesList;
NativeAdsManager nativeAdsManager;
private MyNativeAdapter myNativeAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_themes, null, false);
unbinder = ButterKnife.bind(this, root);
initViews();
return root;
}
private void initViews() {
themesList = new ArrayList<>();
addAdsToList();
//Add themes into the list
addThemesToList();
recyclerView.setHasFixedSize(true);
recyclerView.hasFixedSize();
recyclerView.setItemViewCacheSize(themesList.size());
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setItemAnimator(new DefaultItemAnimator());
//Set layout and orientation for recycler view
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, null, 0);
//Setting List to the Recycler View using adapter
// MyAdapter myAdapter = new MyAdapter(getContext(), themesList);
myNativeAdapter = new MyNativeAdapter(getContext(), themesList, nativeAdsManager);
/*FBNativeAdAdapter fbAdapter = FBNativeAdAdapter.Builder.with(getResources().getString(R.string.FbNativeAd), myAdapter)
.adItemIterval(3)
.build();*/
recyclerView.setAdapter(myNativeAdapter);
}
private void addAdsToList() {
nativeAdsManager = new NativeAdsManager(getContext(), getResources().getString(R.string.FbNativeAd), 2);
nativeAdsManager.loadAds();
nativeAdsManager.setListener(this);
}
private void addThemesToList() {
themesList.add(new Themes(R.drawable.a));
themesList.add(new Themes(R.drawable.b));
themesList.add(new Themes(R.drawable.c));
themesList.add(new Themes(R.drawable.d));
themesList.add(new Themes(R.drawable.e));
themesList.add(new Themes(R.drawable.f));
themesList.add(new Themes(R.drawable.g));
themesList.add(new Themes(R.drawable.h));
themesList.add(new Themes(R.drawable.i));
themesList.add(new Themes(R.drawable.j));
themesList.add(new Themes(R.drawable.k));
themesList.add(new Themes(R.drawable.l));
themesList.add(new Themes(R.drawable.m));
themesList.add(new Themes(R.drawable.n));
themesList.add(new Themes(R.drawable.o));
themesList.add(new Themes(R.drawable.p));
themesList.add(new Themes(R.drawable.q));
}
@Override
public void onAdsLoaded() {
}
@Override
public void onAdError(AdError adError) {
}
私有列表主题列表;
本地经理本地经理;
私人MyNativeAdapter MyNativeAdapter;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图根=充气器。充气(R.layout.fragment\u主题,null,false);
unbinder=ButterKnife.bind(这个,根);
initViews();
返回根;
}
私有void initViews(){
themesList=newArrayList();
addAdsToList();
//将主题添加到列表中
添加主题列表();
recyclerView.setHasFixedSize(true);
RecycleView.hasFixedSize();
recyclerView.setItemViewCacheSize(themesList.size());
recyclerView.setNestedScrollingEnabled(false);
setItemAnimator(新的DefaultItemAnimator());
//设置回收器视图的布局和方向
setLayoutManager(新的GridLayoutManager(getContext(),2));
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView,null,0);
//使用适配器将列表设置为回收器视图
//MyAdapter MyAdapter=新建MyAdapter(getContext(),主题列表);
myNativeAdapter=新的myNativeAdapter(getContext(),主题列表,NativeAdapter);
/*fbNativeAdapter fbAdapter=fbNativeAdapter.Builder.with(getResources().getString(R.string.FbNativeAd),myAdapter)
.adItemIterval(3)
.build()*/
recyclerView.setAdapter(myNativeAdapter);
}
私有无效addAdsToList(){
nativeadmanager=newnativeadmanager(getContext(),getResources().getString(R.string.FbNativeAd),2);
nativeadmanager.loadAds();
nativeadmanager.setListener(this);
}
private void addThemesToList(){
添加(新主题(R.drawable.a));
添加(新主题(R.drawable.b));
添加(新主题(R.drawable.c));
添加(新主题(R.drawable.d));
添加(新主题(R.drawable.e));
添加(新主题(R.drawable.f));
添加(新主题(R.drawable.g));
添加(新主题(R.drawable.h));
添加(新主题(R.drawable.i));
添加(新主题(R.drawable.j));
添加(新主题(R.drawable.k));
添加(新主题(R.drawable.l));
添加(新主题(R.drawable.m));
添加(新主题(R.drawable.n));
添加(新主题(R.drawable.o));
添加(新主题(R.drawable.p));
添加(新主题(R.drawable.q));
}
@凌驾
已加载的公共无效(){
}
@凌驾
公开无效,无效(无效){
}
@Override
public int getItemCount() {
return themesList.size() + mAdItems.size();
}
@Override
public int getItemViewType(int position) {
if (position % AD_DISPLAY_FREQUENCY == 0 && position != 0)
return AD_TYPE;
else
return POST_TYPE;
}
public static class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@BindView(R.id.img_theme)
ImageView imgTheme;
Unbinder unbinder;
IRecyclerClickListener listener;
public void setListener(IRecyclerClickListener listener) {
this.listener = listener;
}
public ImageViewHolder(@NonNull View itemView) {
super(itemView);
unbinder = ButterKnife.bind(this, itemView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
listener.onThemItemClick(v, getAdapterPosition());
}
}
public static class UnifiedFbHolder extends RecyclerView.ViewHolder {
public NativeAdLayout nativeAdLayout;
public MediaView mvAdMedia, ivAdIcon;
public TextView tvAdTitle, tvAdBody, tvAdSocialContext, tvAdSponsoredLable;
public Button btnCallToAction;
public LinearLayout adChoicesContainer;
public UnifiedFbHolder(NativeAdLayout nativeAdLayout) {
super(nativeAdLayout);
this.nativeAdLayout = nativeAdLayout;
mvAdMedia = nativeAdLayout.findViewById(R.id.native_ad_media);
tvAdTitle = nativeAdLayout.findViewById(R.id.native_ad_title);
tvAdBody = nativeAdLayout.findViewById(R.id.native_ad_body);
tvAdSocialContext = nativeAdLayout.findViewById(R.id.native_ad_social_context);
tvAdSponsoredLable = nativeAdLayout.findViewById(R.id.natibe_ad_sponsored_lable);
btnCallToAction = nativeAdLayout.findViewById(R.id.native_ad_call_to_action);
ivAdIcon = nativeAdLayout.findViewById(R.id.native_ad_icon);
adChoicesContainer = nativeAdLayout.findViewById(R.id.ad_choses_container);
}
}
private List<Themes> themesList;
NativeAdsManager nativeAdsManager;
private MyNativeAdapter myNativeAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_themes, null, false);
unbinder = ButterKnife.bind(this, root);
initViews();
return root;
}
private void initViews() {
themesList = new ArrayList<>();
addAdsToList();
//Add themes into the list
addThemesToList();
recyclerView.setHasFixedSize(true);
recyclerView.hasFixedSize();
recyclerView.setItemViewCacheSize(themesList.size());
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setItemAnimator(new DefaultItemAnimator());
//Set layout and orientation for recycler view
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, null, 0);
//Setting List to the Recycler View using adapter
// MyAdapter myAdapter = new MyAdapter(getContext(), themesList);
myNativeAdapter = new MyNativeAdapter(getContext(), themesList, nativeAdsManager);
/*FBNativeAdAdapter fbAdapter = FBNativeAdAdapter.Builder.with(getResources().getString(R.string.FbNativeAd), myAdapter)
.adItemIterval(3)
.build();*/
recyclerView.setAdapter(myNativeAdapter);
}
private void addAdsToList() {
nativeAdsManager = new NativeAdsManager(getContext(), getResources().getString(R.string.FbNativeAd), 2);
nativeAdsManager.loadAds();
nativeAdsManager.setListener(this);
}
private void addThemesToList() {
themesList.add(new Themes(R.drawable.a));
themesList.add(new Themes(R.drawable.b));
themesList.add(new Themes(R.drawable.c));
themesList.add(new Themes(R.drawable.d));
themesList.add(new Themes(R.drawable.e));
themesList.add(new Themes(R.drawable.f));
themesList.add(new Themes(R.drawable.g));
themesList.add(new Themes(R.drawable.h));
themesList.add(new Themes(R.drawable.i));
themesList.add(new Themes(R.drawable.j));
themesList.add(new Themes(R.drawable.k));
themesList.add(new Themes(R.drawable.l));
themesList.add(new Themes(R.drawable.m));
themesList.add(new Themes(R.drawable.n));
themesList.add(new Themes(R.drawable.o));
themesList.add(new Themes(R.drawable.p));
themesList.add(new Themes(R.drawable.q));
}
@Override
public void onAdsLoaded() {
}
@Override
public void onAdError(AdError adError) {
}