我想在布告栏上显示一些admob广告。因此,我将recyclerview更改为使用两个viewholder,并在位置位于noticeitem末端的情况下显示adview。但这会在木板项目视图的第一列表中显示该项目,而看不到木板的第一项
我尝试将第一个项目添加到位置+1,其中显示了两个项目
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();
}
}
答案 0 :(得分:0)
适配器的getItemCount()函数应返回talkArrayList.size()+ 1
@Override
public int getItemCount() {
return this.talkArrayList.size()+1;
}
在onBindViewHolder中,您也不能直接使用位置绑定数据,因为recyclerview将广告项保存在talkArrayList之外的某个位置。 如果位置在mNoticeCount之前,请使用talkArrayList.get(position)获取msgItem或使用talkArrayList.get(position-1),即
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;