1

I'm having this problem when ever more data is added to the recycleview the total number of of item to be be displayed reduces by 1 or 2 depending on the number of items in the recycleview or it will return full size sometimes

Here is my code

private static final int MENU_ITEM_VIEW_TYPE= 0;
// The Native Express ad view type.
private static final int NATIVE_AD_VIEW_TYPE = 1;
private static final int ITEM_FEED_COUNT = 6;
private final Activity activity;
Keystore stopAdsPref;
Boolean isStopAd;

public AdapterPosts(Context context, Activity activity, List<ModelPost> postList) {
    this.context = context;
    this.postList = postList;
    this.activity = activity;

    myUid = Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();
    likeRef = FirebaseDatabase.getInstance().getReference().child("Likes");
    postsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
    stopAdsPref = Keystore.getInstance(context);
    isStopAd = stopAdsPref.getBoolean(STOP_ADS);
}

public void setItems(ArrayList<ModelPost> emp) {
    postList.addAll(emp);
}

@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    if (viewType == MENU_ITEM_VIEW_TYPE) {
        View view = LayoutInflater.from(context).inflate(R.layout.row_posts, parent, false);
        return new AdapterPosts.MyHolder(view);

    } else if (viewType == NATIVE_AD_VIEW_TYPE) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_ad, parent, false);

        return new AdapterPosts.MyHolder(view);
    } else {
        return null;
    }

}

I think the problem is after int pos = position - Math.round(position / ITEM_FEED_COUNT);

@Override
public void onBindViewHolder(@NonNull 
MyHolder holder, int position) {
    Log.e(TAG, "onBindViewHolder: step 1 " + position);
    if (holder.getItemViewType() == MENU_ITEM_VIEW_TYPE) {
        int pos = position - Math.round(position / ITEM_FEED_COUNT);
        Log.e(TAG, "onBindViewHolder: step 2: " + pos);

        ((AdapterPosts.MyHolder) holder).bindData(postList.get(pos), pos, holder);

    } else if (holder.getItemViewType() == NATIVE_AD_VIEW_TYPE) {
        if (isStopAd.equals(true)) {
            Log.e(TAG, "AD stop");
        } else {
            ((AdapterPosts.MyHolder) holder).bindAdData();
        }
    }

    requestQueue = Volley.newRequestQueue(context);
}

@Override
public int getItemCount() {

    if (postList.size() > 0) {
        return postList.size() + Math.round(postList.size() / ITEM_FEED_COUNT);
    }
    return postList.size();
}

@Override
public int getItemViewType(int position{

    if ((position + 1) % ITEM_FEED_COUNT== 0) {
        return NATIVE_AD_VIEW_TYPE;
    }
    return MENU_ITEM_VIEW_TYPE;
}
Evans Drah
  • 41
  • 6

1 Answers1

0

I was able to solve this it might be helpful to someone in the future code below Answer from here

 @Override
 public int getItemCount() {
int additionalContent = 0;
if (data.size() > 0 && LIST_AD_DELTA > 0 && data.size() > LIST_AD_DELTA) {
    additionalContent = data.size() / LIST_AD_DELTA;
}
return data.size() + additionalContent;

}

@Override
public void onBindViewHolder(BaseRecyclerHolder baseHolder, int position) {
if (getItemViewType(position) == CONTENT) {
    ContentRecyclerHolder holder = (ContentRecyclerHolder) baseHolder;
    Content content = data.get(getRealPosition(position));
} else {
    AdRecyclerHolder holder = (AdRecyclerHolder) baseHolder;
        AdRequest adRequest = new AdRequest.Builder().build();
        if (adRequest != null && holder.adView != null){
            holder.adView.loadAd(adRequest);
         }
  }
}
Evans Drah
  • 41
  • 6