MoPubRecyclerAdapter

public final class MoPubRecyclerAdapter

Contains methods to request native ads for a stream (a RecyclerView). For the manual native ad integration, refer to the MoPubNative.java class and to our documentation.

Public Methods

public void /clearAds()

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

  • Parameters: None.

public void /destroy()

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

  • Parameters: None.

public int /getAdjustedPosition(int originalPosition)

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

  • Parameters:

    • originalPosition: An integer indicating the original cell index.

public int /getItemCount()

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

  • Parameters: None.

public long /getItemId(int position)

  • Description: Returns a stable negative item ID for ad items, and calls getItemId on the original adapter for non-ad items.

  • Parameters:

    • position: An integer representing the position of the desired item within the adapter’s data set.

public int /getItemViewType(int position)

  • Description: Returns the type of View that will be created for the specified item.

  • Parameters:

    • position: An integer representing the position of the desired item within the adapter’s data set.

public int /getOriginalPosition(int position)

  • Description: Returns the original position of an item in the stream prior to the insertion of ads in the stream. For example, in this stream: {Item0, Ad, Item1, Item2, Ad, Item3}, calling getOriginalPosition(5) returns ‘3’.

  • Parameters:

    • position: An integer representing the adjusted position. For example, in this stream: {Item0, Ad, Item1, Item2, Ad, Item3}, calling getOriginalPosition(5) returns ‘3’.

public boolean /isAd(int position)

  • Description: Whether the given position is an ad. Returns true if ad (only if there is an ad loaded for this position); returns 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)

  • Description: Starts loading ads from the MoPub server.

  • Parameters:

    • adUnitId: MoPub ad unit ID String.

public void /loadAds(String adUnitId, RequestParameters requestParameters)

  • Description: Starts loading ads from the MoPub server, using the given request targeting information.

  • Parameters:

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

public void /refreshAds(String adUnitId)

  • Description: Manually sends out a new ad request, while preserving the scroll position. Used to issue a new ad request and start a new refresh cycle.

  • Parameters:

    • adUnitId: MoPub ad unit ID String.

public void /refreshAds(String adUnitId, RequestParameters requestParameters)

  • Description: Manually sends out a new ad request with additional targeting information, while preserving the scroll position. Used to issue a new ad request and start a new request cycle. RequestParameters is an object for specifying targeting data such as keywords, location, and customer ID.

  • Parameters:

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

public void /registerAdRenderer(MoPubAdRenderer adRenderer)

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

  • Parameters:

public void /setAdLoadedListener(MoPubNativeAdLoadedListener listener)

  • 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.

Note that there is not a one-to-one correspondence between calls to loadAds() and this listener. The SDK will call the listener every time an ad loads.

  • Parameters:

    • listener: An instance of MoPubNativeAdLoadedListener that tracks when ads are loaded (onAdLoaded) and when ads are removed (onAdRemoved) from the stream.

public void /setContentChangeStrategy(MoPubRecyclerAdapter.ContentChangeStrategy strategy)

  • Description: Sets the strategy this adapter should use for moving ads when content is added or removed from the wrapped original adapter. This strategy can be set at any time to change the behavior of the adapter.

  • Parameters:

    • strategy:
      • INSERT_AT_END will insert ads when content is added to the end of the stream. This is the default behavior and the recommended strategy.
      • MOVE_ALL_ADS_WITH_CONTENT will cause all ad positions after an insertion or deletion to be adjusted. New ads will not be displayed when items are added to the end of the stream.
      • KEEP_ADS_FIXED will never adjust ad positions when items are inserted or removed.

Last updated March 03, 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.)