Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chiragthummar/2b3f022f0db2294f84a91ddb2d802731 to your computer and use it in GitHub Desktop.
Save chiragthummar/2b3f022f0db2294f84a91ddb2d802731 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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
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.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 = "c480x180";
private HashMap<Integer, String> titleMap = new HashMap<>();
private NativeAd nativeAd;
View adView;
public RecyclerAdapter(List<FoodData> food, Context context, NativeAd nativeAd) {
this.foodDataList = food;
this.context = context;
this.nativeAd = nativeAd;
}
@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;
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)
.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;
adView = NativeAdView.render(context, nativeAd, NativeAdView.Type.HEIGHT_100);
new_holder.templateContainer.addView(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) {
}
@Override
public void onAdLoaded(Ad ad) {
// Render the View with the next native ad,
// and define the Native Ad Template
View adView = NativeAdView.render(context, nativeAd, NativeAdView.Type.HEIGHT_100);
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public int getItemViewType(int position) {
int viewType = 0;
if (position % 10 == 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;
public AdditionalHolder(View view) {
super(view);
templateContainer = (LinearLayout) view.findViewById(R.id.ad_test2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment