I am using this code in my adapter
How to place Admob Native Advanced Ads in recycler view android?
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 sameArrayList
. Thats why the item gets override by the AdslayoutType
. A better way to do this is to insert the Ad Item in the list before hand With conditionposition!=0 && position%4 == 0
.This way you do not have to manage or calculate the item position in list inside
onBindViewHolder
. Also you do not have to calculate theitemCount
insidegetItemCount()
.