android - showing rewarded video ads after click list

十年热恋 提交于 2019-12-20 07:51:01

问题


I have a list of items at homepage and when clicked will go to the activity detail. Before that I want to add Rewarded Video Ads, but with a limit after the user 3x click on the item list the ads will appear. Any suggestions for making it like that? or Similar features of Shared Preferences maybe?

UPDATE: I have try to run with below code, but this does not count every list that is clicked..

class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle;
        private LinearLayout rowLayout;

        ViewHolder(View itemView, final Context ctx) {
            super(itemView);
            mContext = ctx;
            tvTitle = itemView.findViewById(R.id.tvTitle);
            rowLayout = itemView.findViewById(R.id.rowLayout);

               itemView.setOnClickListener(new View.OnClickListener() {
               int clickCount = 1;
                    @Override
                    public void onClick(View v){
                        if(clickCount > 3) {

                          if(mRewardedVideoAd.isLoaded()){
                            mRewardedVideoAd.show();
                        }
                            clickCount = 0;
                        } else {

                            clickCount++;
                        Intent intent = new Intent(mContext, DetailsActivity.class);
                        intent.putExtra("title", dataList.get(getAdapterPosition()));
                        intent.putExtra("preview", previewList.get(getAdapterPosition()));
                        ctx.startActivity(intent);

                        }
                    }
                  }
                 }
                });

It should apply to all every clicked lists, not just for the list per item counted.


回答1:


Updated:

itemView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v){
int clickCount = mPreference.getInt("count",0);
                        if(clickCount > 3) {

                          if(mRewardedVideoAd.isLoaded()){
                            mRewardedVideoAd.show();

                        }
                             mPreference.edit().remove("count").apply();

                        } else {

                            clickCount++;
mPreference.edit().putInt("count",clickCount).apply();
                        Intent intent = new Intent(mContext, DetailsActivity.class);
                        intent.putExtra("title", dataList.get(getAdapterPosition()));
                        intent.putExtra("preview", previewList.get(getAdapterPosition()));
                        ctx.startActivity(intent);

                        }
                    }
                  });

Why don't you use custom interface in your ViewHolder class rather than doing this !




回答2:


Try to use this code :

     Button button = findViewById(R.id.button_id);
     int click = 0 ;
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            if(click > 3){
            //show ads
            }else{
             click++ ;
            }
         PreferenceManager.getDefaultSharedPreferences(MainActivity.this)
        .edit().putString(key, value).apply();

         }
     });


来源:https://stackoverflow.com/questions/54362410/android-showing-rewarded-video-ads-after-click-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!