Я пытаюсь использовать нативную рекламу в фейсбуке.Но Facebook заставляет использовать их метод, так как обычные родные загружаются только один раз.Поэтому мне нужно перейти к коду, предоставленному Facebook, но я не хочу показывать первую позицию в обзоре реселлера в качестве нативной рекламы в сети аудитории Facebook.Как я могу решить это в нижеприведенном коде?Спасибо.
В моем классе адаптера public public getItemViewType (int position) я знаю, что мне нужно внести некоторые изменения, но я не могу этого сделать.
Активность как NativeAdRecyclerActivity
public class NativeAdRecyclerActivity extends AppCompatActivity implements NativeAdsManager.Listener {
private ArrayList<RecyclerPostItem> mPostItemList;
private NativeAdsManager mNativeAdsManager;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_native_ad_recycler);
// Create some dummy post items
mPostItemList = new ArrayList<>();
for (int i = 1; i <= 1000; i++) {
mPostItemList.add(new RecyclerPostItem("RecyclerView Item #" + i));
}
String placement_id = "VID_HD_16_9_46S_APP_INSTALL#YOUR_PLACEMENT_ID";
mNativeAdsManager = new NativeAdsManager(this, placement_id, 5);
mNativeAdsManager.loadAds();
mNativeAdsManager.setListener(this);
// Inflate the layout for this fragment
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
}
@Override
public void onAdsLoaded() {
if (this == null) {
return;
}
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this,
DividerItemDecoration.VERTICAL);
mRecyclerView.addItemDecoration(itemDecoration);
NativeAdRecyclerAdapter adapter = new NativeAdRecyclerAdapter(this, mPostItemList,
mNativeAdsManager);
mRecyclerView.setAdapter(adapter);
}
@Override
public void onAdError(AdError error) {
}
}
Адаптер как NativeAdRecyclerAdapter
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);
}
}
}