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();
}
}