Created
July 6, 2020 05:23
-
-
Save kamaravichow/92a3e7534e25427c935e55392cdbd6e0 to your computer and use it in GitHub Desktop.
Ads in recycler view
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 com.aravi.popularly.adapter; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.graphics.Paint; | |
import android.net.Uri; | |
import android.util.Log; | |
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 androidx.recyclerview.widget.RecyclerView; | |
import com.aravi.popularly.R; | |
import com.aravi.popularly.helper.SharedPreferenceHelper; | |
import com.aravi.popularly.model.Caption; | |
import com.aravi.popularly.util.CaptionTools; | |
import com.bumptech.glide.Glide; | |
import com.facebook.ads.AdOptionsView; | |
import com.facebook.ads.MediaView; | |
import com.facebook.ads.NativeAd; | |
import com.facebook.ads.NativeAdLayout; | |
import com.facebook.ads.NativeAdsManager; | |
import java.util.ArrayList; | |
import java.util.List; | |
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; | |
public class NativeAdRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
private List<Caption> mPostItems; | |
private List<NativeAd> mAdItems; | |
private NativeAdsManager mNativeAdsManager; | |
private Context mActivity; | |
private static final int AD_DISPLAY_FREQUENCY = 5; | |
private static final int POST_TYPE = 0; | |
private static final int AD_TYPE = 1; | |
public NativeAdRecyclerAdapter(Context activity, List<Caption> postItems, NativeAdsManager nativeAdsManager) { | |
mNativeAdsManager = nativeAdsManager; | |
mPostItems = postItems; | |
mAdItems = new ArrayList<>(); | |
mActivity = activity; | |
} | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
if (viewType == AD_TYPE) { | |
NativeAdLayout inflatedView = (NativeAdLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_native_ad, parent, false); | |
return new AdHolder(inflatedView); | |
} else { | |
View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_caption, parent, false); | |
return new ProductViewHolder(inflatedView); | |
} | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return position % AD_DISPLAY_FREQUENCY == 0 ? AD_TYPE : POST_TYPE; | |
} | |
@Override | |
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | |
if (holder.getItemViewType() == AD_TYPE) { | |
NativeAd ad; | |
if (mAdItems.size() > position / AD_DISPLAY_FREQUENCY) { | |
ad = mAdItems.get(position / AD_DISPLAY_FREQUENCY); | |
} else { | |
ad = mNativeAdsManager.nextNativeAd(); | |
if (!ad.isAdInvalidated()) { | |
mAdItems.add(ad); | |
} else { | |
Log.w(NativeAdRecyclerAdapter.class.getSimpleName(), "Ad is invalidated!"); | |
} | |
} | |
AdHolder adHolder = (AdHolder) holder; | |
adHolder.adChoicesContainer.removeAllViews(); | |
if (ad != null) { | |
adHolder.tvAdTitle.setText(ad.getAdvertiserName()); | |
adHolder.tvAdBody.setText(ad.getAdBodyText()); | |
adHolder.tvAdSocialContext.setText(ad.getAdSocialContext()); | |
adHolder.tvAdSponsoredLabel.setText("Sponsored"); | |
adHolder.btnAdCallToAction.setText(ad.getAdCallToAction()); | |
adHolder.btnAdCallToAction.setVisibility( | |
ad.hasCallToAction() ? View.VISIBLE : View.INVISIBLE); | |
AdOptionsView adOptionsView = | |
new AdOptionsView(mActivity, ad, adHolder.nativeAdLayout); | |
adHolder.adChoicesContainer.addView(adOptionsView, 0); | |
List<View> clickableViews = new ArrayList<>(); | |
clickableViews.add(adHolder.ivAdIcon); | |
clickableViews.add(adHolder.mvAdMedia); | |
clickableViews.add(adHolder.btnAdCallToAction); | |
ad.registerViewForInteraction( | |
adHolder.nativeAdLayout, | |
adHolder.mvAdMedia, | |
adHolder.ivAdIcon, | |
clickableViews); | |
} | |
} else { | |
ProductViewHolder itemHolder = (ProductViewHolder) holder; | |
int index = position - (position / AD_DISPLAY_FREQUENCY) - 1; | |
Caption product = mPostItems.get(index); | |
itemHolder.captionMain.setText(product.getCaption()); | |
if (product.getInstaID() == null) { | |
itemHolder.userName.setText("@" + "hiketoppro"); | |
} else { itemHolder.userName.setText("@" + product.getInstaID()); } | |
if (product.getImageRep() == null) { | |
itemHolder.mImageRep.setVisibility(View.GONE); | |
} else { | |
itemHolder.mImageRep.setVisibility(View.VISIBLE); | |
Glide.with(mActivity).load(mPostItems.get(position).getImageRep()).into(itemHolder.mImageRep); | |
} | |
if (product.getUserImg() == null) { | |
Glide.with(mActivity).load(R.mipmap.ic_launcher).into(itemHolder.userDp); | |
} else { | |
Glide.with(mActivity).load(mPostItems.get(position).getUserImg()).into(itemHolder.userDp); | |
} | |
itemHolder.mCopy.setOnClickListener(view -> { | |
CaptionTools.copyText(mActivity, product.getCaption() + "\n \n CC : " + itemHolder.userName.getText().toString()); | |
}); | |
itemHolder.mlike.setOnClickListener(view -> CaptionTools.addToFavorites(mActivity, product.getCaption(), product.getInstaID(), product.getUserImg())); | |
itemHolder.mShare.setOnClickListener(view -> { | |
Intent shareIntent = new Intent(Intent.ACTION_SEND); | |
shareIntent.setType("text/plain"); | |
shareIntent.putExtra(Intent.EXTRA_TEXT, product.getCaption() + "\n \n CC : " + itemHolder.userName.getText().toString()); | |
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
mActivity.startActivity(Intent.createChooser(shareIntent, "Sharing Caption....")); | |
}); | |
itemHolder.userName.setOnClickListener(view -> { | |
String userNamee = itemHolder.userName.getText().toString(); | |
userNamee = userNamee.replace("@", ""); | |
Uri uri = Uri.parse("http://www.instagram.com/" + userNamee); | |
Intent intent = new Intent(Intent.ACTION_VIEW, uri); | |
intent.addFlags(FLAG_ACTIVITY_NEW_TASK); | |
mActivity.startActivity(intent); | |
}); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return mPostItems.size() + mAdItems.size(); | |
} | |
private static class ProductViewHolder extends RecyclerView.ViewHolder { | |
final ImageView userDp; | |
final TextView captionMain; | |
final TextView userName; | |
ImageView mlike, mCopy, mShare, mImageRep; | |
LinearLayout mLinearLayout; | |
private ProductViewHolder(View itemView) { | |
super(itemView); | |
userDp = itemView.findViewById(R.id.user_dp); | |
captionMain = itemView.findViewById(R.id.caption_text); | |
userName = itemView.findViewById(R.id.userName); | |
mImageRep = itemView.findViewById(R.id.image_rep); | |
mLinearLayout = itemView.findViewById(R.id.winner_trigger); | |
mlike = itemView.findViewById(R.id.action_like); | |
mCopy = itemView.findViewById(R.id.action_copy); | |
mShare = itemView.findViewById(R.id.action_share); | |
} | |
} | |
private static class AdHolder extends RecyclerView.ViewHolder { | |
NativeAdLayout nativeAdLayout; | |
MediaView mvAdMedia; | |
MediaView ivAdIcon; | |
TextView tvAdTitle; | |
TextView tvAdBody; | |
TextView tvAdSocialContext; | |
TextView tvAdSponsoredLabel; | |
Button btnAdCallToAction; | |
LinearLayout adChoicesContainer; | |
AdHolder(NativeAdLayout adLayout) { | |
super(adLayout); | |
nativeAdLayout = adLayout; | |
mvAdMedia = adLayout.findViewById(R.id.native_ad_media); | |
tvAdTitle = adLayout.findViewById(R.id.native_ad_title); | |
tvAdBody = adLayout.findViewById(R.id.native_ad_body); | |
tvAdSocialContext = adLayout.findViewById(R.id.native_ad_social_context); | |
tvAdSponsoredLabel = adLayout.findViewById(R.id.native_ad_sponsored_label); | |
btnAdCallToAction = adLayout.findViewById(R.id.native_ad_call_to_action); | |
ivAdIcon = adLayout.findViewById(R.id.native_ad_icon); | |
adChoicesContainer = adLayout.findViewById(R.id.ad_choices_container); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment