Сначала вы должны сделать json Array с рекламными данными, включающими
Например: json Array без рекламы
{"s": true, "d": [{"id ":" 203e63d7-3672-4fb7-a899-3ebd281a0e4c "," title ":" Title 1 "," description ":" Description "}, {" id ":" 203e63d7-3672-4fb7-a899-3ebd281a0e4c ","title": "Title 1", "description": "Description"}, {"id": "203e63d7-3672-4fb7-a899-3ebd281a0e4c", "title": "Title 1", "description": "Description"}, {" id ":" 203e63d7-3672-4fb7-a899-3ebd281a0e4c "," title ":" Title 1 "," description ":" Description "}, {" id ":" 203e63d7-3672-4fb7-a899-3ebd281a0e4c "," title ":" Title 1 "," description ":" Description "}]}
теперь создайте пользовательский класс с таким параметром, как title, type, description, advertise_titleand 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;
}