Android 颤振:在ListView.builder中每N行添加一次广告
在Android Studio中,我使用RecyclerView.Adapter和RecyclerView.ViewHolder以及getItemViewType()来确定RecyclerView中一行的位置,并每隔10行发布一次广告。但在《颤振》中,我不知道如何做到这一点,在过去的两天里,我在网上四处寻找,找不到任何东西,或者可能我不知道《颤振》中它叫什么。我可能不需要一个代码示例,我需要一些关于如何实现这一点的指导,或者它被称为什么来浏览web。 提示:我正在使用ListView.builder从API接收数据并在ListView.builder中显示 编辑:我正在添加代码以使其更清晰:Android 颤振:在ListView.builder中每N行添加一次广告,android,flutter,Android,Flutter,在Android Studio中,我使用RecyclerView.Adapter和RecyclerView.ViewHolder以及getItemViewType()来确定RecyclerView中一行的位置,并每隔10行发布一次广告。但在《颤振》中,我不知道如何做到这一点,在过去的两天里,我在网上四处寻找,找不到任何东西,或者可能我不知道《颤振》中它叫什么。我可能不需要一个代码示例,我需要一些关于如何实现这一点的指导,或者它被称为什么来浏览web。 提示:我正在使用ListView.build
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
));
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Map data;
List recentNews;
var _textController = TextEditingController();
Future getRecent() async {
String url = 'https://example.com/api';
http.Response response = await http.get(url, headers: {HttpHeaders.authorizationHeader: ApiKey});
data = json.decode(response.body);
setState(() {
recentNews = data["data"];
});
}
@override
void initState() {
super.initState();
getRecent();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: DefaultTabController(
child: Scaffold(
appBar: new AppBar(
iconTheme: new IconThemeData(color: Colors.black),
backgroundColor: Colors.white,
title: Text("TEST", style: TextStyle(color: Colors.black),),
),
body:
ListView.builder(
itemCount: recentNews == null ? 0 : recentNews.length,
itemBuilder: (BuildContext context, int index) {
String serverTime = "${recentNews[index]["createdAt"]["date"]}";
DateTime time = DateTime.parse(serverTime);
String agoTime = timeago.format(time);
return Card(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: InkWell(
onTap: (){
var route = new MaterialPageRoute(
builder: (BuildContext context) =>
new WebView(slug: recentNews[index]["slug"], title: recentNews[index]["title"]),
);
Navigator.of(context).push(route);
},
child: Column(
children: <Widget>[
new Container(
child: new Image.network(recentNews[index]["image_url"])
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Text("${recentNews[index]["title"]}",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.w700,
),),
),
new Row(
children: <Widget>[
Text("${recentNews[index]["source"]["name"]}"),
Text(" - "),
Text(agoTime),
],
)
],
),
),
),
);
},
),
),
),
);
}
}
void main(){
runApp(材料应用程序)(
debugShowCheckedModeBanner:false,
主页:主页(),
));
}
类主页扩展了StatefulWidget{
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
地图数据;
列出最近的新闻;
var_textController=TextEditingController();
Future getRecent()异步{
字符串url='0https://example.com/api';
http.Response-Response=wait-http.get(url,头:{HttpHeaders.authorizationHeader:ApiKey});
data=json.decode(response.body);
设置状态(){
最近新闻=数据[“数据”];
});
}
@凌驾
void initState(){
super.initState();
getRecent();
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
主页:DefaultTabController(
孩子:脚手架(
appBar:新的appBar(
iconTheme:new IconThemeData(颜色:Colors.black),
背景颜色:Colors.white,
标题:文本(“测试”,样式:TextStyle(颜色:Colors.black),),
),
正文:
ListView.builder(
itemCount:recentNews==null?0:recentNews.length,
itemBuilder:(构建上下文,int索引){
字符串serverTime=“${recentNews[index][“createdAt”][“date”]}”;
DateTime=DateTime.parse(serverTime);
字符串agoTime=timeago.format(时间);
回程卡(
孩子:填充(
填充:常数边集全部(10.0),
孩子:InkWell(
onTap:(){
var路线=新材料路线(
生成器:(BuildContext上下文)=>
新的网络视图(slug:recentNews[index][“slug”],title:recentNews[index][“title”]),
);
导航器.of(上下文).push(路由);
},
子:列(
儿童:[
新容器(
子:新建图像。网络(最新新闻[索引][“图像\u url”])
),
填充物(
填充:常数边集全部(10.0),
子项:文本(${recentNews[index][“title”]}),
样式:TextStyle(
字体大小:20.0,
fontWeight:fontWeight.w700,
),),
),
新行(
儿童:[
文本(${recentNews[index][“source”][“name”]}”),
正文(“-”,
文本(时间),
],
)
],
),
),
),
);
},
),
),
),
);
}
}
试试ListView.separated()
在separated()构造函数中,生成一个列表,可以指定每个项之间的分隔符
ListView.com(
itemBuilder:(上下文、位置){
返回ListItem();
},
分离器生成器:(上下文、位置){
返回分隔项();
},
itemCount:itemCount,//第n项作为广告。
),
这种类型的列表允许您动态定义分隔符,为不同类型的项目使用不同类型的分隔符,在需要时添加或删除分隔符,等等
此实现还可以用于插入其他类型的元素(例如广告),而不需要对列表项中间的主列表进行任何修改。
< P> <强>尝试ListVIEW .SealDead()< <强> > /P> 在separated()构造函数中,生成一个列表,可以指定每个项之间的分隔符祝你好运。
itemBuilder
获取int index
-当索引%10==9
和你的“正常”项目时,只需使用它返回你的广告即可otherwise@pskink很抱歉,我尽了最大努力,但似乎我不确定在我的代码中在哪里实现它。你能告诉我怎么做吗。提前感谢您:itemBuilder:
-只需使用索引
,索引0-8返回数据,索引9返回第一个广告,索引10-18返回另一个数据,索引19返回第二个广告etcseeListView.builder(itemBuilder:(BuildContext context,int index){if(index%10!=9){var-dataIndex=index-index~/10;返回文本('data#$dataIndex');}var-advertedindex=index~/10;返回容器(color:Colors.red,child:Text('advert#$advertedindex'),);},itemCount:100,)
-你所要做的就是使用你的数据hereDear@pskink我知道这是一个很长的问题,但是我从第一天开始就遇到了一个问题,似乎我无法通过它。这个解决方案覆盖了原始数据,所以如果列表是十个项目+广告,它只会显示十个项目以及它在广告位置的项目精液会丢失。所以我问有没有解决办法?谢谢你
ListView.separated(
itemBuilder: (context, position) {
return ListItem();
},
separatorBuilder: (context, position) {
return Container(child: (index != 0 && index % 5 == 0) ?
AdmobBanner(adUnitId: getBannerAdUnitId(), adSize:
AdmobBannerSize.BANNER, listener: (AdmobAdEvent event, Map args)
{ //handleEvent(event, args, 'Banner'); },
) : Divider( height: 2.0, ));
}, itemCount: itemCount, //nth item as ads.
)