Inserting items manually when using FirestorePagingAdapter

Viewed 158

I am using FirestorePagingAdapter to list images from Firestore Database. I want to insert native Ad objects into the list before setting it to Recycler view so that Ads will be placed in between images. I already have the code to create native ads and insert them into the list. But since I am using FirestorePagingAdapter, how can I modify the list before setting it to RecyclerView.

    Query query = db.collection("Wallpapers")
                        .whereEqualTo("category", mCategoryName).orderBy("uploadedDate", Query.Direction.DESCENDING);

        PagedList.Config config = new PagedList.Config.Builder()
                        .setEnablePlaceholders(false)
                        .setPrefetchDistance(2)
                        .setPageSize(10)
                        .build();


                FirestorePagingOptions<WallpaperResponse> firestorePagingOptions = new FirestorePagingOptions.Builder<WallpaperResponse>()
                        .setLifecycleOwner(this)
                        .setQuery(query, config, WallpaperResponse.class)
                        .build();

                firestorePagingAdapter = new FirestorePagingAdapter<WallpaperResponse, WallpaperViewHolder>(firestorePagingOptions){

                            @NonNull
                            @Override
                            public WallpaperViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
                                switch (viewType) {
                               /* case NATIVE_AD_VIEW_TYPE:
                                    View unifiedNativeLayoutView = LayoutInflater.from(
                                            viewGroup.getContext()).inflate(R.layout.native_ad,
                                            viewGroup, false);
                                    return new NativeAdViewHolder(unifiedNativeLayoutView);*/
                                    case WALLPAPER_ITEM_VIEW_TYPE:
                                        // Fall through.
                                    default:
                                        View menuItemLayoutView = LayoutInflater.from(viewGroup.getContext())
                                                .inflate(R.layout.fragment_wallpapers_item_grid, viewGroup, false);
                                        return new WallpaperViewHolder(menuItemLayoutView);
                                }
                            }

                            @Override
                            protected void onBindViewHolder(@NonNull WallpaperViewHolder holder, int position, @NonNull WallpaperResponse model) {
                                loadingView.setVisibility(View.GONE);
                                int viewType = getItemViewType(position);
                                switch (viewType) {
                            /*case NATIVE_AD_VIEW_TYPE:
                                UnifiedNativeAd unifiedNativeAd = (UnifiedNativeAd) mWallpapers.get(position);
                                populateNativeAdView(unifiedNativeAd, ((NativeAdViewHolder) holder).getAdView());
                                break;*/
                                    case WALLPAPER_ITEM_VIEW_TYPE:
                                        // fall through
                                    default:
                                        holder.favAnimation.setVisibility(View.GONE);
                                        holder.mode.setVisibility(View.GONE);
                                        holder.author.setVisibility(View.GONE);
                                        WallpaperHelper wallpaperHelper = new WallpaperHelper();
                               ImageLoader.getInstance().displayImage(model.getThumbUrl(), new ImageViewAware(holder.image),
                                                mOptions.build(), new SimpleImageLoadingListener() {
                                                    @Override
                                                    public void onLoadingStarted(String imageUri, View view) {
                                                        super.onLoadingStarted(imageUri, view);
                                                        int color;
                                                    }

                                                    @Override
                                                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                                                        super.onLoadingComplete(imageUri, view, loadedImage);
                                                    }
                                                }, null);

                                }

                                holder.card.setOnClickListener(v -> {

                                });
                            }
                        };

firestorePagingAdapter.notifyDataSetChanged();
        mRecyclerView.setAdapter(Utils.getAnimationAdapter(firestorePagingAdapter));


     private void insertAdsInWallpaperItems() {
            if (unifiedNativeAds.size() <= 0) {
                return;
            }

            int offset = (wallpaperList.size() / unifiedNativeAds.size()) + 1;
            int index = 0;
            for (UnifiedNativeAd ad : unifiedNativeAds) {
                if(index != 0){
                    wallpaperList.add(index, ad);
                }
                index = index + offset;
            }
        }
1 Answers

You can try something like below. Although, it does not alter the original list size, it can help you squeeze the ad data into one of your list items.

firestorePagingOptions.value = FirestorePagingOptions.Builder<Story>()
                .setQuery(factsQuery, Source.DEFAULT, config) {
                    val story = it.toObject(Story::class.java)!!
                    //Modify your data here
                    story //return the modified object 
                }.build()
Related