public class MoPubAdAdapter
extends android.widget.BaseAdapter
MoPubAdAdapter facilitates placing ads into an Android ListView or
other widgets that use a ListAdapter.
For your content items, this class will call your original adapter with the original position of
content before ads were loaded.
This adapter uses a MoPubStreamAdPlacer object internally. If you
wish to avoid wrapping your original adapter, you can use MoPubStreamAdPlacer directly.| Constructor and Description |
|---|
MoPubAdAdapter(android.app.Activity activity,
android.widget.Adapter originalAdapter)
Creates a new MoPubAdAdapter object.
|
MoPubAdAdapter(android.app.Activity activity,
android.widget.Adapter originalAdapter,
MoPubNativeAdPositioning.MoPubClientPositioning adPositioning)
Creates a new MoPubAdAdapter object, using client positioning.
|
MoPubAdAdapter(android.app.Activity activity,
android.widget.Adapter originalAdapter,
MoPubNativeAdPositioning.MoPubServerPositioning adPositioning)
Creates a new MoPubAdAdapter object, using server positioning.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
areAllItemsEnabled() |
void |
clearAds()
Stops loading ads, immediately clearing any ads currently in the stream.
|
void |
destroy()
Destroys the ad adapter, preventing it from future use.
|
int |
getAdjustedPosition(int originalPosition)
Returns the position of an item considering ads in the stream.
|
int |
getCount()
Returns the number of items in your stream, including ads.
|
java.lang.Object |
getItem(int position)
For ad items, returns an ad data object.
|
long |
getItemId(int position)
For ad items, returns an ID representing the ad.
|
int |
getItemViewType(int position)
For ad items, returns a number greater than or equal to the view type count for your
underlying adapter.
|
int |
getOriginalPosition(int position)
Returns the original position of an item considering ads in the stream.
|
android.view.View |
getView(int position,
android.view.View view,
android.view.ViewGroup viewGroup)
For ad items, returns an ad View for the underlying position.
|
int |
getViewTypeCount()
Returns the view type count of your original adapter, plus the the number of possible view
types for ads.
|
boolean |
hasStableIds()
Returns the value returned by
hasStableIds() on your original adapter. |
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.
|
boolean |
isEmpty()
Returns whether the adapter is empty, calling through to your original adapter.
|
boolean |
isEnabled(int position) |
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 |
refreshAds(android.widget.ListView listView,
java.lang.String adUnitId)
Refreshes ads in the given ListView while preserving the scroll position.
|
void |
refreshAds(android.widget.ListView listView,
java.lang.String adUnitId,
RequestParameters requestParameters)
Refreshes ads in the given ListView while preserving the scroll position.
|
void |
registerAdRenderer(MoPubAdRenderer adRenderer)
Registers a
MoPubStaticNativeAdRenderer, supporting a specific native ad format,
to use when displaying ads in your stream. |
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 |
setOnClickListener(android.widget.ListView listView,
android.widget.AdapterView.OnItemClickListener listener)
Sets an on click listener for the given ListView, automatically adjusting the listener
callback positions based on ads in the adapter.
|
void |
setOnItemLongClickListener(android.widget.ListView listView,
android.widget.AdapterView.OnItemLongClickListener listener)
Sets an on long click listener for the given ListView, automatically adjusting the listener
callback positions based on ads in the adapter.
|
void |
setOnItemSelectedListener(android.widget.ListView listView,
android.widget.AdapterView.OnItemSelectedListener listener)
Sets an on item selected listener for the given ListView, automatically adjusting the
listener callback positions based on ads in the adapter.
|
void |
setSelection(android.widget.ListView listView,
int originalPosition)
Sets the currently selected item in the ListView, automatically adjusting the position based
on ads in the adapter.
|
void |
smoothScrollToPosition(android.widget.ListView listView,
int originalPosition)
Scrolls an item in the ListView, automatically adjusting the position based on ads in the
adapter.
|
public MoPubAdAdapter(android.app.Activity activity,
android.widget.Adapter originalAdapter)
MoPubAdAdapter(Context,
MoPubClientPositioning).activity - The activity.originalAdapter - Your original adapter.public MoPubAdAdapter(android.app.Activity activity,
android.widget.Adapter originalAdapter,
MoPubNativeAdPositioning.MoPubServerPositioning adPositioning)
activity - The activity.originalAdapter - Your original adapter.adPositioning - A positioning object for specifying where ads will be placed in your
stream. See MoPubNativeAdPositioning.serverPositioning().public MoPubAdAdapter(android.app.Activity activity,
android.widget.Adapter originalAdapter,
MoPubNativeAdPositioning.MoPubClientPositioning adPositioning)
activity - The activity.originalAdapter - Your original adapter.adPositioning - A positioning object for specifying where ads will be placed in your
stream. See MoPubNativeAdPositioning.clientPositioning().public final void registerAdRenderer(MoPubAdRenderer adRenderer)
MoPubStaticNativeAdRenderer, supporting a specific native ad format,
to use when displaying ads in your stream. Note that if multiple ad renderers support
a specific native ad format, the first one registered will be used.
This renderer will automatically create and render your view when you call getView(int, android.view.View, android.view.ViewGroup).adRenderer - The ad renderer.public final 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 refreshAds(ListView, String). 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 boolean isAd(int position)
true only if there is an ad loaded for this position. You can also
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()
refreshAds(ListView, String, RequestParameters) instead of this
method.
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 boolean areAllItemsEnabled()
areAllItemsEnabled in interface android.widget.ListAdapterareAllItemsEnabled in class android.widget.BaseAdapterpublic boolean isEnabled(int position)
isEnabled in interface android.widget.ListAdapterisEnabled in class android.widget.BaseAdapterpublic int getCount()
public java.lang.Object getItem(int position)
public long getItemId(int position)
false from #hasStableIds().public boolean hasStableIds()
hasStableIds() on your original adapter.hasStableIds in interface android.widget.AdapterhasStableIds in class android.widget.BaseAdapterpublic android.view.View getView(int position,
android.view.View view,
android.view.ViewGroup viewGroup)
public int getItemViewType(int position)
getItemViewType in interface android.widget.AdaptergetItemViewType in class android.widget.BaseAdapterpublic int getViewTypeCount()
getViewTypeCount in interface android.widget.AdaptergetViewTypeCount in class android.widget.BaseAdapterpublic boolean isEmpty()
isEmpty in interface android.widget.AdapterisEmpty in class android.widget.BaseAdapterpublic int getOriginalPosition(int position)
position - The adjusted position.MoPubStreamAdPlacer#getOriginalPosition(int)}public int getAdjustedPosition(int originalPosition)
originalPosition - The original position.MoPubStreamAdPlacer#getAdjustedPosition(int)}public void insertItem(int originalPosition)
originalPosition - The original content position at which to add an item. If you have an
adjusted position, you will need to call getOriginalPosition(int) to get this value.MoPubStreamAdPlacer#insertItem(int)}public void removeItem(int originalPosition)
originalPosition - The original content position at which to add an item. If you have an
adjusted position, you will need to call getOriginalPosition(int) to get this value.MoPubStreamAdPlacer#removeItem(int)}public void setOnClickListener(android.widget.ListView listView,
android.widget.AdapterView.OnItemClickListener listener)
listView - The ListView for this adapter.listener - An on click listener.public void setOnItemLongClickListener(android.widget.ListView listView,
android.widget.AdapterView.OnItemLongClickListener listener)
listView - The ListView for this adapter.listener - An an long click listener.public void setOnItemSelectedListener(android.widget.ListView listView,
android.widget.AdapterView.OnItemSelectedListener listener)
listView - The ListView for this adapter.listener - An an item selected listener.public void setSelection(android.widget.ListView listView,
int originalPosition)
listView - The ListView for this adapter.originalPosition - The original content position before loading ads.public void smoothScrollToPosition(android.widget.ListView listView,
int originalPosition)
listView - The ListView for this adapter.originalPosition - The original content position before loading ads.public void refreshAds(android.widget.ListView listView,
java.lang.String adUnitId)
loadAds(String) in order to preserve the scroll position in
your list.adUnitId - The ad unit ID to use when loading ads.public void refreshAds(android.widget.ListView listView,
java.lang.String adUnitId,
RequestParameters requestParameters)
loadAds(String, RequestParameters) in order to preserve the
scroll position in your list.adUnitId - The ad unit ID to use when loading ads.requestParameters - Targeting information to pass to the ad server.