Skip to content

Instantly share code, notes, and snippets.

@burnix
Created February 8, 2016 19:04
Show Gist options
  • Save burnix/8313723e44067de20802 to your computer and use it in GitHub Desktop.
Save burnix/8313723e44067de20802 to your computer and use it in GitHub Desktop.
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.MemoryPolicy;
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<RecyclerAdapter.MainViewHolder> {
protected List<FoodData> foodDataList;
protected Context context;
protected int ingredients_count;
protected Intent intent;
private View adView;
private int width, height;
private NativeAdsManager manager;
private NativeAd nativeAd;
// public RecyclerAdapter(List<FoodData> food, Context context, NativeAd nativeAd, NativeAdsManager manager) {
// this.foodDataList = food;
// this.context = context;
// this.nativeAd = nativeAd;
// this.manager = manager;
// }
public RecyclerAdapter(List<FoodData> food, Context context) {
this.foodDataList = food;
this.context = context;
}
public RecyclerAdapter(List<FoodData> food, Context context, int widht, int height) {
this.foodDataList = food;
this.context = context;
this.width = widht;
this.height = height;
}
public RecyclerAdapter(List<FoodData> food, Context context, int widht, int height, String code) {
this.foodDataList = food;
this.context = context;
this.width = widht;
this.height = height;
manager = new NativeAdsManager(context, "892769720837476_892772047503910", 5);
manager.setListener(new NativeAdsManager.Listener() {
@Override
public void onAdsLoaded() {
nativeAd = manager.nextNativeAd();
}
@Override
public void onAdError(AdError adError) {
}
});
manager.loadAds();
}
@Override
public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MainViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false));
}
@Override
public void onBindViewHolder(final MainViewHolder holder, final int position) {
final FoodData foodData = foodDataList.get(position);
Picasso.with(context)
.load(foodData.getRecipe_resize_image_url())
.resize(width, height)
.placeholder(R.drawable.blank_hd)
.tag("resume_tag")
//.memoryPolicy(MemoryPolicy.NO_CACHE)
.into(holder.food_picture);
holder.title.setText(foodData.getRecipe_text());
holder.ingr_count.setText("Количество ингридиентов: " + ingredients_count);
holder.look.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(context, FoodAppearance.class);
intent.putExtra("id", foodData.getRecipe_id());
context.startActivity(intent);
}
});
holder.food_picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(context, FoodAppearance.class);
intent.putExtra("id", foodData.getRecipe_id());
context.startActivity(intent);
}
});
// try {
// holder.linearLayout.removeViewInLayout(adView);
// nativeAd = manager.nextNativeAd();
// adView = NativeAdView.render(context, nativeAd, NativeAdView.Type.HEIGHT_300);
//
// holder.linearLayout.addView(adView);
// // holder.blank_holder.setVisibility(View.GONE);
// } catch (Exception e) {
// e.printStackTrace();
// }
// 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);
}
@Override
public int getItemCount() {
return foodDataList.size();
}
@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