-
-
Save tomleejumah/586fa25ba5c06c4d517cd1f1cb074ba1 to your computer and use it in GitHub Desktop.
| package com.leeStream.Sporty.Adapter; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.os.Handler; | |
| import android.util.Log; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| import androidx.annotation.NonNull; | |
| import androidx.recyclerview.widget.RecyclerView; | |
| import com.google.android.gms.ads.AdError; | |
| import com.google.android.gms.ads.AdRequest; | |
| import com.google.android.gms.ads.AdView; | |
| import com.google.android.gms.ads.FullScreenContentCallback; | |
| import com.google.android.gms.ads.interstitial.InterstitialAd; | |
| import com.leeStream.Sporty.Interface.AdCallback; | |
| import com.leeStream.Sporty.Games; | |
| import com.leeStream.Sporty.Models.LeagueItem; | |
| import com.leeStream.Sporty.R; | |
| import com.leeStream.Sporty.Screen; | |
| import com.leeStream.Sporty.Utils.NetworkUtils; | |
| import java.util.List; | |
| public class LeaguesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
| private static final int VIEW_TYPE_CONTENT = 0; | |
| private static final int VIEW_TYPE_AD = 1; | |
| private final Context mContext; | |
| private AdCallback adCallback; | |
| private final List<LeagueItem> mLeague; | |
| private InterstitialAd mInterstitialAd; | |
| private boolean isLeaguesData; | |
| public LeaguesAdapter(Context mContext, List<LeagueItem> mLeague) { | |
| this.mContext = mContext; | |
| this.mLeague = mLeague; | |
| } | |
| public void setInterstitialAd(InterstitialAd interstitialAd) { | |
| this.mInterstitialAd = interstitialAd; | |
| } | |
| @NonNull | |
| @Override | |
| public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
| LayoutInflater inflater = LayoutInflater.from(parent.getContext()); | |
| if (viewType == VIEW_TYPE_CONTENT) { | |
| View contentView = inflater.inflate(R.layout.league_item, parent, false); | |
| return new ContentViewHolder(contentView); | |
| } else { | |
| View adView = inflater.inflate(R.layout.banner_ad_layout, parent, false); | |
| return new AdViewHolder(adView); | |
| } | |
| } | |
| @Override | |
| public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { | |
| if (holder.getItemViewType() == VIEW_TYPE_CONTENT) { | |
| LeagueItem leagueItem = mLeague.get(position); | |
| ContentViewHolder contentViewHolder = (ContentViewHolder) holder; | |
| contentViewHolder.leagueName1.setText(leagueItem.getLeague()); | |
| contentViewHolder.itemView.setOnClickListener(view -> { | |
| if (adCallback != null) { | |
| adCallback.startLottieAnimation(); | |
| } | |
| new Handler().postDelayed(() -> { | |
| { | |
| if (mInterstitialAd != null) { | |
| mInterstitialAd.show((Activity) mContext); | |
| mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() { | |
| @Override | |
| public void onAdDismissedFullScreenContent() { | |
| //loading nextActivity when interstitial ad is closed by user | |
| if (isLeaguesData) { | |
| openNextActivity(position, Games.class); | |
| } else { | |
| openNextActivity(position, Screen.class); | |
| } | |
| } | |
| @Override | |
| public void onAdShowedFullScreenContent() { | |
| if (adCallback != null) { | |
| adCallback.onAdDismissed(); | |
| } | |
| } | |
| @Override | |
| public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) { | |
| if (isLeaguesData) { | |
| openNextActivity(position, Games.class); | |
| } else { | |
| openNextActivity(position, Screen.class); | |
| } | |
| } | |
| }); | |
| } else { | |
| Log.d("InterstitialAd", "Failed to Display"); | |
| if (isLeaguesData) { | |
| openNextActivity(position, Games.class); | |
| } else { | |
| openNextActivity(position, Screen.class); | |
| } | |
| } | |
| } | |
| }, 4000); | |
| }); | |
| } else { | |
| AdViewHolder adViewHolder = (AdViewHolder) holder; | |
| AdRequest adRequest = new AdRequest.Builder().build(); | |
| adViewHolder.adView.loadAd(adRequest); | |
| } | |
| } | |
| public void checkIfLeaguesData(boolean isLeaguesData) { | |
| this.isLeaguesData = isLeaguesData; | |
| notifyDataSetChanged(); | |
| } | |
| void openNextActivity(int position, Class<?> targetClass) { | |
| if (NetworkUtils.isNetworkConnected(mContext)) { | |
| Intent intent = new Intent(mContext, targetClass); | |
| String tag = String.valueOf(mLeague.get(position).getLeague()); | |
| String link = String.valueOf(mLeague.get(position).getLink()); | |
| intent.putExtra("League", tag); | |
| intent.putExtra("link", link); | |
| mContext.startActivity(intent); | |
| if (adCallback != null) { | |
| adCallback.onAdDismissed(); | |
| Log.d("YourAdapter", "onAdDismissed callback called."); | |
| } | |
| } else { | |
| Toast.makeText(mContext, "you are not connected to the internet", Toast.LENGTH_SHORT).show(); | |
| } | |
| } | |
| @Override | |
| public int getItemViewType(int position) { | |
| int viewType = (position % 3 == 0) ? VIEW_TYPE_AD : VIEW_TYPE_CONTENT; | |
| Log.d("LeaguesAdapter", "Position: " + position + ", ViewType: " + viewType); | |
| return viewType; | |
| } | |
| @Override | |
| public int getItemCount() { | |
| if (mLeague != null) { | |
| return mLeague.size(); | |
| } else { | |
| return 0; | |
| } | |
| } | |
| public static class ContentViewHolder extends RecyclerView.ViewHolder { | |
| private final TextView leagueName1; | |
| public ContentViewHolder(@NonNull View itemView) { | |
| super(itemView); | |
| leagueName1 = itemView.findViewById(R.id.txtLeague); | |
| //TODO implement image view for the league | |
| } | |
| } | |
| public static class AdViewHolder extends RecyclerView.ViewHolder { | |
| private final AdView adView; | |
| public AdViewHolder(@NonNull View itemView) { | |
| super(itemView); | |
| adView = itemView.findViewById(R.id.adView1); | |
| } | |
| } | |
| } |
@HarshaliSachani The expected output is that the ad should display after 2 RecyclerView Items ie 110220330 in that pattern but the behaviour is :
a) if im having only one item,it only shows the ad and not the item so the output is (0 and the expected is 1 since its only an item)...this behaviois also noticeable if im having 3 items the output is (110) and not (1101). SO basically its only working with even and not odds
N/B : im using the 0 to represent the ads postion and 1 the items in my recyclerview.
@Ttoml33 You can check minimum size of your original content list of it's less then 2 then don't add ad view in list.
For odds issue, I guess you will alter your original list and add ad view in specific index correct?
So, add one condition for last index also.
@Ttoml33 I've added this file in another project and push code. Please visit
https://github.com/HarshaliSachani/AndroidAppsCollection/blob/main/app/src/main/java/com/map/androidappcollections/activities/RecyclerViewWithAdsActivity.kt
You can set adview with dynamic size of list (odd/even)
Hope it will help to solve your problem.
@HarshaliSachani thank you


@Ttoml33 could you please share your current output and expected output image or screenshot?