我有一个适配器,负责从我的本地数据库中读取并显示一些卡片视图,昨天我在组合中添加了另一个视图来处理admob广告。但在其中添加了添加。它会跳过我的数据库条目之一。
所以我做的就是这个
int TYPE_POST = 0;
int TYPE_AD = 1;
@Override
public int getItemViewType(int position) {
if ((position % 5) == 0) {
return TYPE_AD;
}
return TYPE_POST;
}
,如果我将此final dbFeed dbfeed = LemesosAdapter.get(position);
更改为final dbFeed dbfeed = LemesosAdapter.get(position+1);
,那么我将拥有此onBindViewHolder,第一条帖子不会被跳过。但是第二次添加广告将被跳过。
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == TYPE_POST) {
// - get element from dataset at this position
// - replace the contents of the view with that element
PostViewHolder postViewHolder = (PostViewHolder)holder;
final dbFeed dbfeed = LemesosAdapter.get(position);
//Animation by yoyo library
YoYo.with(Techniques.BounceIn).playOn(postViewHolder.cardView);
String description;
description = Html.fromHtml(dbfeed.getDescription()).toString().replaceAll("\n", "").trim().substring(0, 100).replace((char) 65532, (char) 32).trim() + "...";
postViewHolder.Title.setText(dbfeed.getTitle());
postViewHolder.Description.setText(description);
postViewHolder.Date.setText(dbfeed.getPubDate());
Picasso.with(context).load(dbfeed.getThumbnailUrl()).placeholder(R.drawable.ic_image).into(postViewHolder.Thumbnail);
postViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, NewsDetails.class);
intent.putExtra("ThumbNail", dbfeed.getThumbnailUrl());
intent.putExtra("Link", dbfeed.getThumbnailUrl());
intent.putExtra("Title", dbfeed.getTitle());
intent.putExtra("Description", dbfeed.getDescription());
intent.putExtra("Date", dbfeed.getPubDate());
context.startActivity(intent);
}
});
} else if (getItemViewType(position) == TYPE_AD ) {
}
}
最后是我的getItemCount。我认为在这里解决方案就在这里。我在另一篇文章的这一章中看到,我应该使用与我的开始模一样的模态((itemcount%5)+ itemcount),但它不起作用
@Override
public int getItemCount() {
return ((LemesosAdapter.size()%5))+LemesosAdapter.size() ;
}
}
我在这里想念什么?
答案 0 :(得分:1)
如我所见,每次展示广告都会跳过您的一项,因为帖子和广告都共享相同的位置。您可以在这些if-else条件之外通过Log进行检查。
if (getItemViewType(position) == TYPE_POST) {
// - get element from dataset at this position
// - replace the contents of the view with that element
PostViewHolder postViewHolder = (PostViewHolder)holder;
final dbFeed dbfeed = LemesosAdapter.get(position - count);
//Animation by yoyo library
YoYo.with(Techniques.BounceIn).playOn(postViewHolder.cardView);
String description;
description = Html.fromHtml(dbfeed.getDescription()).toString().replaceAll("\n", "").trim().substring(0, 100).replace((char) 65532, (char) 32).trim() + "...";
postViewHolder.Title.setText(dbfeed.getTitle());
postViewHolder.Description.setText(description);
postViewHolder.Date.setText(dbfeed.getPubDate());
Picasso.with(context).load(dbfeed.getThumbnailUrl()).placeholder(R.drawable.ic_image).into(postViewHolder.Thumbnail);
postViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, NewsDetails.class);
intent.putExtra("ThumbNail", dbfeed.getThumbnailUrl());
intent.putExtra("Link", dbfeed.getThumbnailUrl());
intent.putExtra("Title", dbfeed.getTitle());
intent.putExtra("Description", dbfeed.getDescription());
intent.putExtra("Date", dbfeed.getPubDate());
context.startActivity(intent);
}
});
} else if (getItemViewType(position) == TYPE_AD ) {
count += 1
}