我需要在垂直寻呼机的页面之间显示广告。 例如,新闻应用程序(如短裤)。 任何方式都可以做到。
用于新闻页面的数据是标题,内容和日期,为此我将在jsonarray中获取多个新闻。我的要求是在寻呼机适配器内添加其他页面(广告页面)。例如:滑动5次新闻后。我应该获得与新闻页面不同的广告页面
答案 0 :(得分:1)
首先,您应该使用包含广告数据的json数组
例如:不做广告的json数组
{ “ s”:是的, “ d”:[ { “ id”:“ 203e63d7-3672-4fb7-a899-3ebd281a0e4c”, “ title”:“标题1”, “ description”:“ Description” }, { “ id”:“ 203e63d7-3672-4fb7-a899-3ebd281a0e4c”, “ title”:“标题1”, “ description”:“ Description” }, { “ id”:“ 203e63d7-3672-4fb7-a899-3ebd281a0e4c”, “ title”:“标题1”, “ description”:“ Description” }, { “ id”:“ 203e63d7-3672-4fb7-a899-3ebd281a0e4c”, “ title”:“标题1”, “ description”:“ Description” }, { “ id”:“ 203e63d7-3672-4fb7-a899-3ebd281a0e4c”, “ title”:“标题1”, “ description”:“ Description” } ] }
现在使用以下参数创建自定义类:标题,类型,描述,advertise_title和advertise_url
List<CustomObj> list=new ArrayList<>();
for(int i=0;i<jsonArray.size,i++){
int j=i;
j++;
if(j%5==0){
list.add(new CustomObj(jsonArray.get(i).getId(),jsonArray.get(i).getTitle(),jsonArray.get(i).getDescription(),1,"Advertise Title","URL")
}else{
list.add(new CustomObj(jsonArray.get(i).getId(),jsonArray.get(i).getTitle,jsonArray.get(i).getDescription,0,"","")
}
}
带有广告列表的json数组
{
"s": true,
"d": [
{
"id": "203e63d7-3672-4fb7-a899-3ebd281a0e4c",
"title": "Title 1",
"type": 0,
"description": "Description",
"advertise_title": "",
"advertise_img_url": ""
},
{
"id": "203e63d7-3672-4fb7-a899-3ebd281a0e4c",
"title": "Title 1",
"type": 0,
"description": "Description",
"advertise_title": "",
"advertise_img_url": ""
},
{
"id": "203e63d7-3672-4fb7-a899-3ebd281a0e4c",
"title": "Title 1",
"type": 0,
"description": "Description",
"advertise_title": "Description",
"advertise_img_url": "https://www.pexels.com/photo/nature-red-love-romantic-67636/a.jpg"
},
{
"id": "203e63d7-3672-4fb7-a899-3ebd281a0e4c",
"title": "Title 1",
"type": 0,
"description": "Description",
"advertise_title": "Description",
"advertise_img_url": "https://www.pexels.com/photo/nature-red-love-romantic-67636/a.jpg"
},
{
"id": "203e63d7-3672-4fb7-a899-3ebd281a0e4c",
"title": "Title 1",
"type": 1,
"description": "Description",
"advertise_title": "Description",
"advertise_img_url": "https://www.pexels.com/photo/nature-red-love-romantic-67636/a.jpg"
},
{
"id": "203e63d7-3672-4fb7-a899-3ebd281a0e4c",
"title": "Title 1",
"type": 0,
"description": "Description",
"advertise_title": "",
"advertise_img_url": ""
},
{
"id": "203e63d7-3672-4fb7-a899-3ebd281a0e4c",
"title": "Title 1",
"type": 0,
"description": "Description",
"advertise_title": "",
"advertise_img_url": ""
},
{
"id": "203e63d7-3672-4fb7-a899-3ebd281a0e4c",
"title": "Title 1",
"type": 0,
"description": "Description",
"advertise_title": "",
"advertise_img_url": ""
},
{
"id": "203e63d7-3672-4fb7-a899-3ebd281a0e4c",
"title": "Title 1",
"type": 0,
"description": "Description",
"advertise_title": "",
"advertise_img_url": ""
}
]
}
在ViewPagerAdapter中,
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView;
if (mList.get(position).getType() == 0) {
//for simple item
itemView = mLayoutInflater.inflate(R.layout.pager_news, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
TextView text = (TextView) itemView.findViewById(R.id.text_id);
imageView.setImageResource(mResources[position].getUrl());
text.setText(mResources[position].getTitle());
container.addView(itemView);
} else {
//for adavertise item
itemView = mLayoutInflater.inflate(R.layout.pager_advertise, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
}
return itemView;
}
答案 1 :(得分:0)
您可以只创建两种类型的片段,一种是您的主要片段,例如新闻片段,另一个是将显示广告的广告片段。
然后根据您的逻辑将这些片段添加到列表中,然后再将其传递给pageradapter。
private ArrayList<Fragment> fragmentList = new ArrayList<>();
fragmentList.add(NewsFragment.newInstance());
fragmentList.add(NewsFragment.newInstance());
fragmentList.add(NewsFragment.newInstance());
fragmentList.add(NewsFragment.newInstance());
fragmentList.add(NewsFragment.newInstance());
fragmentList.add(AdFragment.newInstance());
NewsPagerAdapter newsPagerdapter = new NewsPagerAdapter(getSupportFragmentManager(), fragmentList);
viewPager.setAdapter(newsPagerdapter);
您的适配器将像这样-
public class NewsPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> fragmentList = new ArrayList<>();
public NewsPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragmentList) {
super(fm);
this.fragmentList=fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}