首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何随机显示包含广告的CardView,但不是在3张卡片之前,也不是在7张卡片之后?

如何随机显示包含广告的CardView,但不是在3张卡片之前,也不是在7张卡片之后?
EN

Stack Overflow用户
提问于 2017-10-09 12:38:22
回答 1查看 3.1K关注 0票数 0

我正在按照this教程来展示卡片和集成类似于Tinder的刷卡功能。

在我想要显示广告的cards和我使用的AdMob之间。

代码如下:

代码语言:javascript
复制
@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

代码语言:javascript
复制
<?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张卡片之后。

这是我尝试过的:

代码语言:javascript
复制
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;
}

虽然这个代码是随机显示包含广告的卡片,但它不是根据我的需要显示的,即使在第一张卡片之后也会出现这张卡片。

我如何确保这个包含卡片的广告随机出现,但不是在3张卡片之前,也不是在7张卡片之后。

EN

回答 1

Stack Overflow用户

发布于 2017-10-16 13:45:16

如果你知道之前要放的牌,那么你可以做的是1.创建一个卡片栈2.放入前两张非附加卡3.然后随机测试是否必须放置第三张附加卡,否则放置非附加卡。4.做这张牌的测试,直到7张牌。

示例:

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46638945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档