Why Native Advanced ads removed the item from recycler view android?

532 Views Asked by At

I am using this code in my adapter

How to place Admob Native Advanced Ads in recycler view android?

This problem occur

enter image description here

But i want result like this

enter image description here

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();
}

}

1

There are 1 best solutions below

4
On

Its because of getItemCount() you are still returning the size of same ArrayList. Thats why the item gets override by the Ads 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 .

This way you do not have to manage or calculate the item position in list inside onBindViewHolder . Also you do not have to calculate the itemCount inside getItemCount().