如何在RecyclerView中实施Admob原生广告?

时间:2017-09-28 20:13:39

标签: android android-recyclerview admob recycler-adapter google-admob

我尝试过谷歌的教程,但事情并没有成功。我在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));
}

}

1 个答案:

答案 0 :(得分:1)

  1. 您不应将视图添加到listItemsNativeExpressAdViewHolder包含项目,而非视图。

  2. RecyclerViewHolder应扩展holder.getPosition()

  3. NativeExpressAdView是一个位置,而不是adView。它无法转换为private final List<Object> mRecyclerViewItems;

  4. 如果您仍想在项目列表中保留adView,则应更改itemsList的类型,如示例中所述。 List<app.appname.CardSetterGetter>

  5. 不是testView : Html Msg testView = div [] [ button [onClick TestAction] [text "Test Action"] ]