public class MoPubStreamAdPlacer
extends java.lang.Object
MoPubStreamAdPlacer facilitates loading ads and placing them into a content stream.
If you are inserting ads into a ListView, we recommend that you use a MoPubAdAdapter
instead of this class.
To start loading ads, call loadAds(java.lang.String). We recommend passing targeting information to
increase the chance that you show ads that are relevant to your users.
This class is not intended to be used by multiple threads. All calls should be made from the main
UI thread.| Modifier and Type | Field and Description |
|---|---|
static int |
CONTENT_VIEW_TYPE
Constant representing that the view type for a given position is a regular content item
instead of an ad.
|
| Constructor and Description |
|---|
MoPubStreamAdPlacer(android.app.Activity activity)
Creates a new MoPubStreamAdPlacer object.
|
MoPubStreamAdPlacer(android.app.Activity activity,
MoPubNativeAdPositioning.MoPubClientPositioning adPositioning)
Creates a new MoPubStreamAdPlacer object, using client positioning.
|
MoPubStreamAdPlacer(android.app.Activity activity,
MoPubNativeAdPositioning.MoPubServerPositioning adPositioning)
Creates a new MoPubStreamAdPlacer object, using server positioning.
|
| Modifier and Type | Method and Description |
|---|---|
void |
bindAdView(NativeAd nativeAd,
android.view.View adView)
Given an ad and a view, attaches the ad data to the view and prepares the ad for display.
|
void |
clearAds()
Stops loading ads, immediately clearing any ads currently in the stream.
|
void |
destroy()
Destroys the ad placer, preventing it from future use.
|
java.lang.Object |
getAdData(int position)
Returns an ad data object, or
null if there is no ad at this position. |
int |
getAdjustedCount(int originalCount)
Returns the number of items considering ads in the stream.
|
int |
getAdjustedPosition(int originalPosition)
Returns the position of an item considering ads in the stream.
|
MoPubAdRenderer |
getAdRendererForViewType(int viewType) |
android.view.View |
getAdView(int position,
android.view.View convertView,
android.view.ViewGroup parent)
Gets the ad at the given position, or
null if there is no ad at the given position. |
int |
getAdViewType(int position)
The ad view type for this position.
|
int |
getAdViewTypeCount()
Returns the number of ad view types that can be placed by this ad placer.
|
int |
getOriginalCount(int count)
Returns the original number of items considering ads in the stream.
|
int |
getOriginalPosition(int position)
Returns the original position of an item considering ads in the stream.
|
void |
insertItem(int originalPosition)
Inserts a content row at the given position, adjusting ad positions accordingly.
|
boolean |
isAd(int position)
Whether the given position is an ad.
|
void |
loadAds(java.lang.String adUnitId)
Start loading ads from the MoPub server.
|
void |
loadAds(java.lang.String adUnitId,
RequestParameters requestParameters)
Start loading ads from the MoPub server, using the given request targeting information.
|
void |
moveItem(int originalPosition,
int newPosition)
Moves the content row at the given position adjusting ad positions accordingly.
|
void |
placeAdsInRange(int startPosition,
int endPosition)
Inserts ads that should appear in the given range.
|
void |
registerAdRenderer(MoPubAdRenderer adRenderer)
Registers an ad renderer for rendering a specific native ad format in your stream.
|
int |
removeAdsInRange(int originalStartPosition,
int originalEndPosition)
Removes ads in the given range from [originalStartPosition, originalEndPosition).
|
void |
removeItem(int originalPosition)
Removes the content row at the given position, adjusting ad positions accordingly.
|
void |
setAdLoadedListener(MoPubNativeAdLoadedListener listener)
Sets a listener that will be called after the SDK loads new ads from the server and places
them into your stream.
|
void |
setItemCount(int originalCount)
Sets the original number of items in your stream.
|
public static final int CONTENT_VIEW_TYPE
public MoPubStreamAdPlacer(android.app.Activity activity)
MoPubStreamAdPlacer(Context,
MoPubClientPositioning).activity - The activity.public MoPubStreamAdPlacer(android.app.Activity activity,
MoPubNativeAdPositioning.MoPubServerPositioning adPositioning)
activity - The activity.adPositioning - A positioning object for specifying where ads will be placed in your
stream. See MoPubNativeAdPositioning.serverPositioning().public MoPubStreamAdPlacer(android.app.Activity activity,
MoPubNativeAdPositioning.MoPubClientPositioning adPositioning)
activity - The activity.adPositioning - A positioning object for specifying where ads will be placed in your
stream. See MoPubNativeAdPositioning.clientPositioning().public void registerAdRenderer(MoPubAdRenderer adRenderer)
getAdView(int, android.view.View, android.view.ViewGroup).adRenderer - The ad renderer.public MoPubAdRenderer getAdRendererForViewType(int viewType)
public void setAdLoadedListener(MoPubNativeAdLoadedListener listener)
loadAds(java.lang.String) and when you call destroy(). You can also set the listener to null to remove the listener.
Note that there is not a one to one correspondence between calls to loadAds(java.lang.String) and this
listener. The SDK will call the listener every time an ad loads.listener - The listener.public void loadAds(java.lang.String adUnitId)
loadAds(String, RequestParameters) instead of this method, in
order to pass targeting information to the server.adUnitId - The ad unit ID to use when loading ads.public void loadAds(java.lang.String adUnitId,
RequestParameters requestParameters)
MoPubNativeAdLoadedListener.onAdLoaded(int) will be called for
each ad that is added to the stream.
To refresh ads in your stream, call loadAds again. When new ads load, they will
replace the current ads in your stream. If you are using MoPubNativeAdLoadedListener
you will see a call to onAdRemoved for each of the old ads, followed by a calls to
onAdLoaded.adUnitId - The ad unit ID to use when loading ads.requestParameters - Targeting information to pass to the ad server.public void placeAdsInRange(int startPosition,
int endPosition)
placeAdsInRange
again before ads are retrieved from the server, the new ads will show in the new positions
rather than the old positions.
You can pass any integer as a startPosition and endPosition for the range, including negative
numbers or numbers greater than the current stream item count. The ad placer will only place
ads between 0 and item count.startPosition - The start of the range in which to place ads, inclusive.endPosition - The end of the range in which to place ads, exclusive.public boolean isAd(int position)
true only if there is an ad loaded for this position. You can listen
for ads to load using MoPubNativeAdLoadedListener.onAdLoaded(int).position - The position to check for an ad, expressed in terms of the position in the
stream including ads.public void clearAds()
removeAdsInRange(int, int).
When ads are cleared, MoPubNativeAdLoadedListener.onAdRemoved(int) will be called for each
ad that is removed from the stream.public void destroy()
Fragment#onCreateView you should destroy it in {code
Fragment#onDestroyView}.public java.lang.Object getAdData(int position)
null if there is no ad at this position.
This method is useful when implementing your own Adapter using MoPubStreamAdPlacer.
To avoid worrying about view type, consider using MoPubAdAdapter instead of this
class.position - The position where to place an ad.public android.view.View getAdView(int position,
android.view.View convertView,
android.view.ViewGroup parent)
null if there is no ad at the given position.
This method will attempt to reuse the convertView if it is not null, and will
otherwise create it. See MoPubAdRenderer.createAdView(Context, ViewGroup).position - The position to place an ad into.convertView - A recycled view into which to render data, or null.parent - The parent that the view will eventually be attached to.public void bindAdView(NativeAd nativeAd, android.view.View adView)
nativeAd - the ad to bind.adView - the view to bind it to.public int removeAdsInRange(int originalStartPosition,
int originalEndPosition)
originalStartPosition - The start position to clear (inclusive), expressed as the original content
position before ads were inserted.originalEndPosition - The position after end position to clear (exclusive), expressed as the
original content position before ads were inserted.public int getAdViewTypeCount()
getAdViewType(int)public int getAdViewType(int position)
getAdViewTypeCount().
This method is useful when implementing your own Adapter using MoPubStreamAdPlacer.
To avoid worrying about view type, consider using MoPubAdAdapter instead of this
class.position - The stream position.public int getOriginalPosition(int position)
position - The adjusted position.public int getAdjustedPosition(int originalPosition)
originalPosition - The original position.public int getOriginalCount(int count)
count - The number of items in the stream.public int getAdjustedCount(int originalCount)
originalCount - The original number of items.public void setItemCount(int originalCount)
(int) each time an ad is loaded in the stream.originalCount - The original number of items.public void insertItem(int originalPosition)
Item0 Ad Item1 Item2 Ad Item3
and you insert an item at position 2, your new stream will look like:
Item0 Ad Item1 Item2 NewItem Ad Item3originalPosition - The position at which to add an item. If you have an adjusted
position, you will need to call getOriginalPosition(int) to get this value.public void removeItem(int originalPosition)
Item0 Ad Item1 Item2 Ad Item3
and you remove an item at position 2, your new stream will look like:
Item0 Ad Item1 Ad Item3originalPosition - The position at which to add an item. If you have an adjusted
position, you will need to call getOriginalPosition(int) to get this value.public void moveItem(int originalPosition,
int newPosition)
Item0 Ad Item1 Item2 Ad Item3
and you move item at position 2 to position 3, your new stream will look like:
Item0 Ad Item1 Ad Item3 Item2originalPosition - The position from which to move an item. If you have an adjusted
position, you will need to call getOriginalPosition(int) to get this value.newPosition - The new position, also expressed in terms of the original position.