-1

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 Answers1

1

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().

ADM
  • 20,406
  • 11
  • 52
  • 83
  • It gives me error public int getItemViewType(int position) { public int getItemCount() { return mlistItems.size(); } if (position!=0 && position%4 == 0) { return AD_TYPE; } return CONTENT_TYPE; } Would be great you can share the example here – Rohit Kumar Sehrawat Sep 28 '20 at 05:49
  • is it complete now @ADM? If we remove everything from onBindViewHolder and onCreateViewHolder. How it will fetch the id ads – Rohit Kumar Sehrawat Sep 28 '20 at 06:49
  • @RohitKumarSehrawat [Here](https://stackoverflow.com/a/38027391/4168607) is way i am talking about. And [Here](https://stackoverflow.com/a/40608363/4168607) is the other way where you need to calculate the indexes. – ADM Sep 28 '20 at 06:51
  • It look too confusing to me. – Rohit Kumar Sehrawat Sep 28 '20 at 06:53