Facebook原生广告不会点击:Android

时间:2016-12-10 09:41:48

标签: android facebook listview facebook-audience-network

我正在使用受众群体网络在Android上创建原生广告。问题是广告已展示但未点击。每当我点击我注册点击广告的任何视图时,都不会发生任何事情。我正在使用自定义适配器将广告加载到自定义listView中:

static StaticListView turning_up_lv; //My custom listview

private static void showNativeAd() {
    nativeAd = new NativeAd(context, AD_ID);
    nativeAd.setAdListener(new AdListener() {

        @Override
        public void onError(Ad ad, AdError error) {
        }

        @Override
        public void onAdLoaded(Ad ad) {
            if (ad != nativeAd) {
                return;
            }

            isAdLoaded = true;
            if ((turning_up_lv.getAdapter()) != null && turning_up_lv.getCount() > 3) {
                ((MyAdapter) turning_up_lv.getAdapter()).addNativeAd(ad, false);
            }
        }

        @Override
        public void onAdClicked(Ad ad) {

        }
    });

    nativeAd.loadAd(NativeAd.MediaCacheFlag.ALL);
}

以下是自定义列表视图的代码:

public class StaticListView extends ListView {

public StaticListView(Context context) {
    super(context);
}

public StaticListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public StaticListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST));
    getLayoutParams().height = getMeasuredHeight();
}

}

最后,这是我将广告加载到适配器的部分。请注意,由于适配器太长,我删除了一些不相关的部分

   @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
    View row = convertView;
    MyViewHolder holder;

    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.turning_up_item, parent,
                false);
        holder = new MyViewHolder(row);
        row.setTag(holder);
    } else {
        holder = (MyViewHolder) row.getTag();
    }

    if (position == AD_INDEX && ad != null) {
        inflateAd((NativeAd) ad, holder, row);
    } else {

       //Load other listview items
      }
}

以下是宣传广告的代码:

private void inflateAd(final NativeAd nativeAd, MyViewHolder holder, final View view) {
        // Create native UI using the ad metadata.
// Setting the Text
    holder.time_tv.setText("Sponsored");
    holder.native_ad_social_context.setText(nativeAd.getAdSocialContext());
    holder.native_ad_call_to_action.setText(nativeAd.getAdCallToAction());
    holder.native_ad_call_to_action.setVisibility(View.VISIBLE);
    holder.un_tu.setText(nativeAd.getAdTitle());
    holder.comment_or_caption_tv.setText(nativeAd.getAdBody());

    // Downloading and setting the ad icon.
    NativeAd.Image adIcon = nativeAd.getAdIcon();
    NativeAd.downloadAndDisplayImage(adIcon, holder.ad_iv);


    // Downloading and setting the cover image.
    NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
    int bannerWidth = adCoverImage.getWidth();
    int bannerHeight = adCoverImage.getHeight();
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int mediaWidth = holder.native_ad_media.getWidth() > 0 ? holder.native_ad_media.getWidth() : metrics.widthPixels;
    holder.native_ad_media.setLayoutParams(new LinearLayout.LayoutParams(
            mediaWidth,
            Math.min(
                    (int) (((double) mediaWidth / (double) bannerWidth) * bannerHeight),
                    metrics.heightPixels / 3)));
    holder.native_ad_media.setAutoplay(AdSettings.isVideoAutoplay());

    holder.native_ad_media.setNativeAd(nativeAd);

    addLoadedToMediaView = true;

    final ArrayList<View> clickableViews = new ArrayList<>();
    clickableViews.add(holder.native_ad_media);
    clickableViews.add(holder.native_ad_social_context);
    clickableViews.add(holder.native_ad_call_to_action);

    nativeAd.registerViewForInteraction(view, clickableViews);
}

0 个答案:

没有答案