gpt4 book ai didi

java - 当通过 getItemViewType 将项目添加到每 5 个备用位置时,删除 recyclerview 中作为 Facebook Native 广告的第一个项目位置

转载 作者:行者123 更新时间:2023-12-02 06:21:49 32 4
gpt4 key购买 nike

我正在尝试在 recyclerview 中使用 Facebook 原生广告。但 Facebook 强制使用他们的方法,因为普通的原生只加载一次。所以我必须转向 facebook 提供的代码,但我不想将 recyclerview 的第一个位置显示为 Facebook 受众网络原生广告。我该如何在下面的工作代码中解决这个问题?谢谢。

在 public int getItemViewType(intposition) 的适配器类中,我知道我必须做一些更改,但无法这样做。

作为 NativeAdRecyclerActivity 的 Activity

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);
}
}
}

最佳答案

将 getItemViewType 更改如下,以忽略位置 0 作为广告

@Override
public int getItemViewType(int position) {
if (position == 0) {
return POST_TYPE;
} else {
return position % AD_DISPLAY_FREQUENCY == 0 ? AD_TYPE : POST_TYPE;
}
}

关于java - 当通过 getItemViewType 将项目添加到每 5 个备用位置时,删除 recyclerview 中作为 Facebook Native 广告的第一个项目位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55829468/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com