问题
I'm following this tutorial to show cards and integrate Tinder-like swipe feature.
In-between the cards
I want to show ads and for that I'm using AdMob
.
Here's the code:
@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);
}
}
Here's 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>
Now I want to show these cards randomly in the stack but not before 3 cards and not after 7 cards.
Here's what I tried:
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.
How can I make sure that this ad containing card appears randomly but not before 3 cards and not after 7 cards.
回答1:
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.
Example:
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;
}
来源:https://stackoverflow.com/questions/46638945/how-to-show-cardview-containing-ads-randomly-but-not-before-3-cards-and-not-afte