MoPubStreamAdPlacer /2.4.0

public class MoPubStreamAdPlacer

Contains methods to request native ads for a stream (any other type of stream). For a manual native ad integration, refer to MoPubNative.java.

Public Constants

public int /CONTENT_VIEW_TYPE /+3.2.0

  • Description: Constant representing that the view type for a given position is a regular content item instead of an ad.

Public Methods

public void /bindAdView(NativeAd nativeAd, View adView) /+3.9.0

  • Description: Given an ad and a view, attaches the ad data to the view and prepares the ad for display.

  • Parameters:

    • nativeAd: The ad to bind.
    • adView: The view to bind the ad to.

public void /clearAds() /+2.4.0

  • Description: Stops loading ads, immediately clearing any ads currently in the stream.

  • Parameters: None.

public void /destroy() /+2.4.0

  • Description: Cleans up the existing ad placer instance in session. Used when finished showing an ad to free up existing resources.

  • Parameters: None.

public Object /getAdData(int position) /+2.4.0

  • Description: Returns an ad data object, or null if there is no ad at this position.

  • Parameters:

    • position: The position where to place an ad.

public int /getAdjustedCount(int originalCount) /+2.4.0

  • Description: Returns the number of items in the stream. Items include both content and ads; each item is a cell in the list.

  • Parameters:

    • originalCount: The original number of items.

public int /getAdjustedPosition(int originalPosition) /+2.4.0

  • Description: Returns the adjusted position of an item in the stream, after the insertion of ads in the stream.

  • Parameters:

    • originalPosition: The original position.

public View /getAdView(int position, View convertView, ViewGroup parent) /+2.4.0

  • Description: Gets the ad at the given position, or null if there is no ad at the given position.

  • Parameters:

    • 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 int /getAdViewType(int position) /+2.4.0

  • Description: Returns the ad view type (content or ad) for this position. Returns 0 if content; between 1 and getAdViewTypeCount() if ad.

  • Parameters:

    • position: The stream position.

public int /getAdViewTypeCount() /+2.4.0

  • Description: Returns the number of ad view types that can be placed by this ad placer.

  • Parameters: None.

public int /getOriginalCount(int count) /+2.4.0

  • Description: Returns the number of items in the stream prior to the insertion of ads. Items include only content; each item is a cell in the list.

  • Parameters:

    • count: Total count of items in the stream, including ads.

public int /getOriginalPosition(int position) /+2.4.0

  • Description: Returns the original position of an item in the stream prior to the insertion of ads in the stream.

  • Parameters:

    • position: The adjusted position.

public void /insertItem(int originalPosition) /+2.4.0

  • Description: Inserts a content row at the given position, adjusting ad positions accordingly. Used when inserting an item into a stream, to increment ad positions based on that new item. To avoid incrementing ad positions when inserting items, notifyDataSetChanged() can be called on the adapter to reload items normally. This is typically the case when inserting items at the end of a stream.

  • Parameters:

    • originalPosition: The original content position at which to add an item. If this is an adjusted position, getOriginalPosition() should be called to get this value.

public boolean /isAd(int position) /+2.4.0

  • Description: Whether the given position is an ad. This will return true only if there is an ad loaded for this position; false otherwise.

  • Parameters:

    • position: The position to check for an ad, expressed in terms of the position in the stream including ads.

public void /loadAds(String adUnitId) /+2.4.0

  • Description: Issues an ad request. Only called after having initialized the SDK with initializeSdk().

  • Parameters:

    • adUnitId: MoPub ad unit ID String.

public void /loadAds(String adUnitId, RequestParameters requestParameters) /+2.4.0

  • Description: Starts loading ads from the MoPub server, using the given request targeting information. Request parameters is an object for specifying targeting data such as keywords, location (deprecated as of 5.12.0), and customer ID.

  • Parameters:

    • adUnitId: MoPub ad unit ID String.
    • requestParameters: An object for specifying targeting data such as keywords, location (deprecated as of 5.12.0), and customer ID. Refer to RequestParameters.java.

public void /moveItem(int originalPosition, int newPosition) /+2.4.0

  • Description: Moves the content row at the given position adjusting ad positions accordingly. Used when moving an item in the stream to have ad positions move as well. For example, if the stream looks like this: {Item0, Ad, Item1, Item2, Ad, Item3}, and the item at position 2 is moved to position 3, the new stream will look like this: {Item0, Ad, Item1, Ad, Item3, Item2}.

  • Parameters:

    • originalPosition: The position from which to move an item. If working with an adjusted position, getOriginalPosition() should be called to get this value.
    • newPosition: The new position, also expressed in terms of the original position.

public void /placeAdsInRange(int startPosition, int endPosition) /+2.4.0

  • Description: Inserts ads that should appear in the given range. By default, the ad placer will place ads within the first 10 positions in the stream, according to the positions specified. This method can be used to place ads into the currently visible range as the user scrolls through the stream.

    This method takes advantage of a short-lived in-memory ad cache, and will immediately place any ads from the cache. If there are no ads in the cache, this method will load additional ads from the server and place them once they are loaded. If placeAdsInRange() is called again before ads are retrieved from the server, the new ads will show in the new positions rather than the old positions.

    Any integer can be passed 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.

  • Parameters:

    • 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 void /registerAdRenderer(MoPubAdRenderer adRenderer) /+2.4.0

  • Description: Registers an ad renderer for rendering a specific native ad format in the stream.

  • Parameters:

public int /removeAdsInRange(int originalStartPosition, int originalEndPosition) /+2.4.0

  • Description: Removes ads in the given range from (originalStartPosition, originalEndPosition). Returns an integer corresponding to the number of ads removed.

  • Parameters:

    • 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 void /removeItem(int originalPosition) /+2.4.0

  • Description: Removes the content row at the given position, adjusting ad positions accordingly. Used when removing an item from the stream, to decrement ad positions based on that removed item. For example, if the stream looks like this: {Item0, Ad, Item1, Item2, Ad, Item3}, and an item at position 2 is removed, the new stream will look like this: {Item0, Ad, Item1, Ad, Item3}.

  • Parameters:

    • originalPosition: The position at which to remove an item. If working with an adjusted position, getOriginalPosition() should be called to get this value.

public void /setAdLoadedListener(MoPubNativeAdLoadedListener listener) /+2.4.0

  • Description: Sets a listener that will be called after the SDK loads new ads from the server and places them into the stream. The listener will be active starting when loadAds() is called and until destroy() is called. The listener can also be set to null to remove the listener. There is not a one-to-one correspondence between calls to loadAd and this listener. The SDK will call the listener every time an ad loads.

  • Parameters:

public void /setItemCount(int originalCount) /+2.4.0

  • Description: Sets the original number of items in the stream. This method is necessary for the placer to know where the valid positions are for placing ads. After calling this method, the ad placer will call MoPubNativeAdLoadedListener.onAdLoaded (int) each time an ad is loaded in the stream.

  • Parameters:

    • originalCount: The original number of items.

Last updated October 06, 2020

TWITTER, MOPUB, and the Bird logo are trademarks of Twitter, Inc. or its affiliates. All third party logos and trademarks included are the property of their respective owners.

© 2020 MoPub (a division of Twitter, Inc.)