如何显示非活动类的非页内广告?

时间:2018-03-31 11:07:23

标签: java android admob

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

自定义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();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

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

1 个答案:

答案 0 :(得分:2)

您可以从非活动类

中调用此类插页式广告
  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)另外,看到用户花费的时间足以使广告         已加载。