[英]Why Native Advanced ads removed the item from recycler view android?
I am using this code in my adapter我在我的适配器中使用此代码
How to place Admob Native Advanced Ads in recycler view android? 如何在回收站视图 android 中放置 Admob Native Advanced Ads?
This problem occur出现这个问题
But i want result like this但我想要这样的结果
Addition to my code, I am using this formula to place the item in recycler view.除了我的代码之外,我还使用此公式将项目放置在回收站视图中。
@Override
public int getItemViewType(int position) {
if (position!=0 && position%4 == 0) {
return AD_TYPE;
}
return CONTENT_TYPE;
}
@Override
public int getItemCount() {
return mlistItems.size();
}
Complete adapter code完整的适配器代码
public class CenterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private RecyclerItemClickListener listener;
private List<list_item_center> mlistItems;
private Context mcontext;
private static final int AD_TYPE = 2;
private static final int CONTENT_TYPE = 1;
public CenterAdapter(List<list_item_center> listItems, Context context, RecyclerItemClickListener listener) {
mlistItems = listItems;
mcontext = context;
this.listener = listener;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemViewType(int position) {
if (position != 0 && position % 4 == 0) {
return AD_TYPE;
}
return CONTENT_TYPE;
}
@Override
public int getItemCount() {
return mlistItems.size();
}
} }
Its because of getItemCount()
you are still returning the size of same ArrayList
.由于getItemCount()
您仍然返回相同ArrayList
的大小。 Thats why the item gets override by the Ads layoutType
.这就是该项目被 Ads layoutType
覆盖的layoutType
。 A better way to do this is to insert the Ad Item in the list before hand With condition position!=0 && position%4 == 0
.一个更好的方法是预先在列表中插入 Ad Item with condition position!=0 && position%4 == 0
。
This way you do not have to manage or calculate the item position in list inside onBindViewHolder
.这样您就不必在onBindViewHolder
内管理或计算列表中的项目位置。 Also you do not have to calculate the itemCount
inside getItemCount()
.此外,您不必在getItemCount()
计算itemCount
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.