我试着按照谷歌的教程,但事情只是没有解决。我在RecyclerViewAdapter中出错。

错误如下
RecyclerViewAdapter。 (在第9行中进行了评论)

@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {

int viewType = getItemViewType(position);

switch (viewType){

    case AD_VIEW_TYPE:
        NativeExpressAdViewHolder nativeExpressHolder = (NativeExpressAdViewHolder)holder;
        NativeExpressAdView adView = (NativeExpressAdView)arrayList.get(Position); //Inconvertible types; cannot cast 'app.modals.cardSetterGetter' to 'com.google.android.gms.ads.NativeExpressAdView

        ViewGroup adCardView = (ViewGroup)nativeExpressHolder.itemView;
        adCardView.removeAllViews();

        if(adView.getParent() != null){
            ((ViewGroup)adView.getParent()).removeView(adView);
        }

        adCardView.addView(adView);
        break;

    case MENU_ITEM_VIEW_TYPE:

    default:
        String card_image;
        CardSetterGetter cardSetterGetter;
        cardSetterGetter = arrayList.get(position);

        card_image = constants.SERVERIP.concat(cardSetterGetter.getImageurl());
        holder.cardSetterGetter = cardSetterGetter;
        holder.title.setText(cardSetterGetter.getImagetitle()); //set title

        cardSetterGetter.getTag();

        PointF focusPoint = new PointF(0.5f, 0f);
        // your app populates the focus point

        holder.image // set image
                .getHierarchy()
                .setActualImageFocusPoint(focusPoint);
        holder.image.setImageURI(Uri.parse(card_image));
}

}

最佳答案

您不应将视图添加到listItems
listItems包含项目,而不是视图。
NativeExpressAdViewHolder应该扩展RecyclerViewHolder
holder.getPosition()是排名,而不是adView。无法将其强制转换为NativeExpressAdView
如果您仍然希望将adViews保留在项目列表中,则应按照示例中的说明更改itemsList的类型。 private final List<Object> mRecyclerViewItems;


不是List<app.appname.CardSetterGetter>

关于android - 如何在RecyclerView中实现Admob原生广告?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46477547/

10-12 04:39