带有Recycler视图的Google AdMod原生广告

Tho*_*rya 4 admob android-recyclerview

我正在使用具有卡片视图的回收器视图编写Android App.我需要使用Google AdMod原生广告.

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

请帮我提供示例代码或指导我到正确的位置,我可以找到它.

kot*_*107 8

最近我坚持使用相同的问题,但我为ListView实施了AdMob原生广告.然后我决定将我的解决方案发布到admobadapter.希望它会对你有所帮助.我认为为RecyclerView/CardViews定制我的解决方案并不是很复杂.BTW随时可以贡献/分叉.

基本用法可能是这样的:

    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();
    }
Run Code Online (Sandbox Code Playgroud)

结果会像这样

更新:整合

您只需从github复制以下来源即可

admobadapter/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterWrapper.java
admobadapter/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobFetcher.java
Run Code Online (Sandbox Code Playgroud)

到您的javasources文件夹(随意编辑所有文件中的包名称,但请保留许可证标题).

和以下资源

admobadapter/admobadapter/src/main/res/layout/adcontentlistview_item.xml
admobadapter/admobadapter/src/main/res/layout/adinstalllistview_item.xml
Run Code Online (Sandbox Code Playgroud)

到你的res/layout文件夹.也请不要忘记将复制string的资源test_admob_unit_idadmobadapter/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) {
//...
}
Run Code Online (Sandbox Code Playgroud)

然后我想你必须AdmobAdapterWrapper'sRecyclerView.Adapter's特定的方法替换一些方法,我想它将足以替换getView(...)by onBindViewHolder(...),onCreateViewHolder(...)所以看来这部分工作取决于你:)这似乎不是很难.然后你可以RecyclerView在上面的例子中简单地使用它:

AdmobAdapterWrapper adapterWrapper = new AdmobAdapterWrapper(this); 
  adapterWrapper.setAdapter(your_recyclerview_adapter_that_will_be_filled_with_your_data); 
recyclerView.setAdapter(adapterWrapper);
Run Code Online (Sandbox Code Playgroud)

如果您将延伸AdmobAdapterWrapper,RecyclerView.Adapter请不要犹豫,为我的lib贡献/分叉.非常感谢!我RecyclerView.Adapter稍后会为我的lib 扩展包装器,但此刻我没有时间,对不起...... 你有可能在一段时间内使用ListView而不是RecyclerView吗?但是,如果您有任何疑问,请不要犹豫,问我.