我正在尝试在回收者视图中添加Facebook横幅广告。 我已经通过从本网站获得帮助实现了方法。 除了一种情况,一切都进行正常。 我在回收器视图的每2个项目之后显示一次广告。但是问题是,当我添加第一个项目时,它不会显示在位置1上。横幅广告应显示在位置0处,内容应显示在位置1处。先前内容的位置。
public class CarsAdapter extends RecyclerView.Adapter<CarsAdapter.MyViewHolderCar>{
private Context context;
private List<Cars> carsList;
private int AD_TYPE = 0;
private int CONTENT_TYPE = 1;
int lastPosition=-1;
private BillingHelper billingHelper;
public CarsAdapter(Context context, List<Cars> carsList) {
this.context = context;
this.carsList = carsList;
billingHelper=new BillingHelper(context);
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
@NonNull
@Override
public MyViewHolderCar onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
if(viewType==CONTENT_TYPE){
view=LayoutInflater.from(parent.getContext()).inflate(R.layout.car_details_row,parent,false);
}
else {
view = new AdView(context, context.getString(R.string.fb_banner), AdSize.BANNER_HEIGHT_50);
float density = parent.getContext().getResources().getDisplayMetrics().density;
int height = Math.round(AdSize.BANNER_HEIGHT_50.getHeight() * density);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT,height);
view.setLayoutParams(params);
if (billingHelper.shouldShowAds()) ((AdView) view).loadAd();
}
return new MyViewHolderCar(view);
}
@Override
public int getItemViewType(int position) {
if (position>=0&&position % 3 == 0) return AD_TYPE;
return CONTENT_TYPE;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolderCar holder, int position) {
if (position % 3 == 0)
return;
Cars note=carsList.get(position - Math.round((position / 3) + 1));
holder.id.setText(Html.fromHtml("•"));
holder.brandDetails.setText(note.getCarBrand());
holder.modelDetails.setText(Integer.toString(note.getCarModel()));
holder.plateDetails.setText(note.getCarNum());
holder.colorDetails.setBackgroundColor(Integer.parseInt(Integer.toString(note.getCarColor())));
AnimationsUtils.setScaleAnimation(holder.itemView);
}
@Override
public int getItemCount() {
int count=carsList.size();
return count+(count/3);
}
public class MyViewHolderCar extends RecyclerView.ViewHolder{
public TextView id;
public TextView brandDetails;
public TextView modelDetails;
public TextView plateDetails;
public TextView colorDetails;
public MyViewHolderCar(@NonNull View itemView) {
super(itemView);
id=itemView.findViewById(R.id.id);
brandDetails=itemView.findViewById(R.id.brandDetails);
modelDetails=itemView.findViewById(R.id.modelDetails);
plateDetails=itemView.findViewById(R.id.plateDetails);
colorDetails=itemView.findViewById(R.id.colorDetails);
}
}
}