简体   繁体   English

如何在 onBindViewHolder 中调用 onRewardedVideoAdLoaded

[英]How to call onRewardedVideoAdLoaded in onBindViewHolder

I want to use admob with recyclerview but there is a problem.我想将 admob 与 recyclerview 一起使用,但存在问题。 I need to hide some elements that are belong to viewholder.我需要隐藏一些属于 viewholder 的元素。 I need to hide the imageview in which position the ImageView belongs.我需要隐藏 ImageView 所属位置的 imageview。 When i click holder.btnReklamIzle the picture in that position must be hid in onRewardedVideoAdLoaded method.当我点击 holder.btnReklamIzle 时,该位置的图片必须隐藏在 onRewardedVideoAdLoaded 方法中。 How can i transmit the position to onRewardedVideoAdLoaded method?如何将位置传输到 onRewardedVideoAdLoaded 方法?

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

onRewardedVideoAdLoaded is a callback method for an asynchronous operation hence you cannot pass values to it as arguments but use referenced variables. onRewardedVideoAdLoaded是异步操作的回调方法,因此您不能将值作为参数传递给它,而是使用引用变量。

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 onClickonBindViewHolder要隐藏在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 onRewardedVideoAdLoadedonRewardedVideoAdLoaded调用您的辅助函数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM