How to display AdView at every nth position in listView

I have the code below that works, except that it always hides at least one real item in the list, because the ad is displayed at that position.

Example of a problem: I have a list of 4 times, and adView is displayed in position 3. In the view list, I see only 3 times and AdView, the 4th element will not be displayed

I played with an increase in adapter size every time I return an ad, but it doesn’t work very well.

Any ideas?

public View getView(final int position, View row, ViewGroup parent) {
        MyHolder holder = null;
        boolean showAd = proVersion == false && (position % 8 == k);
        if (showAd) {
            AdView adView = adList.get(position);
            if (adView == null) {
                AdView adViewNew = new AdView((Activity) context, AdSize.BANNER, context.getResources().getString(
                        R.string.adId));
                adViewNew.loadAd(Utils.getAdRequest("gps", lat, lng, keywords));
                adList.add(position, adViewNew);
                return adViewNew;
            } else {
                return adView;
            }
        } else if (row == null || row instanceof AdView) {
            LayoutInflater inflater = ((SherlockActivity) context).getLayoutInflater();
            row = inflater.inflate(viewResourceId, parent, false);
            holder = new MyHolder();
            holder.textName = (TextView) row.findViewById(R.id.name);           
            row.setTag(holder);
        } else {
            holder = (MyHolder) row.getTag();
        }
        holder.textName.setText(items.get(position).getName());

        // more code

        return row;
    }

    @Override
    public int getCount() {
        if (items != null) {
            return items.size();
        } else {
            return 0;
        }
    }
+4
source share
8 answers

. - listView .

  • getViewTypeCount() - , View
  • getItemViewType (int position) , . , listView.

, : , " ".

, ( , , ).

, items.size() items.size() + Math.floor(items.size()/NTH_ITEM), , (% NTH_ITEM == 0) , Math.floor(position/NTH_ITEM), .

ListView , .

, , , , type, , " ", ( , ).

, - .

+6

. .

.

:

private class ItemWrapper {
               public static final int TYPE_NORMAL = 0;
               public static final int TYPE_AD = 1;
               public static final int TYPE_COUNT = 2; 

                public ListObject item;
                public AdObject adItem;
                public int type;

                public ItemWrapper(ListObject item) {
                    this.type = TYPE_NORMAL;
                    this.item = item
                }
                public ItemWrapper(AdObject adItem) {
                    this.type = TYPE_AD;
                    this.adItem = adItem;  
                }
}

:

,

 public class MyAdapter extends BaseAdapter{
    ArrayList<ItemWrapper> mWrappedItems = new ArrayList<ItemWrapper>();
         public void MyAdapter(List<ListObject> items,AdItem adItem){

                for(ListObject item:items){
                    mWrappedItems.add(new ItemWrapper(item));
                }
                //you can insert your ad item wherever you want!
                mWrappedItems.add(2,new ItemWrapper(adItem)); 

            }
}

:

@Override
   public ItemWrapper getItem(int position) {
      return mWrappedItems == null ? null : mWrappedItems.get(position);
   }
   @Override
    public int getItemViewType(int position) {
        return getItem(position).type;
    }

    @Override
    public int getViewTypeCount() {
        //tells the adapter how many different view types it will need to recycle
        return ItemWrapper.TYPE_COUNT;
    }

getView.... , , , .

public View getView(final int position, View row, ViewGroup parent) {
        final ItemWrapper item = getItem(position);
        final int type = item.type;
        if (row == null) {
            if (type == ItemWrapper.TYPE_NORMAL) {
                //inflate your normal view layouts
            } else if (type == ItemWrapper.TYPE_AD) {
                //inflate your ad layout
            }
        }

        //now you can easily populate your views based on the type
        if (type == ItemWrapper.TYPE_NORMAL) {
            //get your item data
            final ListObject data = item.item;
            //populate as you would normally
        } else if (type == ItemWrapper.TYPE_AD) {
            final AdItem adItem = item.adItem;
            //populate your ad as needed
        }
        return row;
    }

, , / , , - getView(); . , ! P.S : http://qtcstation.com/2012/04/a-limitation-with-the-viewholder-pattern-on-listview-adapters/

+2

-, . , ( View row getView()) , LinearLayout, AdView.

, , , , .

, :

<LinearLayout
  ...>

  <!-- Put here the AdView layout -->
  <LinearLayout
    android:id="@+id/adViewLL"
    ... 
    android:visibility="gone" />

  <!-- Add whatever else you need in your default layout -->
  ...
</LinearLayout>

getView(), VISIBLE, , View AdView.

public View getView(final int position, View row, ViewGroup parent) {
  MyHolder holder = null;
  boolean showAd = proVersion == false && (position % 8 == k);

  if (row == null) {
    LayoutInflater inflater = ((SherlockActivity) context).getLayoutInflater();
    row = inflater.inflate(viewResourceId, parent, false);
    holder = new MyHolder();
    holder.textName = (TextView) row.findViewById(R.id.name);           
    row.setTag(holder);

    if (showAd) {
      LinearLayout myAd = (LinearLayout) row.findViewByid(R.id.adViewLL);
      myAd.setVisibility(View.VISIBLE);

      AdView adViewNew = new AdView((Activity) context, AdSize.BANNER, context.getResources().getString(R.string.adId));
      ...
      myAd.addView(adViewNew);
    }
  } else {
    holder = (MyHolder) row.getTag();
  }

  holder.textName.setText(items.get(position).getName());

  // more code

  return row;
}
+1

nKn :

  • ViewSwitcher
  • , , - : " ..."
  • (onAdLoaded), holder.switcher.setDisplayedChild(1); // if 1 is where your AdView is.
  • , , , (showAd && adsEnabled) {}, , , API, .
  • # 4 else: else {holder.switcher.setDisplayedChild(0);//your generic message}
  • "AdController", , ( ## , ). if (YourAdController.isAd(position) && adsEnabled) { do the add thing }
0

.

, .

, 8x

(.. , ) , 8x

,

, getview

if(ad item)
    populate adView
else
    populate regularItemView

,

0

, . , .

  • -, , .

    AdUnitDetails {   private String mUnitId;   private int mIndex;

    public AdUnitDetails(String mUnitId,int mIndex) {
        super();
        this.mUnitId = mUnitId;
        this.mIndex = mIndex;
    }
    
    public String getUnitId() {
        return mUnitId;
    }
    
    public void setUnitId(String mUnitId) {
        this.mUnitId = mUnitId;
    }
    
    
    public int getIndex() {
        return mIndex;
    }
    
    public void setIndex(int mIndex) {
        this.mIndex = mIndex;
    }
    

    }

  • Sparsearray

    SparseArray<AdView> mAdViewsMap = new SparseArray<AdView>();
    
  • , , . (item, item, item, addetails, item, item ..).

  • . :

    private static final int TYPE_ITEM= 0;
    private static final int TYPE_AD = 1;
    private static final int TYPES_COUNT = TYPE_AD + 1;
    
    @Override
    public int getViewTypeCount() {
        return TYPES_COUNT;
    }
    
    @Override
    public int getItemViewType(int position) {
        if (mItems.get(position) instanceof YourItemClass)
            return TYPE_ITEM;
        else if (mItems.get(position) instanceof AdUnitDetails)
            return TYPE_AD;
    return super.getItemViewType(position);
    }
    
  • getView()

        View view = convertView;
        if (mItems.get(position) instanceof YourItemClass) {
            // Handle your listview item view
        } else if (mItems.get(position) instanceof AdUnitDetails) {
            return getAdView(position);
        }
    
        return view;
    
  • getAdView()

     private AdView getAdView(final int position) {
    if (mAdViewsMap.get(position) == null) {
        AdSize adSize = getAdSize(AdSize.BANNER);
        AdView mAdView = new AdView(mContext, adSize, ((AdUnitDetails) mItems.get(position)).getUnitId());
        mAdView.loadAd(new AdRequest());
        mAdViewsMap.put(position, mAdView);
        return mAdView;
    } else {
        return mAdViewsMap.get(position);
    }
    

    }

0

, , . int () , (3 || 6 || 9), 3- . , , :)  oh || "" -

0
source

here is the code Ashish Kumar Gupta

RelativeLayout rl = (RelativeLayout) convertView.findViewById(R.id.adviewlayout); //putting the advertisement banner inside a relativelayout/linearlayout if you want. so that you can make it visibile whenever you want.

    String pos = String.valueOf(position); // converting listview item position from int to string//


    if (pos.endsWith("3")||pos.endsWith("6")||pos.endsWith("9")) {
        // every fourth position will show an advertisement banner inside a listview. here is the proof (0,1,2,advertisement,4,5,advertisement,7,8,advertisement,10,11,12,advertisement,14,15,advertisement,17,18,advertisement,20). did not put advertisements in 0 position cuz its rude to put advertisements in like the first layout in my POV//

        rl.setVisibility(View.VISIBLE);

    }
    else 
    {
        rl.setVisibility(View.GONE);

// if his advertisement in the 3rd position will not be displayed //} you can :)

0
source

Source: https://habr.com/ru/post/1529289/


All Articles