Recyclerview - Facebook 原生广告 - 不可点击

Posted

技术标签:

【中文标题】Recyclerview - Facebook 原生广告 - 不可点击【英文标题】:Recyclerview - Facebook Native Ads - Not clickable 【发布时间】:2017-12-19 16:41:43 【问题描述】:

我正在RecyclerView 中实施 Facebook 原生广告。广告加载正常,但广告不可点击。我的RecyclerView 中的其他一般项目是可点击的,因为我为它们实现了OnClickListener。如何让 Facebook 广告可点击?有人可以帮我解决这个问题吗?

这是我的代码:

 private class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> 

    private static final int VIEW_ITEM_TYPE = 0;
    private static final int VIEW_FACEBOOK_AD_TYPE = 1;

    Context context;

    public RecyclerViewAdapter(Context context) 
        this.context = context;
    


    @Override
    public int getItemViewType(int position) 

        if (listItems.get(position).isAd())
            return VIEW_FACEBOOK_AD_TYPE;
        else
            return VIEW_ITEM_TYPE;
    

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 

        if (viewType == VIEW_ITEM_TYPE) 
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_recyclerview, parent, false);
            return new CustomViewHolder(v);
         else if (viewType == VIEW_FACEBOOK_AD_TYPE) 
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_recyclerview_dashboard_fb_ad, parent, false);
            return new FacebookAdViewHolder(v);
        

        return null;
    

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) 

        Video video = listItems.get(position);

        if (video.isAd()) 

            FacebookAdViewHolder facebookAdViewHolder = (FacebookAdViewHolder) holder;

            View adView = NativeAdView.render(context, nativeAd, NativeAdView.Type.HEIGHT_300);

            List<View> clickableViews = new ArrayList<>();
            clickableViews.add(adView);
            clickableViews.add(facebookAdViewHolder.nativeAdContainer);

            nativeAd.registerViewForInteraction(facebookAdViewHolder.nativeAdContainer, clickableViews);
            facebookAdViewHolder.nativeAdContainer.addView(adView);
         else 
            CustomViewHolder customViewHolder = (CustomViewHolder) holder;
            Glide.with(context).load(URL_PART_1 + video.getVideoId() + URL_PART_2).into(customViewHolder.imageView);
            customViewHolder.textViewTitle.setText(video.getTitle());
        
    

    @Override
    public int getItemCount() 
        return listItems == null ? 0 : listItems.size();
    

    private class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener 

        ImageView imageView;
        TextView textViewTitle;

        public CustomViewHolder(View itemView) 
            super(itemView);
            itemView.setOnClickListener(this);
            imageView = itemView.findViewById(R.id.imageView);
            textViewTitle = itemView.findViewById(R.id.textView_title);
        

        @Override
        public void onClick(View v) 

            ...
            ....
            .....

            // Un-necessary code

        
    

    private class FacebookAdViewHolder extends RecyclerView.ViewHolder 

        LinearLayout nativeAdContainer;

        public FacebookAdViewHolder(View facebookAd) 
            super(facebookAd);
            nativeAdContainer = facebookAd.findViewById(R.id.native_ad_container);
        


    


【问题讨论】:

【参考方案1】:

好的,对于那些希望使用 RecyclerView 将 Facebook Ads 集成到 android 应用程序的用户,以下是解决方案:

这里是示例的链接,其中包含实现:

https://origincache.facebook.com/developers/resources/?id=audience-network-sdk-4.25.0.zip

另外,对于快速代码sn-ps:

item_recylerview_fb_ad.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_
    android:layout_
    android:paddingBottom="10dp"
    android:paddingTop="10dp">

    <ImageView
        android:id="@+id/native_ad_icon"
        android:layout_
        android:layout_ />

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical"
        android:paddingLeft="5dp">

        <TextView
            android:id="@+id/native_ad_title"
            android:layout_
            android:layout_
            android:ellipsize="end"
            android:lines="1"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/native_ad_body"
            android:layout_
            android:layout_
            android:ellipsize="end"
            android:lines="2"
            android:textColor="@android:color/black"
            android:textSize="15sp" />
    </LinearLayout>
</LinearLayout>

<com.facebook.ads.MediaView
    android:id="@+id/native_ad_media"
    android:layout_
    android:layout_
    android:gravity="center" />

<LinearLayout
    android:layout_
    android:layout_
    android:orientation="horizontal"
    android:padding="5dp">

    <TextView
        android:id="@+id/native_ad_social_context"
        android:layout_
        android:layout_
        android:layout_weight="3"
        android:ellipsize="end"
        android:lines="2"
        android:paddingRight="5dp"
        android:textColor="@android:color/black"
        android:textSize="15sp" />

    <Button
        android:id="@+id/native_ad_call_to_action"
        android:layout_
        android:layout_
        android:layout_weight="2"
        android:gravity="center"
        android:textSize="16sp" />
</LinearLayout>

Activity.java

// Activity should implement NativeAdsManager.Listener
public class MainActivity extends AppCompatActivity implements 
NativeAdsManager.Listener

// Listeners for NativeAdsManager
@Override
public void onAdsLoaded() 
    mRecyclerViewAdapter.notifyDataSetChanged();


@Override
public void onAdError(AdError error) 


// Probably in onCreate()
NativeAdsManager mNativeAdsManager;
String placement_id = "Your ad placement id";
mNativeAdsManager = new NativeAdsManager(this, placement_id, 5);
mNativeAdsManager.loadAds();
mNativeAdsManager.setListener(this);

// Your CustomViewHolder class
private class FacebookAdViewHolder extends RecyclerView.ViewHolder 

        MediaView mvAdMedia;
        ImageView ivAdIcon;
        TextView tvAdTitle;
        TextView tvAdBody;
        TextView tvAdSocialContext;
        Button btnAdCallToAction;

        public FacebookAdViewHolder(View facebookAd) 
            super(facebookAd);

            mvAdMedia = facebookAd.findViewById(R.id.native_ad_media);
            tvAdTitle = facebookAd.findViewById(R.id.native_ad_title);
            tvAdBody = facebookAd.findViewById(R.id.native_ad_body);
            tvAdSocialContext = facebookAd.findViewById(R.id.native_ad_social_context);
            btnAdCallToAction = facebookAd.findViewById(R.id.native_ad_call_to_action);
            ivAdIcon = facebookAd.findViewById(R.id.native_ad_icon);
        

    

   // Finally your onBindViewHolder method
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) 

        Video video = listItems.get(position);

        if (holder.getItemViewType() == VIEW_FACEBOOK_AD_TYPE) 

            NativeAd ad;

            if (mAdItems.size() > position / 3) 
                ad = mAdItems.get(position / 3);
             else 
                ad = mNativeAdsManager.nextNativeAd();
                mAdItems.add(ad);
            

            if (ad != null) 
                FacebookAdViewHolder facebookAdViewHolder = (FacebookAdViewHolder) holder;
                facebookAdViewHolder.tvAdTitle.setText(ad.getAdTitle());
                facebookAdViewHolder.tvAdBody.setText(ad.getAdBody());
                facebookAdViewHolder.tvAdSocialContext.setText(ad.getAdSocialContext());
                facebookAdViewHolder.mvAdMedia.setNativeAd(ad);
                facebookAdViewHolder.btnAdCallToAction.setText(ad.getAdCallToAction());
                NativeAd.Image adIcon = ad.getAdIcon();
                NativeAd.downloadAndDisplayImage(adIcon, facebookAdViewHolder.ivAdIcon);
                ad.registerViewForInteraction(facebookAdViewHolder.itemView);
            

         else 
            CustomViewHolder customViewHolder = (CustomViewHolder) holder;
            Glide.with(context).load(URL_PART_1 + video.getVideoId() + URL_PART_2).into(customViewHolder.imageView);
            customViewHolder.textViewTitle.setText(video.getTitle());
        
    

同样,正如我之前提到的,上面提供的链接有解决方案。

【讨论】:

滚动的最后一项因为索引超出范围而崩溃我认为因为计数大于列表中的实际计数。【参考方案2】:

您必须将其添加到您的 FacebookAdViewHolder 才能使原生广告容器可点击。

private static class AdViewHolder extends RecyclerView.ViewHolder 

    TextView nativeAdTitle;
    Button nativeAdCallToAction;
    FrameLayout nativeAdContainer;
    ImageView nativeAdIcon;

AdViewHolder(View view) 
        super(view);
        nativeAdContainer = view.findViewById(R.id.fb_native_ad_container);
        nativeAdTitle = (TextView) view.findViewById(R.id.native_ad_title);
        nativeAdSocialContext = (TextView) view.findViewById(R.id.native_ad_social_context);
        nativeAdCallToAction = (Button) view.findViewById(R.id.native_ad_call_to_action);
        nativeAdIcon = (ImageView) view.findViewById(R.id.native_ad_icon);

    
public void bindView(NativeAd ad) 
    nativeAdSocialContext.setText(ad.getAdSocialContext());
            nativeAdCallToAction.setText(ad.getAdCallToAction());
            nativeAdTitle.setText(ad.getAdTitle());
            NativeAd.Image adIcon = ad.getAdIcon();
            NativeAd.downloadAndDisplayImage(adIcon, nativeAdIcon);
            List<View> clickableViews = new ArrayList<>();
            clickableViews.add(nativeAdTitle);
            clickableViews.add(nativeAdCallToAction);
            ad.registerViewForInteraction(nativeAdContainer, clickableViews);
        

【讨论】:

以上是关于Recyclerview - Facebook 原生广告 - 不可点击的主要内容,如果未能解决你的问题,请参考以下文章

如何在 RecyclerView 之间添加 Facebook Audience Network 原生广告

在滚动列表中实现视频的播放(ListView & RecyclerView)

Android在滚动列表中实现视频的播放 ListView RecyclerView

如何使布局与recyclerview一起滚动

如何在 Android RecyclerView 中制作占位符项目?

是否允许在 RecyclerView 上投放多个 AdMob 广告?另类广告网络?