1
votes

I want to show admob ads after some notice board items. So I changed recyclerview to use two viewholder and show adview if the position is in noticeitem's end. But it shows item on the first list of board item view and can't see the first item of the board

I tried to add first item to position+1 and it shows two items

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
       private RecyclerView recyclerView;
       private Context context;
       private ArrayList<stTalkListContentsData> talkArrayList = new ArrayList<>();
       private ViewGroup parent;
       private int mNoticeCount = 0;

       public RecyclerAdapter(Context context, RecyclerView recyclerView, ArrayList<stTalkListContentsData> talkArrayList) {
           this.context = context;
           this.recyclerView = recyclerView;
           this.talkArrayList = talkArrayList;
       }

       public class AdViewHolder extends RecyclerView.ViewHolder {
           private AdView adView;
           AdViewHolder(View view) {
               super(view);
               adView = view.findViewById(R.id.adView);

           }
       }

       @Override
       public int getItemViewType(int position) {
           if (position == mNoticeCount) {
               return Define.AD_TYPE;
           } else {
               return Define.CONTENT_TYPE;
           }
       }
 @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ViewHolder viewHolder = null;
            switch (viewType) {
                case Define.CONTENT_TYPE:
                    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_board_title_list, parent, false);
                    return new ViewHolder(v);
                case Define.AD_TYPE:
                    default:
                    View bannerAdView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_board_adview, parent, false);
                    return new AdViewHolder(bannerAdView);
            }
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            int type = getItemViewType(position);
            switch (type){
                case AD_TYPE:
                    AdViewHolder bannerHolder = (AdViewHolder) holder;
                    AdView adView = ((AdViewHolder) holder).adView;
                    ViewGroup adCardView = (ViewGroup) bannerHolder.itemView;
                    if (adCardView.getChildCount() > 0) {
                        adCardView.removeAllViews();
                    }
                    if (adView.getParent() != null) {
                        ((ViewGroup) adView.getParent()).removeView(adView);
                    }
                    AdRequest adRequest =  new AdRequest.Builder().build();
                    adView.loadAd(adRequest);
                    adCardView.addView(adView);
                    break;
                case CONTENT_TYPE:{
                    stTalkListContentsData msgitem = talkArrayList.get(position);

                    JSAppNoticeContentsData talk_notice_info = msgitem.getTalkNoticeInfo();
                    JSBoardInfoData talk_board_info = msgitem.getTalkListBoardInfo();
                    ViewHolder talkholder = (ViewHolder) holder;

                   ....

                    talkholder.bind(position);
                }
                    break;
            }
        }

        @Override
        public int getItemCount() {
            return this.talkArrayList.size();
        }
    }
1

1 Answers

1
votes

The getItemCount() function of adapter should return talkArrayList.size()+1

 @Override
    public int getItemCount() {
        return this.talkArrayList.size()+1;
    }

Also in onBindViewHolder you cannot directly use position to bind data as the recyclerview holds ad item at some position which is not in the talkArrayList. If the position is before mNoticeCount use talkArrayList.get(position) to get msgItem or use talkArrayList.get(position-1), that is

 case CONTENT_TYPE:{
                stTalkListContentsData msgitem;
                    if(position < mNoticeCount ){
                msgitem = talkArrayList.get(position);
                   }else {
                msgitem = talkArrayList.get(position-1);
               }


                JSAppNoticeContentsData talk_notice_info = msgitem.getTalkNoticeInfo();
                JSBoardInfoData talk_board_info = msgitem.getTalkListBoardInfo();
                ViewHolder talkholder = (ViewHolder) holder;

               ....

                talkholder.bind(position);
            }
                break;