Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to fix this : showing ads in recycleview hide some articles in rss entries

abdellahamriabdellahamri USMember ✭✭
edited October 2018 in Xamarin.Android

I'm trying to show ads in recycleview and i succeeded to do it using the code below .. the problem is that in every "MspaceBetweenAds" position the ad show up but the article at this replaced with the ad
i tried to fix it by modifying ItemCount() by Mposts.Count + (Mposts.count% MspaceBetweenAds) but i'm getting "IndexOutOfBounds " error
any help please .. this is my code

 public class AdsView : ListViewHolder
        {
            public AdView mAdView { get; private set; }
            public AdsView(View view) : base(view)
            {
               mAdView = view.FindViewById<AdView>(Resource.Id.AdsCard);
            }
        }

 public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            RecyclerView.ViewHolder vh = null;
            switch (viewType)
            {
                case 1:
                    View vBig = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.BigCard, parent, false);
                    vh = new MyView(vBig);
                    break;
                case 2:                  
                    View vAds = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.AdsCard, parent, false);
                    vh = new AdsView(vAds);
                    break;
            }
            return vh;
        } 
 public override void OnBindListViewHolder(ListViewHolder holder, int position)
        {
            var MyHolder = holder as MyView;
            switch (holder.ItemViewType)
            {
                case 1:
                    // code to show posts articles here
                    break;
                case 2:                 
                    var AdHolder = holder as AdsView;
                    fnc.AddBannerAd(AdHolder.mAdView);
                    break;
            }
        }
public override int GetItemViewType(int position)
        {
            if (position > 0 && position % mSpaceBetweenAds  == 0) { return 2; }
            else { return 1; }
        }

Answers

Sign In or Register to comment.