Search code examples
javaandroidandroid-studioandroid-recyclerviewadapter

How to call onRewardedVideoAdLoaded in onBindViewHolder


I want to use admob with recyclerview but there is a problem. I need to hide some elements that are belong to viewholder. I need to hide the imageview in which position the ImageView belongs. When i click holder.btnReklamIzle the picture in that position must be hid in onRewardedVideoAdLoaded method. How can i transmit the position to onRewardedVideoAdLoaded method?

 public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.fragment_kuponlar, container, false);

    mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(KuponlarFragment.this.getActivity());
    mRewardedVideoAd.setRewardedVideoAdListener(this);

    tahminlerRecyclerView = root.findViewById(R.id.tahminlerRecyclerView);
    linearLayoutManager = new LinearLayoutManager(this.getActivity());
    tahminlerRecyclerView.setLayoutManager(linearLayoutManager);
    tahminlerRecyclerView.setHasFixedSize(true);
    loadRewardedVideoAd();

    fetch();
    return root;
}

private void loadRewardedVideoAd() {
    mRewardedVideoAd.loadAd(getString(R.string.admob_ads_id),
            new AdRequest.Builder().build());
}

private void fetch() {
    Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("tahminler");

    FirebaseRecyclerOptions<Mac> options =
            new FirebaseRecyclerOptions.Builder<Mac>()
                    .setQuery(query, new SnapshotParser<Mac>() {
                        @NonNull
                        @Override
                        public Mac parseSnapshot(@NonNull DataSnapshot snapshot) {
                            return new Mac((double)snapshot.child("oran").getValue(),
                                    snapshot.child("tahmin").getValue().toString(),                     
                            );
                        }
                    })
                    .build();

    adapter = new FirebaseRecyclerAdapter<Mac, ViewHolder>(options) {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.tahmin_tasarim_recyclerview, parent, false);



            return new ViewHolder(view);
        }


        @Override
        protected void onBindViewHolder(final ViewHolder holder, final int position, Mac mac) {


            holder.btnReklamIzle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mRewardedVideoAd.isLoaded()) {
                        mRewardedVideoAd.show();                    
                    }
                }});
        }
    };
    tahminlerRecyclerView.setAdapter(adapter);
}

@Override
public void onRewardedVideoAdLoaded() {

}

public class ViewHolder extends RecyclerView.ViewHolder {

public ImageView image;

    public ViewHolder(View itemView) {
        super(itemView);
        image = itemView.findViewById(R.id.image);
    }
}

Solution

  • onRewardedVideoAdLoaded is a callback method for an asynchronous operation hence you cannot pass values to it as arguments but use referenced variables.

    For your case do the following:

    Firstly

    Create a global variable to hold the list of views to hide

    ArrayList<View> views_to_hide = new ArrayList<>();
    

    Secondly

    Create a helper function to hide the views

    function hideViews(ArrayList<View> views){
          for(View v : views) v.setVisibility(View.GONE);
    }
    

    Thirdly

    Inside onBindViewHolder Add to the list the views you want to hide under your button onClick

    public void onClick(View v) {
       //...
        if (mRewardedVideoAd.isLoaded()) {
               mRewardedVideoAd.show();   
               // Ads already shown you may want to manually hide other images here                 
          }else{
                // We only need to add to list when ads not loaded
                // We also want to make sure we don't add same view to the list twice
               if(!views_to_hide.contains(holder.image))
                  views_to_hide.add(holder.image);
          }});
       //...
    

    Finally

    Call your helper function inside onRewardedVideoAdLoaded

     @Override
     public void onRewardedVideoAdLoaded() {
      //This hides the views that was added to the list before now
         hideViews(views_to_hide);
     }