Created
January 27, 2016 17:42
-
-
Save burnix/84581959a9c3ebceecf8 to your computer and use it in GitHub Desktop.
This file contains 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.uzb; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Handler; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
import android.widget.FrameLayout; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import com.facebook.ads.Ad; | |
import com.facebook.ads.AdChoicesView; | |
import com.facebook.ads.AdError; | |
import com.facebook.ads.AdListener; | |
import com.facebook.ads.MediaView; | |
import com.facebook.ads.NativeAd; | |
import com.facebook.ads.NativeAdView; | |
import com.facebook.ads.NativeAdsManager; | |
import com.squareup.picasso.Picasso; | |
import java.util.HashMap; | |
import java.util.List; | |
/** | |
* Created by igor on 11.01.16. | |
*/ | |
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements AdListener { | |
protected List<FoodData> foodDataList; | |
protected Context context; | |
protected String image_url, food_title, id; | |
protected int ingredients_count; | |
protected Intent intent; | |
private static final String FROM = "c172x172"; | |
private static final String TO = "c240x90"; | |
private HashMap<Integer, String> titleMap = new HashMap<>(); | |
private NativeAd nativeAd; | |
private NativeAdsManager manager; | |
View adView; | |
public RecyclerAdapter(List<FoodData> food, Context context, NativeAd nativeAd, NativeAdsManager manager) { | |
this.foodDataList = food; | |
this.context = context; | |
this.nativeAd = nativeAd; | |
this.manager = manager; | |
} | |
@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 AdditionalHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_test2, parent, false)); | |
} | |
return null; | |
} | |
@Override | |
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { | |
switch (holder.getItemViewType()) { | |
case 0: | |
MainViewHolder m_holder = (MainViewHolder) holder; | |
if ((position % 5 == 0) && position != 0) { | |
nativeAd = manager.nextNativeAd(); | |
} | |
image_url = foodDataList.get(position).getRecipe_image_url(); | |
image_url = image_url.replaceAll(FROM, TO); | |
food_title = foodDataList.get(position).getRecipe_text(); | |
if (titleMap.containsKey(position)) { | |
m_holder.title.setText(titleMap.get(position)); | |
} else { | |
titleMap.put(position, food_title); | |
updateTextViewUI(m_holder.title, food_title); | |
} | |
id = foodDataList.get(position).getRecipe_id(); | |
ingredients_count = foodDataList.get(position).getRecipe_ingr_count(); | |
Picasso.with(context) | |
.load(image_url) | |
.placeholder(R.drawable.blank2) | |
.into(m_holder.food_picture); | |
m_holder.title.setText(food_title); | |
m_holder.ingr_count.setText("Количество ингридиентов: " + ingredients_count); | |
m_holder.look.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
intent = new Intent(context, FoodAppearance.class); | |
intent.putExtra("id", foodDataList.get(position).getRecipe_id()); | |
context.startActivity(intent); | |
} | |
}); | |
m_holder.food_picture.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
intent = new Intent(context, FoodAppearance.class); | |
intent.putExtra("id", foodDataList.get(position).getRecipe_id()); | |
context.startActivity(intent); | |
} | |
}); | |
return; | |
case 2: | |
AdditionalHolder new_holder = (AdditionalHolder) holder; | |
// try { | |
// new_holder.templateContainer.removeViewInLayout(adView); | |
// } catch (Exception e) { | |
// e.printStackTrace(); | |
// } | |
// nativeAd = manager.nextNativeAd(); | |
// adView = NativeAdView.render(context, nativeAd, NativeAdView.Type.HEIGHT_300); | |
// | |
// new_holder.templateContainer.addView(adView); | |
// new_holder.blank_holder.setVisibility(View.GONE); | |
// Setting the Text. | |
new_holder.nativeAdSocialContext.setText(nativeAd.getAdSocialContext()); | |
new_holder.nativeAdCallToAction.setText(nativeAd.getAdCallToAction()); | |
new_holder.nativeAdTitle.setText(nativeAd.getAdTitle()); | |
new_holder.nativeAdBody.setText(nativeAd.getAdBody()); | |
// Downloading and setting the ad icon. | |
NativeAd.Image adIcon = nativeAd.getAdIcon(); | |
NativeAd.downloadAndDisplayImage(adIcon, new_holder.nativeAdIcon); | |
// Download and setting the cover image. | |
NativeAd.Image adCoverImage = nativeAd.getAdCoverImage(); | |
new_holder.nativeAdMedia.setNativeAd(nativeAd); | |
// Add adChoices icon | |
if (new_holder.adChoicesView == null) { | |
new_holder.adChoicesView = new AdChoicesView(context, nativeAd, true); | |
new_holder.adView.addView(new_holder.adChoicesView, 0); | |
} | |
nativeAd.registerViewForInteraction(new_holder.adView); | |
return; | |
default: | |
break; | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return foodDataList.size(); | |
} | |
private void updateTextViewUI(final TextView textView, final String title) { | |
new Handler(context.getMainLooper()).post(new Runnable() { | |
@Override | |
public void run() { | |
textView.setText(title); | |
} | |
}); | |
} | |
@Override | |
public void onError(Ad ad, AdError adError) { | |
Log.i("Error", "recycleradapter"); | |
} | |
@Override | |
public void onAdLoaded(Ad ad) { | |
Log.i("onadLoaded", "recycleradapter"); | |
// Render the View with the next native ad, | |
// and define the Native Ad Template | |
} | |
@Override | |
public void onAdClicked(Ad ad) { | |
Log.i("onAdClicker", "recycleradapter"); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
int viewType = 0; | |
if ((position % 9 == 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 AdditionalHolder extends RecyclerView.ViewHolder { | |
protected LinearLayout templateContainer; | |
protected ImageView blank_holder; | |
private LinearLayout nativeAdContainer; | |
private LinearLayout adView; | |
private AdChoicesView adChoicesView; | |
ImageView nativeAdIcon; | |
TextView nativeAdTitle; | |
TextView nativeAdBody; | |
MediaView nativeAdMedia; | |
TextView nativeAdSocialContext; | |
Button nativeAdCallToAction; | |
public AdditionalHolder(View view) { | |
super(view); | |
//// Add ad into the ad container. | |
nativeAdContainer = (LinearLayout) view.findViewById(R.id.ad_test2); | |
LayoutInflater inflater = LayoutInflater.from(context); | |
adView = (LinearLayout) inflater.inflate(R.layout.ad_test3, nativeAdContainer, false); | |
nativeAdContainer.addView(adView); | |
// templateContainer = (LinearLayout) view.findViewById(R.id.ad_test2); | |
// blank_holder = (ImageView) view.findViewById(R.id.blank_holder); | |
// Create native UI using the ad metadata. | |
nativeAdIcon = (ImageView) view.findViewById(R.id.native_ad_icon); | |
nativeAdTitle = (TextView) view.findViewById(R.id.native_ad_title); | |
nativeAdBody = (TextView) view.findViewById(R.id.native_ad_body); | |
nativeAdMedia = (MediaView) view.findViewById(R.id.native_ad_media); | |
nativeAdSocialContext = (TextView) view.findViewById(R.id.native_ad_social_context); | |
nativeAdCallToAction = (Button) view.findViewById(R.id.native_ad_call_to_action); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment