Created
May 13, 2014 12:45
-
-
Save BartoszJarocki/b4b337c3df8346c2db45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.smartsoft.library.adapters; | |
import android.app.Activity; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import com.google.android.gms.ads.AdRequest; | |
import com.google.android.gms.ads.AdSize; | |
import com.google.android.gms.ads.AdView; | |
/** | |
* Shows ads every K items in ListView | |
*/ | |
public class AdViewForListViewWrapperAdapter extends BaseAdapter { | |
private static final int SHOW_AD_EVERY_K_ITEMS = 5; | |
private final Activity activity; | |
private final BaseAdapter delegate; | |
private final String adMobId; | |
public AdViewForListViewWrapperAdapter(Activity activity, BaseAdapter delegate, String adMobId) { | |
this.activity = activity; | |
this.delegate = delegate; | |
this.adMobId = adMobId; | |
} | |
@Override | |
public int getCount() { | |
// Total count includes list items and ads. | |
return delegate.getCount(); | |
} | |
@Override | |
public Object getItem(int position) { | |
// Return null if an item is an ad. Otherwise return the delegate item. | |
if (isItemAnAd(position)) { | |
return null; | |
} | |
return delegate.getItem(getOffsetPosition(position)); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if (isItemAnAd(position)) { | |
if (convertView instanceof AdView) { | |
return convertView; | |
} else { | |
AdView adView = new AdView(activity); | |
adView.setAdSize(AdSize.BANNER); | |
adView.setAdUnitId(adMobId); | |
AdRequest adRequest = new AdRequest.Builder() | |
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) | |
.build(); | |
adView.loadAd(adRequest); | |
return adView; | |
} | |
} else { | |
return delegate.getView(getOffsetPosition(position), convertView, parent); | |
} | |
} | |
@Override | |
public int getViewTypeCount() { | |
return delegate.getViewTypeCount() + 1; | |
} | |
@Override | |
public int getItemViewType(int position) { | |
if (isItemAnAd(position)) { | |
return delegate.getViewTypeCount(); | |
} else { | |
return delegate.getItemViewType(getOffsetPosition(position)); | |
} | |
} | |
@Override | |
public boolean areAllItemsEnabled() { | |
return false; | |
} | |
@Override | |
public boolean isEnabled(int position) { | |
return (!isItemAnAd(position)) && delegate.isEnabled(getOffsetPosition(position)); | |
} | |
private boolean isItemAnAd(int position) { | |
return (position % SHOW_AD_EVERY_K_ITEMS) == 0; | |
} | |
private int getOffsetPosition(int position) { | |
return position; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment