如何在recyclerview中放置横幅

时间:2018-09-18 16:34:03

标签: java android android-recyclerview admob

我能够将横幅放到recylerview中,但是它与其中一项重叠,取代了其中一项,而不是进入它们之间,有人知道如何解决吗?

我要拉一个api,不知道这有什么区别。我搜索了一些教程,但没有找到,我需要做的是添加admob本机横幅。

public class HistoricoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

List<Capitulos> capitulos;
Context context;
private static final int DEFAULT_VIEW_TYPE = 0;
private static final int NATIVE_AD_VIEW_TYPE = 1;

public HistoricoAdapter(List<Capitulos> capitulos, Context context) {
    this.capitulos = capitulos;
    this.context = context;
}

@Override
public int getItemViewType(int position) {
    if (position>1 && position % 3 == 0) {
        return NATIVE_AD_VIEW_TYPE;
    }
    return DEFAULT_VIEW_TYPE;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    switch (viewType) {
        default:
            view = layoutInflater
                    .inflate(R.layout.row_historico, parent, false);
            return new MyViewHolder(view);
        case NATIVE_AD_VIEW_TYPE:
            view = layoutInflater.inflate(R.layout.ad_unified, parent, false);
            return new NativeAdViewHolder(view);
    }
}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (!(holder instanceof MyViewHolder)) {
        return;
    }
    MyViewHolder holder2 = (MyViewHolder) holder;

    Capitulos l = capitulos.get(position);

    int aInt = Integer.parseInt(l.getTempo());
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(aInt * 1000L);
    String dataFormato = android.text.format.DateFormat.format("dd-MM-yyyy HH:mm:ss", cal).toString();

    SpannableString ultimoLido = new SpannableString("Último lido: " + l.getCapitulo());
    ultimoLido.setSpan(new StyleSpan(Typeface.BOLD), 0, "Último lido: ".length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    SpannableString data = new SpannableString("Data: " + dataFormato);
    data.setSpan(new StyleSpan(Typeface.BOLD), 0, "Data: ".length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    holder2.titulo.setText(l.getTitle());
    holder2.ultimoLido.setText(ultimoLido);
    holder2.data.setText(data);
    Picasso.get()
            .load("http://unionmangas.site/assets/uploads/mangas/" + l.getCapa())
            .into(holder2.capaView);

}


@Override
public int getItemCount() {
    return capitulos.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView titulo;
    TextView ultimoLido;
    TextView data;
    ImageView capaView;

    public MyViewHolder(View itemView) {
        super(itemView);

        titulo = itemView.findViewById(R.id.tituloHistoricoViewID);
        ultimoLido = itemView.findViewById(R.id.ultimosLidosViewID);
        data = itemView.findViewById(R.id.dataViewID);
        capaView = itemView.findViewById(R.id.capaHistoricoViewID);

    }

}

public class NativeAdViewHolder extends RecyclerView.ViewHolder {

    public AdView mAdView;
    public NativeAdViewHolder(View view) {
        super(view);
        mAdView = (AdView) view.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .build();
        mAdView.loadAd(adRequest);
    }
}}

1 个答案:

答案 0 :(得分:1)

  

编辑答案以忽略横幅点击事件

问题与getItemCount一起出现。在项目之间添加广告时,您还需要增加计数。这样,项目的数量就不会被广告项目所取代。

public static final int ITEM_PER_AD = 3;
@Override
public int getItemCount() {
    int itemCount = capitulos.size();
    itemCount += itemCount / ITEM_PER_AD ;
    return itemCount;
}

因此,您还需要排除广告排名,以获取原始项目的排名。

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (!(holder instanceof MyViewHolder)) {
        return;
    }
    int itemPosition = position - position / ITEM_PER_AD ; // need to adjust to get the list item position excluding ads
}

下面的代码将忽略广告的点击事件。

@Override
public void onItemClick(View view, int position) {
      if (position>1 && position % ITEM_PER_AD == 0) { 
            return; 
      }
      // rest of the code keep as it is.
}