0

我正在尝试显示AdMob横幅。问题是onAdLoaded()每次我启动包含我的横幅的活动时都会调用该方法。即使在设备宽度小于横幅宽度的情况下。我只有没有足够的空间在我的 Logcat 中显示广告。这是我的代码(部分):

private class BannerHolder extends RecyclerView.ViewHolder {

    private PublisherAdView adView;
    private ImageView closeAdd;
    private View divider;
    private RelativeLayout adLayout;
    private PublisherAdRequest adRequest = new PublisherAdRequest.Builder()
            .build();

    BannerHolder(View itemView) {
        super(itemView);
        adLayout = (RelativeLayout) itemView.findViewById(R.id.banner_place);
        divider = itemView.findViewById(R.id.divider);
        adView = new PublisherAdView(itemView.getContext());
        closeAdd = (ImageView) itemView.findViewById(R.id.close_add);

        adView.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                super.onAdClosed();
            }

            @Override
            public void onAdFailedToLoad(int i) {
                super.onAdFailedToLoad(i);
                adLayout.setVisibility(View.GONE);
                divider.setVisibility(View.GONE);
                adView.setVisibility(View.GONE);
                closeAdd.setVisibility(View.GONE);
            }

            @Override
            public void onAdLeftApplication() {
                super.onAdLeftApplication();
            }

            @Override
            public void onAdOpened() {
                super.onAdOpened();
            }

            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                adLayout.setVisibility(View.VISIBLE);
                divider.setVisibility(View.VISIBLE);
                adView.setVisibility(View.VISIBLE);
                closeAdd.setVisibility(View.VISIBLE);
            }
        });

        closeAdd.setOnClickListener(v -> {
            adLayout.setVisibility(View.GONE);
            divider.setVisibility(View.GONE);
            adView.setVisibility(View.GONE);
            closeAdd.setVisibility(View.GONE);

            isAddClosedByUser = true;

            itemView.getContext().getSharedPreferences("BANNER", Context.MODE_PRIVATE).edit().putBoolean("MAIN_SCREEN_BANNER_CLOSED", true).apply();
        });

        if (!isAddClosedByUser) {
            //Параметры View для баннера
            if (Build.VERSION.SDK_INT >= 19) {
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT));
                params.setMargins(0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_top),
                        0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_bottom));
                params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                adView.setLayoutParams(params);
                adView.setVisibility(View.GONE);
            } else {
                ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT));
                params.setMargins(0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_top),
                        0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_bottom));
                adView.setLayoutParams(params);
                adView.setVisibility(View.GONE);
            }

            //Задание параметров баннера
            if (adView.getAdUnitId() == null) {
                adView.setAdUnitId(tabBannerId);
            }
            if (adView.getAdSizes() == null) {
                adView.setAdSizes(AdSize.MEDIUM_RECTANGLE, new AdSize((int) tabBannerExtraWidth, (int) tabBannerExtraHeight), AdSize.SMART_BANNER);
            }

            if (!isAddClosedByUser) {
                adView.loadAd(adRequest);
                adLayout.removeView(adView);
                adLayout.addView(adView);

            }
        }

        if (!isAddClosedByUser) {
            adView.loadAd(adRequest);
        }
    }

    void update() {
        adView.loadAd(adRequest);
    }
}

我的 BannerHolder xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<View
    android:id="@+id/divider"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#E5E5E5"
    android:visibility="gone" />

<RelativeLayout
    android:id="@+id/banner_place"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@color/banner_main_screen_background"
    android:visibility="gone">

    <ImageView
        android:id="@+id/close_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="@dimen/banner_body_close_add_padding"
        android:src="@drawable/cancel"
        android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>

当横幅大于为其准备的容器时,有什么办法可以捕捉到这种情况?

4

1 回答 1

0

我了解到您正在尝试在第一个实例中加载广告,因此 isAddClosedByUser 以 false 开头,所以在这里:

if (Build.VERSION.SDK_INT >= 19) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));
            params.setMargins(0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_top),
                    0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_bottom));
            params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
            adView.setLayoutParams(params);
            adView.setVisibility(View.GONE); //<----- YOUR ADVIEW IS NEVER GONNA SHOW
        } else {
            ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));
            params.setMargins(0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_top),
                    0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_bottom));
            adView.setLayoutParams(params);
            adView.setVisibility(View.GONE); //<----- YOUR ADVIEW IS NEVER GONNA SHOW
        }

让它可见应该可以解决问题。

于 2017-12-19T14:37:52.620 回答