【问题标题】:How can I show Interstitial ad from Non Activity Class?如何在非活动类中展示插页式广告?
【发布时间】:2018-03-31 23:42:54
【问题描述】:

我有一个名为自定义适配器的非活动类,它在主要活动上显示图像,从互联网加载图像并在点击时设置壁纸 单击图像视图时,我想显示非 Activity 类(CustomAdapter)的插页式广告 尝试了很多东西,但都没有成功,请帮助我

自定义适配器.java

    class CustomViewHolder extends  RecyclerView.ViewHolder {

    RibbonLayout ribbonLayout;
    ImageView imageView;

    public CustomViewHolder(View itemView) {
        super(itemView);

        ribbonLayout = (RibbonLayout)itemView.findViewById(R.id.ribbonLayout);
        imageView = (ImageView)itemView.findViewById(R.id.imageView);
    }

    }

    public class CustomAdapter extends  RecyclerView.Adapter<CustomViewHolder> {

    Context context;

    List<Item> itemList;

    public CustomAdapter(Context context, List<Item> itemList) {
        this.context = context;
        this.itemList = itemList;
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
        return new CustomViewHolder(itemView);

    }

    @Override
    public void onBindViewHolder(CustomViewHolder holder, int position) {
        Item item = itemList.get(position);
        if (item.type == 0) { //new
            holder.ribbonLayout.setShowBottom(false);
            holder.ribbonLayout.setShowBottom(false);


            holder.ribbonLayout.setHeaderRibbonColor(Color.parseColor("#2B323A"));
            holder.ribbonLayout.setHeaderTextColor(Color.parseColor("#FFFFFF"));

            holder.ribbonLayout.setHeaderText((item.headerText));
            Picasso.with(context).load(item.imageURL)
                    .into(holder.imageView);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        Bitmap bitmap = ((BitmapDrawable) ((ImageView) view).getDrawable()).getBitmap();
                        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
                        wallpaperManager.setBitmap(bitmap);

                        Toast.makeText(context, " \\ (•◡•) /Yay! Wallpaper Set \\ (•◡•) / ", Toast.LENGTH_LONG).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

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

【问题讨论】:

  • 在设置适配器时将上下文从活动类传递给非活动类。只要您想展示广告,只需使用上下文即可。
  • 尝试了很多东西,但都没有成功。告诉我们你尝试了什么。
  • @Rahulrr2602 你能写出来作为答案吗,也会帮助其他人
  • 如果某件事对其他人有用,但对您无效,那么您一定是做错了什么。除非你展示你的代码和你遇到了什么错误,否则很难调试你的问题。

标签: java android admob


【解决方案1】:

您可以从非活动类中调用这样的插页式广告

  Custom Adapter.java

        class CustomViewHolder extends  RecyclerView.ViewHolder {

        RibbonLayout ribbonLayout;
        ImageView imageView;

        public CustomViewHolder(View itemView) {
            super(itemView);

            ribbonLayout = (RibbonLayout)itemView.findViewById(R.id.ribbonLayout);
            imageView = (ImageView)itemView.findViewById(R.id.imageView);
        }

        }

        public class CustomAdapter extends  RecyclerView.Adapter<CustomViewHolder> {

        Context context;

        List<Item> itemList;

        public CustomAdapter(Context context, List<Item> itemList) {
            this.context = context;
            this.itemList = itemList;
        }

        @Override
        public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
            return new CustomViewHolder(itemView);

        }

        @Override
        public void onBindViewHolder(CustomViewHolder holder, int position) {
            Item item = itemList.get(position);
            if (item.type == 0) { //new
                holder.ribbonLayout.setShowBottom(false);
                holder.ribbonLayout.setShowBottom(false);


                holder.ribbonLayout.setHeaderRibbonColor(Color.parseColor("#2B323A"));
                holder.ribbonLayout.setHeaderTextColor(Color.parseColor("#FFFFFF"));

                holder.ribbonLayout.setHeaderText((item.headerText));
                Picasso.with(context).load(item.imageURL)
                        .into(holder.imageView);
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            Bitmap bitmap = ((BitmapDrawable) ((ImageView) view).getDrawable()).getBitmap();
                            WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
                            wallpaperManager.setBitmap(bitmap);

                            Toast.makeText(context, " \\ (•◡•) /Yay! Wallpaper Set \\ (•◡•) / ", Toast.LENGTH_LONG).show();
 Activity.ShowAd();//Hope you want to call the ads from here.
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }

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

这是你的活动类

    Activity.java

    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.InterstitialAd;

    public class MainActivity extends Activity {

        public static InterstitialAd mInterstitialAd;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            MobileAds.initialize(this,
                "ca-app-pub-3940256099942544~3347511713");

            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
            mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        // Code to be executed when an ad finishes loading.
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        // Code to be executed when an ad request fails.
    }

    @Override
    public void onAdOpened() {
        // Code to be executed when the ad is displayed.
    }

    @Override
    public void onAdLeftApplication() {
        // Code to be executed when the user has left the app.
    }

    @Override
    public void onAdClosed() {
        // Code to be executed when when the interstitial ad is closed.
    }
});
            // Make sure to set the adapter after the above code.
        }

      public static void ShowAd()
        {
           if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
            } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    }

注意- 1) 更改所有广告 ID,因为这些是测试 ID。 2) 请求插播广告后调用设置的适配器代码 3)另外,请注意用户花费的时间是否足够,以便广告 已加载。

【讨论】:

  • 谢谢它的工作,只需要进一步添加 Adlistener
  • @AnishJain 还添加了监听器代码。如果您也需要任何帮助,也可以告诉我。
  • 不,我做了那部分,但是感谢添加,它可能会帮助其他一些人寻找答案
  • @AnishJain 很高兴,它帮助了你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多