RecyclerView is loading Multiple ads for the same position

17 Views Asked by At
package com.example.movieflix.ui.adapters;

import static com.google.android.material.internal.ContextUtils.getActivity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.movieflix.R;
import com.google.android.ads.nativetemplates.TemplateView;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.nativead.NativeAdOptions;

import java.util.List;

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MyViewHolder> {

    private final Context mContext;
    private final List<VideoItems> mData;
    private final RequestOptions option;

    public M[enter image description here][1]ovieAdapter(Context mContext, List<VideoItems> mData) {
        this.mContext = mContext;
        this.mData = mData;
        this.option = new RequestOptions().centerCrop().placeholder(R.drawable.loading_icon).error(R.drawable.loading_icon);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_video_items, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        if ((position + 1) % 3 == 0) { // Show native ad after every 3 items
            holder.templateView.setVisibility(View.VISIBLE);
            loadNativeAd(holder.templateView);
        } else {
            holder.templateView.setVisibility(View.GONE);
        }

        VideoItems item = mData.get(position);
        holder.bind(item);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    private void loadNativeAd(TemplateView templateView) {
        AdLoader adLoader = new AdLoader.Builder(mContext, "ca-app-pub-3940256099942544/2247696110")
                .forNativeAd(nativeAd -> {
                    // Display the native ad
                    templateView.setNativeAd(nativeAd);
                })
                .withAdListener(new AdListener() {

                    private boolean adLoaded = false;

                    @Override
                    public void onAdLoaded() {
                        super.onAdLoaded();
                        // Mark ad as loaded
                        adLoaded = true;
                    }

                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                        super.onAdFailedToLoad(loadAdError);
                    }
                })
                .withNativeAdOptions(new NativeAdOptions.Builder().build())
                .build();

        adLoader.loadAd(new AdRequest.Builder().build());
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_name;
        TextView tv_type;
        TextView tv_detail;
        ImageView img_thumbnail;
        TemplateView templateView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            img_thumbnail = itemView.findViewById(R.id.movieThumbnail);
            tv_name = itemView.findViewById(R.id.movieTitle);
            tv_type = itemView.findViewById(R.id.movieType);
            tv_detail = itemView.findViewById(R.id.movieDetail);
            templateView = itemView.findViewById(R.id.my_template);
        }

        public void bind(VideoItems item) {
            tv_name.setText(item.getName());
            tv_type.setText(item.getType());
            tv_detail.setText(item.getDetail());

            // Load image with Glide
            if (item.getImage_url() != null && !item.getImage_url().isEmpty()) {
                Glide.with(mContext)
                        .load(item.getImage_url())
                        .apply(option)
                        .error(R.drawable.loading_icon) // Placeholder image in case of loading error
                        .into(img_thumbnail);
            } else {
                // If image URL is empty, you can set a default placeholder image
                img_thumbnail.setImageResource(R.drawable.loading_icon);
            }

            // Set OnClickListener for the item
            itemView.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, MoviePlayer.class);
                intent.putExtra("video_url", item.getVideo_url());
                mContext.startActivity(intent);
            });
        }
    }
}

Please check the adapter. I have implemented the native ads in it and ads are loading and showing multiple for the same position. Ad loads every time new when I scroll down to it. I think once the ad is loaded and showed up it should not load and showed again a new ad for the same position. This auto multiple time loading and showing as is making app so laggy.

0

There are 0 best solutions below