I have RecyclerView
, where every 14-th item is a Facebook Audience Network Ad
.
@Override
public int getItemViewType(int position) {
int viewType = 0;
if (position % 14 == 0 && position != 0) viewType = 2;
return viewType;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0:
return new MainViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false));
case 2:
return new AdHolder((LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_test3, parent, false)));
}
return null;
}
The is as follows: every 14th element is the same. Here's onBindViewHolder
method.
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final FoodData foodData = foodDataList.get(position);
switch (holder.getItemViewType()) {
case 0:
MainViewHolder mainViewHolder = (MainViewHolder) holder;
...
break;
case 2:
AdHolder adHolder = (AdHolder) holder;
//System.out.println("ad hasH" + position);
if (manager.isLoaded()) {
NativeAd nativeAd;
if (map.containsKey(position)) {
nativeAd = map.get(position);
} else {
nativeAd = manager.nextNativeAd();
map.put(position, nativeAd);
}
System.out.println(" Native Ad" + nativeAd.hashCode());
System.out.println(" Native Ad.Title" + nativeAd.getAdTitle());
adHolder.templateContainer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Config.AD_HEIGHT_DP));
adHolder.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
adHolder.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
adHolder.nativeAdTitle.setText(nativeAd.getAdTitle());
adHolder.nativeAdBody.setText(nativeAd.getAdBody());
Picasso.with(context)
.load(nativeAd.getAdIcon().getUrl())
.tag("resume_tag")
.into(adHolder.nativeAdIcon);
Picasso.with(context)
.load(nativeAd.getAdCoverImage().getUrl())
.resize(width, ad_height)
.tag("resume_tag")
.placeholder(R.drawable.adholder2)
.into(adHolder.nativeAdMedia);
System.out.println("url =" + nativeAd.getAdCoverImage().getUrl());
if (adHolder.adChoicesView == null) {
adHolder.adChoicesView = new AdChoicesView(context, nativeAd, true);
adHolder.adChoiceContainer.addView(adHolder.adChoicesView, 0);
}
nativeAd.registerViewForInteraction(holder.itemView);
} else {
adHolder.params = adHolder.templateContainer.getLayoutParams();
adHolder.templateContainer.setLayoutParams(new ViewGroup.LayoutParams(0, 0));
}
break;
}
}
What I can't understand is when I check where I the same NativeAd object
:
System.out.println(" Native Ad" + nativeAd.hashCode());
System.out.println(" Native Ad.Title" + nativeAd.getAdTitle());
I find, that hashCode
of the NativeAd changes, but title
(and other elements) doesn't!
Hope somebody we'll help me. Here's full code of Adapter
https://gist.github.com/burnix/c1dd34dd896f5c6ddc6b2b8971908e28