我想在我的viewPager中添加插页式广告,但我遇到了容器问题"正在消失"插页式广告关闭后(或打开时)广告(或被销毁)。 @Override方法" public void destroyItem(ViewGroup容器,int位置,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容器,int位置,Object对象)方法。
2)然后自动调用@Override onPageSelected(int position)方法,并显式调用displayInterstitial()方法。
3)从displayInterstitial()方法内部,显示插页式广告。此时,将自动调用@Override方法saveState()。
4)作为最终用户',然后我点击" X"或按设备返回按钮关闭显示的广告。
5)整个视图(或/和容器)消失了。我希望的预期行为是@Override方法restoreState()将被调用,然后我仍然可以看到我的容器及其视图。
如何在调用和关闭插页式广告之前将所有内容从中断位置恢复?有任何想法如何解决这个问题的人?提前谢谢!
答案 0 :(得分:1)
可能与您的问题无关,但
if (isConnection = true) {
应该是:
if (isConnection == true) {
答案 1 :(得分:0)
感谢您的回复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);
}
});
}