Created
April 6, 2016 17:27
-
-
Save burnix/c1dd34dd896f5c6ddc6b2b8971908e28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package be.motti.tort.adapters; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import com.facebook.ads.AdChoicesView; | |
import com.facebook.ads.AdError; | |
import com.facebook.ads.NativeAd; | |
import com.facebook.ads.NativeAdsManager; | |
import com.squareup.picasso.Picasso; | |
import java.util.HashMap; | |
import java.util.List; | |
import be.motti.tort.Config; | |
import be.motti.tort.objects.FoodData; | |
import be.motti.tort.R; | |
import be.motti.tort.activities.FoodAppearance; | |
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
protected List<FoodData> foodDataList; | |
protected Context context; | |
protected Intent intent; | |
private int width, height, ad_height; | |
private NativeAdsManager manager; | |
private HashMap<Integer, NativeAd> map = new HashMap<>(); | |
public RecyclerAdapter(List<FoodData> food, final Context context, int widht, int height, String code) { | |
this.foodDataList = food; | |
this.context = context; | |
this.width = widht; | |
this.height = height; | |
manager = new NativeAdsManager(context, Config.NETWORK_AUDIENCE_ID, 3); | |
manager.loadAds(); | |
double ads_height = width * 0.52; | |
ad_height = (int) ads_height; | |
} | |
@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; | |
} | |
@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; | |
System.out.println("main hasH " + holder.hashCode()); | |
Picasso.with(context) | |
.load(foodData.getRecipe_resize_image_url()) | |
.resize(width, height) | |
.placeholder(R.drawable.empty_image) | |
.error(R.drawable.empty_image) | |
.tag("resume_tag") | |
.into(mainViewHolder.food_picture); | |
String ingrs = "Количество ингридиентов: " + String.valueOf(foodData.getRecipe_ingr_count()); | |
System.out.println("ингредиенты = " + ingrs); | |
mainViewHolder.title.setText(foodData.getRecipe_text()); | |
mainViewHolder.ingr_count.setText(ingrs); | |
mainViewHolder.look.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
intent = new Intent(context, FoodAppearance.class); | |
intent.putExtra("id", foodData.getRecipe_id()); | |
intent.putExtra("width", width); | |
intent.putExtra("height", height); | |
context.startActivity(intent); | |
} | |
}); | |
mainViewHolder.food_picture.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
intent = new Intent(context, FoodAppearance.class); | |
intent.putExtra("id", foodData.getRecipe_id()); | |
intent.putExtra("width", width); | |
intent.putExtra("height", height); | |
context.startActivity(intent); | |
} | |
}); | |
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); | |
} | |
// if (adHolder.params != null) { | |
// adHolder.templateContainer.setLayoutParams(adHolder.params); | |
// } | |
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; | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return foodDataList.size(); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
int viewType = 0; | |
if (position % 14 == 0 && position != 0) viewType = 2; | |
return viewType; | |
} | |
public class MainViewHolder extends RecyclerView.ViewHolder { | |
protected ImageView food_picture; | |
protected TextView title, ingr_count, look; | |
protected LinearLayout linearLayout; | |
public MainViewHolder(View v) { | |
super(v); | |
food_picture = (ImageView) v.findViewById(R.id.food_picture); | |
title = (TextView) v.findViewById(R.id.title); | |
ingr_count = (TextView) v.findViewById(R.id.ingr_count); | |
look = (TextView) v.findViewById(R.id.look); | |
linearLayout = (LinearLayout) v.findViewById(R.id.list_item); | |
} | |
} | |
public class AdHolder extends RecyclerView.ViewHolder { | |
protected LinearLayout templateContainer; | |
protected TextView nativeAdTitle, nativeAdBody, nativeAdSocialContext, nativeAdCallToAction; | |
protected ImageView nativeAdIcon, nativeAdMedia; | |
protected ViewGroup.LayoutParams params; | |
protected LinearLayout adChoiceContainer; | |
private AdChoicesView adChoicesView; | |
public AdHolder(View v) { | |
super(v); | |
templateContainer = (LinearLayout) v.findViewById(R.id.ad_unit); | |
adChoiceContainer = (LinearLayout) v.findViewById(R.id.ad_choice); | |
nativeAdMedia = (ImageView) v.findViewById(R.id.native_ad_media); | |
nativeAdIcon = (ImageView) v.findViewById(R.id.native_ad_icon); | |
nativeAdTitle = (TextView) v.findViewById(R.id.native_ad_title); | |
nativeAdBody = (TextView) v.findViewById(R.id.native_ad_body); | |
nativeAdSocialContext = (TextView) v.findViewById(R.id.native_ad_social_context); | |
nativeAdCallToAction = (TextView) v.findViewById(R.id.native_ad_call_to_action); | |
} | |
} | |
public void add(int position) { | |
foodDataList.add(position, null); | |
notifyItemInserted(position); | |
} | |
public void delete(int position) { | |
foodDataList.remove(position); | |
notifyItemRemoved(position); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment