我正在尝试展示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);
}
}
My 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>
当横幅越大然后容器为它做好准备时,有没有办法捕捉到这种情况?
答案 0 :(得分: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
}
让它可见应该可以解决问题。