【发布时间】:2018-03-20 05:17:07
【问题描述】:
我正在关注this 教程来展示卡片并集成类似 Tinder 的滑动功能。
在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;
}
虽然此代码随机显示包含广告的卡片,但它没有根据我的需要显示,而且即使在第一张卡片之后,这张卡片也会出现。
如何确保此包含卡片的广告随机出现,但不在 3 张卡片之前,也不在 7 张卡片之后。
【问题讨论】:
-
如果我正确理解了您的问题,您需要一个介于 3 和 7 之间的数字(3 是最小值,7 是最大值)。根据我的回答here,公式为:
final int random = Random.nextInt((7 - 3) + 1) + 3;或为速度而简化(执行时少加一个):final int random = Random.nextInt((4) + 1) + 3; -
@ModularSynth 我知道了如何获得 3 到 7 范围内的随机数。我想知道的是我应该使用的算法来随机显示
mSwipeView.addView(new AdCards(mContext, mSwipeView));,而不是在 3 张牌之前而不是在 7 张牌之后如问题中所述的卡片。我希望你明白了。 -
那么你不需要 nextInt 逻辑。您正在询问如何管理 SwipeView 自定义控件以遵循您所追求的逻辑。但我对这个 TinderSwipe 一无所知。
-
@ModularSynth 不。请看问题。我正在尝试使用随机数显示该视图,其中我尝试在 3 张卡片之前而不是在 7 张卡片之后显示它。
-
但是你已经成功地处理了随机数。现在你需要做的就是弄清楚如何在你的应用程序的逻辑流程中使用它们,如果这对你有意义的话。
标签: android random admob android-cardview