如何显示CardView包含广告随机但不是3张卡之前,而不是7张卡之后?
我正在关注this教程来展示卡片并整合了类似Tinder的滑动功能。如何显示CardView包含广告随机但不是3张卡之前,而不是7张卡之后?
介于cards
之间我想展示广告并为此使用AdMob
。
下面的代码:
@Layout(R.layout.ad_cards_view)
public class AdCards {
@View(R.id.adView)
NativeExpressAdView nativeExpressAdView;
private Context mContext;
private SwipePlaceHolderView mSwipeView;
public AdCards (Context context, SwipePlaceHolderView swipePlaceHolderView) {
mContext = context;
mSwipeView = swipePlaceHolderView;
}
@Resolve
private void onResolved() {
AdRequest request = new AdRequest.Builder()
.addTestDevice("***")
.addTestDevice("***")
.build();
nativeExpressAdView.setVideoOptions(new VideoOptions.Builder()
.setStartMuted(true)
.build());
nativeExpressAdView.loadAd(request);
}
}
这里的ad_cards_view.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="350dp"
android:layout_height="395dp"
android:layout_gravity="center"
android:layout_marginTop="35dp">
<android.support.v7.widget.CardView
android:orientation="vertical"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
app:cardCornerRadius="7dp"
app:cardElevation="4dp">
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
ads:adUnitId="ca-app-pub-xxx"
ads:adSize="320x300">
</com.google.android.gms.ads.NativeExpressAdView>
</android.support.v7.widget.CardView>
</RelativeLayout>
现在我想在随意堆而不是之前3张牌,不单7卡显示这些卡。
这里是我的尝试:
rand = new Random();
random = rand.nextInt(8-3) + 3;
count = rand.nextInt(8-3) + 3;
mSwipeView.addView(new Cards(mContext, profile, mSwipeView));
if (count >= random) {
mSwipeView.addView(new AdCards(mContext, mSwipeView));
random = rand.nextInt(8-3) + 3;
count = rand.nextInt(8-3) + 3;
}
尽管此代码显示含有广告牌随机,但它是根据我的需求没有展示这张卡即使在第1张卡出现。
如何确保此广告包含卡片随机出现,但不在3张卡片之前,而不是7张卡片之后。
如果你知道卡从之前那么你可以做什么来把被 1.创建一个卡片堆栈 2.第一次就把两个非附加卡 3.然后进行随机测试,如果它有把第三个添加卡放在非添加卡上。 4.把这张卡放在测试卡上,直到7张卡。
例子:
boolean atLeastOneAddPut = false;
int count = 1;
for(Data data: DataArray){
if(count > 2 && count < 8 && shouldPutAdd()){
mSwipeView.add(new AddView());
count++;
}
mSwipeView.add(new NonAddView(data));
count++;
}
private boolean shouldPutAdd(){
//generate random 0 and 1
int random;
...
return random == 1;
}
当计数增加7时,这将停止显示adView。我想在每3张普通卡之前随机显示广告,而不是在每7张普通卡之后显示广告。 –
请参考:https://github.com/janishar/Tutorials/tree/master/RandomPromotionalCards –
如果我明白你的问题正确,你想3和7(3为最低,7为最大)之间的数字。根据我的回答,[这里](https://*.com/a/21049922/2649012),公式是:'final int random = Random.nextInt((7 - 3)+ 1)+ 3;'或简化速度(一个加法执行):'final int random = Random.nextInt((4)+ 1)+ 3;' –
@ModularSynth我得到了如何获得范围在3到7之间的随机数。我想知道的是我应该使用的算法来显示'mSwipeView.addView(新的AdCards(mContext,mSwipeView));'在问题中提到的'3个卡之后而不是7个卡之后的随机数。我希望你明白了。 –
然后你不要求nextInt逻辑。您在问如何管理SwipeView自定义控件以遵循您之后的逻辑。但我对这个TinderSwipe一无所知。 –