Admob 谷歌AdMod原生广告与回收视图

Admob 谷歌AdMod原生广告与回收视图,admob,android-recyclerview,Admob,Android Recyclerview,我正在用回收器视图和卡片视图编写Android应用程序。 我需要使用谷歌AdMod原生广告 我试着搜索示例和谷歌开发者网络,但没有找到具体的解决方案 请用示例代码帮助我,或者将我带到适当的位置,在那里我可以找到这些内容。最近我遇到了同样的问题,但我为ListView实现了AdMob本机广告。然后我决定将我的解决方案发布到。希望它能帮助你。我想定制针对RecyclerView/CardView的解决方案并不复杂。顺便说一句,您可以随意贡献/分叉 基本用法可能如下所示: ListView l

我正在用回收器视图和卡片视图编写Android应用程序。 我需要使用谷歌AdMod原生广告

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


请用示例代码帮助我,或者将我带到适当的位置,在那里我可以找到这些内容。

最近我遇到了同样的问题,但我为ListView实现了AdMob本机广告。然后我决定将我的解决方案发布到。希望它能帮助你。我想定制针对RecyclerView/CardView的解决方案并不复杂。顺便说一句,您可以随意贡献/分叉

基本用法可能如下所示:

    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();
    }
转到
java
sources文件夹(可以自由编辑所有文件中的包名,但请保留许可证标题不变)

以及下列资源

admobadapter/admobadapter/src/main/res/layout/adcontentlistview_item.xml
admobadapter/admobadapter/src/main/res/layout/adinstalllistview_item.xml
转到您的
res/layout
文件夹。另外,请不要忘记将
string
资源
test\u admob\u unit\u 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) {
//...
}
如果要为
RecyclerView.Adapter
扩展
AdmobAdapterWrapper
,请毫不犹豫地为我的库贡献/fork。非常感谢!
稍后我将为我的lib扩展
RecyclerView.Adapter
包装器,但我现在没有时间,很抱歉…您有可能在一段时间内使用ListView而不是RecyclerView吗?但是如果您有问题,请随时问我。

谢谢,但我主要是在寻找RecyclerView,而不使用其他SDK作为包装器。没问题。顺便说一句,这里没有任何“SDK”或类似的东西,只有一个适配器(AdmobAdapterWrapper),您可以获得它并与您的RecyclerView一起使用(需要进行一点定制)。谢谢。我在哪里可以找到这个包装纸?我无法导入此类-“AdmobAdapterWrapper”。请帮帮我。@Nguyenminhinh Hi!我要实施它soon@binuj您好,请看一下这里描述的第二个问题的答案。这就是你要找的东西吗?请为RecyclerView编辑该片段。
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 adapterWrapper = new AdmobAdapterWrapper(this); 
  adapterWrapper.setAdapter(your_recyclerview_adapter_that_will_be_filled_with_your_data); 
recyclerView.setAdapter(adapterWrapper);