-
Notifications
You must be signed in to change notification settings - Fork 6
Advanced integration of AdInline ListView
For more complicated implementation like ListView, a view should always have a unique ID and use a different banner class, which is AdInlineLW
.
When implementing an ad into ListView you should NOT pass in events such as onReturn
, onPause
, destroy
like in normal view.
First of all, we need to do a basic ListView implementation. If you already have done this, you can skip this step.
To use a ListView
we need to declare it in an XML layout.
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Then we need an adapter that will populate its items. We will be using an adapter from Sample project TestAdapter
. Its projected to hold two kinds of views, a mock one and an ad view.
public static class TestAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;
private final TestAdapterListener adapterListener;
public TestAdapter(Context context, ArrayList<String> values, TestAdapterListener l) {
super(context, R.layout.lw_empty, values);
this.context = context;
this.values = values;
this.adapterListener = l;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = convertView;
// Determines which type of view to use.
if (getItemViewType(position) == 1) {
// In this case we use AdView. If no instance is returned from scrap list, we initialize a new one
if ((rowView != null && !(rowView.getTag() instanceof ViewAdHolder)) || rowView == null) {
rowView = adapterListener.inflateCustomView();
ViewAdHolder viewHolder = new ViewAdHolder();
rowView.setTag(viewHolder);
}
ViewAdHolder holder = (ViewAdHolder) rowView.getTag();
} else {
// We initialize a simple view if no instance is returned from the scrap list.
if ((rowView != null && !(rowView.getTag() instanceof ViewTextHolder)) || rowView == null) {
rowView = inflater.inflate(R.layout.lw_empty, parent, false);
ViewTextHolder viewHolder = new ViewTextHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.text_view);
// We set a view holder as a tag for later reuse
rowView.setTag(viewHolder);
}
// Initialized anew or returned from scrap list we assign needed values.
ViewTextHolder holder = (ViewTextHolder) rowView.getTag();
holder.text.setText(values.get(position));
}
return rowView;
}
// Determines the case that defines which type of view should be used
// In this case the interface returns the case from the outside
@Override
public int getItemViewType(int position) {
return adapterListener.getCustomViewType(position);
}
// Determines different type of views to use
// In this case we use 2 types, one for ad and one for simple view
@Override
public int getViewTypeCount() {
return 2;
}
// Instance holder that is used when reusing the *same* view but assigning different values
static class ViewTextHolder {
public TextView text;
}
static class ViewAdHolder {
}
public interface TestAdapterListener {
public int getCustomViewType(int position);
public View inflateCustomView();
}
}
Then we have to get ListView instance, and provide it with an adapter.
// Initializing adapter that will be used with the listview
TestAdapter adapter = new TestAdapter(this, templateList, new TestAdapter.TestAdapterListener() {
@Override
public int getCustomViewType(int position) {
// We define the adapter to display each 5th item to be an ad...
if (position % 5 == 0)
return 1;
// ...otherwise its a simple list item
return 0;
}
@Override
public View inflateCustomView() {
// Use builder to initialize ad view that will be used within the ListView
return new View(AdInlineLWActivity.this);
}
});
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
Now to implement AdInline
view, you have to provide the view that will be used in the adapter. To do this, you can use a helper class AdListViewItemBuilder.init(Context, masterTagId, AdSize(int, int), debugMode)
. Now the adapter implementation should look like this:
TestAdapter adapter = new TestAdapter(this, templateList, new TestAdapter.TestAdapterListener() {
@Override
public int getCustomViewType(int position) {
// We define the adapter to display each 5th item to be an ad...
if (position % 5 == 0)
return 1;
// ...otherwise its a simple list item
return 0;
}
@Override
public View inflateCustomView() {
// Use builder to initialize ad view that will be used within the ListView
return AdListViewItemBuilder.init(AdInlineLWActivity.this, 3987055, new AdSize(320, 50), true);
}
});
Basic integrations
- Integrating Inline Ad
- Integrating Full Screen Overlay Ad
- Integrating Interstitial Ad
- Integrating Adhesion Ad
- Video Ad Integration
Advanced integrations
- Advanced integration of Inline Ad
- Advanced integration of Full Screen Overlay Ad
- Advanced integration of Interstitial Ad
- Advanced integration of Adhesion Ad
- Advanced integration of AdInline ListView
- Advanced integration of AdInline RecyclerView
- Instream video ads integration
Other
- Adding Custom Values
- Adding Keywords
- Adding Key Value Pairs
- Adding Search Words
- Location
- Security
- Ad Tags
- Header Bidding
- Changing ADX domain
- Specifying banner loading behaviour
- GDPR
- Logs
Mediation adapters
Plugins