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