I am implementing Facebook Native Ads in RecyclerView
. The ads load fine, but the ads are not clickable. Other general items in my RecyclerView
are clickable as i implemented OnClickListener
for them. How do i get the facebook ads clickable? Can someone help me with this?
Here is my code:
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);
}
}
}