与 ViewPager 一起使用的插页式广告出现问题
Posted
技术标签:
【中文标题】与 ViewPager 一起使用的插页式广告出现问题【英文标题】:Trouble with Interstitial Ads working with ViewPager 【发布时间】:2014-08-01 02:54:25 【问题描述】:我想在我的 viewPager 中添加插页式广告,但是一旦插页式广告关闭(或者可能在它打开时),我就会遇到容器“消失”(或被破坏)的问题。滑动后会自动调用@Override 方法“public void destroyItem(ViewGroup container, int position, Object object)”,并调用插页式广告,但是,视图不会像未调用插页式广告时那样再次膨胀.
我的类扩展了 PagerAdapter,我的一般设置如下:
@Override
public boolean isViewFromObject(View view, Object object)
// TODO Auto-generated method stub
return view == ((RelativeLayout) object);
@Override
public Object instantiateItem(final ViewGroup container, final int position)
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//instantiate TextViews, Buttons, ect (code not posted)
((ViewPager) container)
.setOnPageChangeListener(new OnPageChangeListener()
@Override
public void onPageSelected(int position)
// the maximum position value is 130
// I want to display an interstitial ad every 7th position
if (position % 7 == 0)
displayInterstitial();
@Override
public void onPageScrollStateChanged(int state)
// TODO Auto-generated method stub
@Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels)
// TODO Auto-generated method stub
);
if (interstitialLoad == false && isConnection == false)
if (NetworkUtils.isConnected(context) == true
&& NetworkUtils.isNetworkActive(context) == true)
isConnection = true;
// create an ad
interstitialAd = new InterstitialAd(context);
interstitialAd.setAdUnitId(AD_UNIT_ID_INT);
// request interstitial ads
AdRequest adRequestInterstitial = new AdRequest.Builder()
.build();
// begin loading interstitial
interstitialAd.loadAd(adRequestInterstitial);
if (isConnection = true)
// Set the AdListener.
interstitialAd.setAdListener(new AdListener()
@Override
public void onAdClosed()
// do nothing
@Override
public void onAdLoaded()
if (interstitialLoad == false)
interstitialLoad = true;
@Override
public void onAdFailedToLoad(int errorCode)
String message = String.format("onAdFailedToLoad (%s)",
getErrorReason(errorCode));
Log.e(TAG, message);
);
// add fragments to container (ViewPager)
((ViewPager) container).addView(itemView);
return itemView;
/** Gets a string error reason from an error code. */
private String getErrorReason(int errorCode)
String errorReason = "";
switch (errorCode)
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
errorReason = "Internal error";
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
errorReason = "Invalid request";
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
errorReason = "Network Error";
break;
case AdRequest.ERROR_CODE_NO_FILL:
errorReason = "No fill";
break;
return errorReason;
// invoke displayInterstitial() when ready to display an interstitial
private void displayInterstitial()
if (interstitialLoad == true && interstitialAd.isLoaded())
Log.e(TAG, "displayInterstitial()");
interstitialAd.show();
@Override
public void restoreState(Parcelable state, ClassLoader loader)
// TODO Auto-generated method stub
super.restoreState(state, loader);
@Override
public Parcelable saveState()
// TODO Auto-generated method stub
return super.saveState();
@Override
public void destroyItem(ViewGroup container, int position, Object object)
((ViewPager) container).removeView((RelativeLayout) object);
这是事情发生的顺序(假设我们在位置 6(第 6 页)):
1)当我滑动到第7页时,首先会自动调用@Override destroyItem(ViewGroup container, int position, Object object)方法。
2)然后自动调用@Override onPageSelected(int position)方法,显式调用displayInterstitial()方法。
3) 从 displayInterstitial() 方法内部,显示插页式广告。此时会自动调用@Override 方法saveState()。
4) 作为“最终用户”,我然后单击“X”或按设备返回按钮以关闭显示的广告。
5) 整个视图(或/和容器)都消失了。我希望的预期行为是 @Override 方法 restoreState() 将被调用,然后我仍然可以看到我的容器和它的视图。
如何使所有内容从调用和关闭插页式广告之前的中断处重新开始?任何人对如何解决这个问题有任何想法?提前致谢!
【问题讨论】:
【参考方案1】:可能与您的问题无关,但是
if (isConnection = true)
应该是:
if (isConnection == true)
【讨论】:
【参考方案2】:感谢您的回复,Dan 和我想出了一个解决方案。我的问题是,在显示并销毁第一个广告后,我需要重新加载新广告。所以我基本上在 AdListener 内部重新实例化,我的冲突就解决了。每 7 次滑动无限次显示广告单元。干杯!
if (isConnection == true)
// Set the AdListener.
interstitialAd.setAdListener(new AdListener()
@Override
public void onAdClosed()
// do nothing
Log.i(TAG, "onAdClosed()");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int layoutColorId = ElementStructure.arrayLayoutColor.get(position);
if (layoutColorId == 1)
itemView = inflater.inflate(R.layout.frame_learn_a, container,
false);
else if (layoutColorId == 2)
itemView = inflater.inflate(R.layout.frame_learn_b, container,
false);
else if (layoutColorId == 3)
itemView = inflater.inflate(R.layout.frame_learn_c, container,
false);
else if (layoutColorId == 4)
itemView = inflater.inflate(R.layout.frame_learn_d, container,
false);
// reload interstitial ad
if (NetworkUtils.isConnected(context) == true
&& NetworkUtils.isNetworkActive(context) == true)
Log.i(TAG, "Internet connection detected :: reload interstitial");
if (isConnection == false)
isConnection = !isConnection;
// create an ad
interstitialAd = new InterstitialAd(context);
interstitialAd.setAdUnitId(AD_UNIT_ID_INT);
// request interstitial ads
AdRequest adRequestInterstitial = new AdRequest.Builder()
.build();
// begin loading interstitial
interstitialAd.loadAd(adRequestInterstitial);
@Override
public void onAdLoaded()
if (interstitialLoad == false)
Log.e(TAG, "onAdLoaded (interstitial)");
interstitialLoad = !interstitialLoad;
@Override
public void onAdFailedToLoad(int errorCode)
String message = String.format("onAdFailedToLoad (%s)",
getErrorReason(errorCode));
Log.e(TAG, message);
);
【讨论】:
以上是关于与 ViewPager 一起使用的插页式广告出现问题的主要内容,如果未能解决你的问题,请参考以下文章