gpt4 book ai didi

admob - 带有 Recyclerview 的 Google AdMob 原生广告

转载 作者:行者123 更新时间:2023-12-04 23:19:06 26 4
gpt4 key购买 nike

我正在使用带有卡片 View 的回收器 View 编写 Android 应用程序。
我需要使用 Google AdMod 原生广告。

我也尝试搜索示例和谷歌开发者网络,但没有得到具体的解决方案。

请帮助我提供示例代码或将我引导到可以找到它的正确位置。

最佳答案

最近我遇到了同样的问题,但我为 ListView 实现了 AdMob 原生广告。然后我决定将我的解决方案发布到 admobadapter .希望它会帮助你。我想为 RecyclerView/CardViews 定制我的解决方案并不是很复杂。顺便说一句,请随意贡献/ fork 。

基本用法可能看起来像:

    ListView lvMessages;
AdmobAdapterWrapper adapterWrapper;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initListViewItems();
}

/**
* Inits an adapter with items, wrapping your adapter with a {@link AdmobAdapterWrapper} and setting the listview to this wrapper
* FIRST OF ALL Please notice that the following code will work on a real devices but emulator!
*/
private void initListViewItems() {
lvMessages = (ListView) findViewById(R.id.lvMessages);

//creating your adapter, it could be a custom adapter as well
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);

adapterWrapper = new AdmobAdapterWrapper(this);
adapterWrapper.setAdapter(adapter); //wrapping your adapter with a AdmobAdapterWrapper.
//here you can use the following string to set your custom layouts for a different types of native ads
//adapterWrapper.setInstallAdsLayoutId(R.layout.your_installad_layout);
//adapterWrapper.setcontentAdsLayoutId(R.layout.your_installad_layout);

//Sets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
adapterWrapper.setLimitOfAds(3);

//Sets the number of your data items between ad blocks, by default it equals to 10.
//You should set it according to the Admob's policies and rules which says not to
//display more than one ad block at the visible part of the screen,
// so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
adapterWrapper.setNoOfDataBetweenAds(10);

//It's a test admob ID. Please replace it with a real one only when you will be ready to deploy your product to the Release!
//Otherwise your Admob account could be banned
//String admobUnitId = getResources().getString(R.string.banner_admob_unit_id);
//adapterWrapper.setAdmobReleaseUnitId(admobUnitId);

lvMessages.setAdapter(adapterWrapper); // setting an AdmobAdapterWrapper to a ListView

//preparing the collection of data
final String sItem = "item #";
ArrayList<String> lst = new ArrayList<String>(100);
for(int i=1;i<=100;i++)
lst.add(sItem.concat(Integer.toString(i)));

//adding a collection of data to your adapter and rising the data set changed event
adapter.addAll(lst);
adapter.notifyDataSetChanged();
}

结果 看起来像 this

更新:集成

您可以简单地从 github 复制以下源代码
admobadapter/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterWrapper.java
admobadapter/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobFetcher.java

给您的 java源文件夹(可以随意编辑所有文件中的包名称,但请保持许可证标题不变)。

以及以下资源
admobadapter/admobadapter/src/main/res/layout/adcontentlistview_item.xml
admobadapter/admobadapter/src/main/res/layout/adinstalllistview_item.xml

给您的 res/layout文件夹。另外请不要忘记复制 string资源 test_admob_unit_id来自 admobadapter/admobadapter/src/main/res/values/strings.xml给您的 strings.xml文件。

实现您必须更改 AdmobAdapterWrapper 声明的所有步骤至少像这样:
public class AdmobAdapterWrapper extends RecyclerView.Adapter<your ContactViewHolder class> implements AdmobFetcher.AdmobListener {

//...
private RecyclerView.Adapter<your ContactViewHolder class> mAdapter;

public RecyclerView.Adapter<your ContactViewHolder class> getAdapter() {
return mAdapter;
}

public void setAdapter(RecyclerView.Adapter<your ContactViewHolder class> adapter) {
//...
}

那么我想你必须更换一些 AdmobAdapterWrapper's方法来自 RecyclerView.Adapter's具体方法,我想换成 getView(...)就够了来自 onBindViewHolder(...)onCreateViewHolder(...)所以这部分工作似乎取决于你:) 这似乎并不难。然后您可以简单地将它与您的 RecyclerView 一起使用。就像上面的例子:
AdmobAdapterWrapper adapterWrapper = new AdmobAdapterWrapper(this); 
adapterWrapper.setAdapter(your_recyclerview_adapter_that_will_be_filled_with_your_data);
recyclerView.setAdapter(adapterWrapper);

如果您要延长 AdmobAdapterWrapperRecyclerView.Adapter请不要犹豫为我的库贡献/ fork 。将不胜感激!
我要延长 RecyclerView.Adapter稍后我的库的包装器,但我现在没有时间,抱歉... 您是否有可能在一段时间内使用 ListView 而不是 RecyclerView? 但是,如果您有问题,请不要犹豫,问我。

关于admob - 带有 Recyclerview 的 Google AdMob 原生广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32551649/

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