如何随机显示包含广告的CardView,但不显示在3张卡片之前而不在7张卡片之后? [英] How to show CardView containing ads randomly but not before 3 cards and not after 7 cards?

查看:85
本文介绍了如何随机显示包含广告的CardView,但不显示在3张卡片之前而不在7张卡片之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注教程,以显示卡片并集成Tinder-

I'm following this tutorial to show cards and integrate Tinder-like swipe feature.

张卡片之间,我要展示广告,为此,我正在使用 AdMob

In-between the cards I want to show ads and for that I'm using 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张卡之后

Now I want to show these cards randomly in the stack but not before 3 cards and not after 7 cards.

这是我尝试的方法:

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;
}

尽管此代码是随机显示包含广告的卡片,但并未根据

though this code is showing the cards containing ad randomly but it is not showing according to my needs and this card is appearing even after 1st card.

我如何确保我的需求,并且该卡的出现是随机出现的,而不是在3张卡之前而不是7张卡之后。

How can I make sure that this ad containing card appears randomly but not before 3 cards and not after 7 cards.

推荐答案

如果您知道之前要放的牌,那么您可以做的是
1.创建一个卡堆栈
2.放置前两个非添加卡
3.然后进行随机测试,是否必须将添加卡放在第三位,否则放置非添加卡。
4.将此卡放在7张卡片上进行测试。

If you know the cards to be put from before then what you can do is 1. create a card stack 2. put first two non-add-cards 3. Then make a random test if it has to put a add-card for third else put the non-add-card. 4. do this card put with test till 7 cards.

示例:

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;
}

这篇关于如何随机显示包含广告的CardView,但不显示在3张卡片之前而不在7张卡片之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆