且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在列表视图中添加原生广告?

更新时间:2023-12-05 17:42:04

listView BaseAdapter首先调用方法getCount()检索要显示的行数,然后针对每行调用getView(int position, View convertView, ViewGroup parent)方法以创建并返回给定位置的View对象.

A listView BaseAdapter first calls the method getCount() to retrieve the number of rows to show and then for each row it calls the getView(int position, View convertView, ViewGroup parent) method to create and return a View object for the given position.

方法是扩展BaseAdapter并创建自己的自定义适配器,而不是使用默认的ArrayAdapter.然后,当您要展示广告时,您需要返回一个广告"视图,而不是通常的普通视图.这意味着您需要覆盖getCount()以返回更多行(例如,如果您有10行,则意味着您需要返回11 = 10个实际内容+ 1个广告)

The approach is to extend BaseAdapter and create your own custom Adapter, instead of using the default ArrayAdapter. Then you need to return an "Ad" View instead of your usual normal View when you want to display an ad. This means that you need to override getCount() to return more rows (for example if you have 10 rows, that means you need to return 11 = 10 actual content + 1 ad)

然后您需要确定创建此视图的位置,我认为您可以通过简单地检查position变量来做到这一点:

Then you need to decide in which position to create this View, I think you can do it by simply checking the position variable:

if (position == VALUE) {
   // Create and return Ad View 
} else {
   // Create and return a normal View 
}

无论如何,这整个过程确实很棘手,因为事情很容易失控(位置与视图不匹配等). StartApp应该能够控制您的listView适配器为您完成所有这些操作.我的猜测是您的适配器无法与StartApp正确通信(也许您未正确初始化?).

Anyway, this whole thing is really tricky as things can go easily out of hand (positions mismatching with Views etc). StartApp should be able to control your listView adapter to do all this for you. My guess is that your adapter is not communicating properly with StartApp (maybe you are not initialising correctly?).

尝试深入阅读文档或从中查找示例.如果您不知道,可以使用其他替代方法,例如Avocarrot,Namomedia,Inmobi等

Try to dig into the documentation or find an example by them. If you can’t figure it out, there are other alternatives you can use such as Avocarrot, Namomedia, Inmobi, etc

我使用了 Avocarrot ,该代码在github中有一个开放源代码示例,可用于插入listViews中的广告. 您可以运行它并在适合时使用它: https://github.com/Avocarrot/android -demo-app

I have used Avocarrot which has an open source example in github for inserting ads in listViews. You can run it and use it if it fits you: https://github.com/Avocarrot/android-demo-app