How to add Native Ads in between my data items? - android

I have created a recycler view with custom adapter where I want to show native ads in every 3rd position of my items. But whatever I wrote , its overlapping my item and on top of that, native ad is shown. But I don't want this.
public class Event_Discover_Combined_Adapter extends RecyclerView.Adapter<Event_Discover_Combined_Adapter.MyViewHolder> {
private Context context;
private ArrayList<Event_Discover_Combined_Details> beanList;
private int itemCount;
public Event_Discover_Combined_Adapter(Context context, ArrayList<Event_Discover_Combined_Details> beanList, int itemCount) {
this.context = context;
this.beanList = beanList;
this.imageLoader = ImageLoader.getInstance();
this.itemCount = itemCount;
}
//int viewType ;
#Override
public int getItemViewType(int position) {
if (position % 3 == 0) {
return R.layout.native_ad_frame_layout;
} else {
if (beanList.get(position).getType() == ConstantUrl.TYPE_EVENT) {
return R.layout.events_item;
} else {
return R.layout.discover_item;
}
}
}
#Override
public Event_Discover_Combined_Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(viewType, parent, false);
return new MyViewHolder(itemView);
}
public static ImageLoader imageLoader;
#Override
public void onBindViewHolder(final Event_Discover_Combined_Adapter.MyViewHolder holder, int position) {
Event_Discover_Combined_Details bean = beanList.get(position);
if (position % 3 == 0) {
//Ad View
//For Native Ads
AdLoader.Builder builder = new AdLoader.Builder(context, context.getResources().getString(R.string.ADMOB_AD_UNIT_ID));
//if (requestContentAds) {
builder.forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
#Override
public void onContentAdLoaded(NativeContentAd ad) {
NativeContentAdView adView = (NativeContentAdView) getLayoutInflater()
.inflate(R.layout.native_ad_content, null);
populateContentAdView(ad, adView);
holder.frameLayout.removeAllViews();
holder.frameLayout.addView(adView);
}
});
//}
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(context, "Failed to load native ad: "
+ errorCode, Toast.LENGTH_SHORT).show();
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
} else {
//Normal View
AppLog.Log("combine", "beanlist" + bean.getType());
if (bean.getType() == ConstantUrl.TYPE_DISCOVER) {
if (!bean.getDiscoverDetails().getIcon().equals(""))
imageLoader.displayImage(bean.getDiscoverDetails().getIcon(),
holder.iv_discover_icon);
holder.tv_author_name.setText(bean.getDiscoverDetails().getAuthor());
holder.item_discover_descrip.setText(bean.getDiscoverDetails().getDescription());
holder.tv_discover_heading.setText(bean.getDiscoverDetails().getTitle());
} else if (bean.getType() == ConstantUrl.TYPE_EVENT) {
if (!bean.getEventDetails().getIcon().equals(""))
imageLoader.displayImage(bean.getEventDetails().getIcon(),
holder.event_icon);
/* holder.item_event_date.setText(bean.getStartDate());
holder.item_event_time.setText(bean.getStartDate());*/
try {
Date date1 = AppUtils.getFormatedDate(bean.getEventDetails().getStartDate());
String event_time = (String) android.text.format.DateFormat.format("EEEE", date1) + " " + context.getString(R.string.at_text) + " " + new SimpleDateFormat("hh:mm aa").format(date1);
// holder.item_event_date.setText((String) android.text.format.DateFormat.format("dd", date1));
holder.item_event_date.setText(event_time);
holder.item_event_time.setText(event_time);
} catch (Exception e) {
e.printStackTrace();
}
holder.item_event_heading.setText(bean.getEventDetails().getName());
holder.item_event_descrip.setText(bean.getEventDetails().getDescription());
}
}
}
#Override
public int getItemCount() {
return itemCount;
}
// int count = 0;
public void setItemCount(int count) {
this.itemCount = count;
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//For Discover
public ImageView iv_discover_icon;
MyTextView tv_author_name;
MyTextView item_discover_descrip;
MyTextView_medium tv_discover_heading;
//For Events
public ImageView event_icon;
public MyTextView_medium item_event_time;
MyTextView item_event_date;
MyTextView item_event_descrip;
MyTextView_medium item_event_heading;
//For Native Ads
FrameLayout frameLayout;
public MyViewHolder(final View itemView) {
super(itemView);
//For Discover
iv_discover_icon = (ImageView) itemView.findViewById(R.id.iv_discover_icon);
tv_author_name = (MyTextView) itemView.findViewById(R.id.tv_author_name);
tv_discover_heading = (MyTextView_medium) itemView.findViewById(R.id.tv_discover_heading);
item_discover_descrip = (MyTextView) itemView.findViewById(R.id.item_discover_descrip);
itemView.setOnClickListener(this);
//For Events
event_icon = (ImageView) itemView.findViewById(R.id.event_icon);
item_event_date = (MyTextView) itemView.findViewById(R.id.item_event_date);
item_event_time = (MyTextView_medium) itemView.findViewById(R.id.item_event_time);
item_event_heading = (MyTextView_medium) itemView.findViewById(R.id.item_event_heading);
item_event_descrip = (MyTextView) itemView.findViewById(R.id.item_event_descrip);
itemView.setOnClickListener(this);
//For Native Ads
frameLayout = (FrameLayout) itemView.findViewById(R.id.fl_adplaceholder);
}
#Override
public void onClick(View v) {
if ((beanList.get(getAdapterPosition()).getType()) == ConstantUrl.TYPE_EVENT) {
Intent i = new Intent(context, Events.class);
i.putExtra("event_details", beanList.get(getAdapterPosition()).getEventDetails());
context.startActivity(i);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(beanList.get(getAdapterPosition()).getDiscoverDetails().getUrl()));
context.startActivity(intent);
}
}
}
private LayoutInflater getLayoutInflater() {
AppLog.Log("classmate5", "inside_getLayoutInflater" + LayoutInflater.from(context));
return LayoutInflater.from(context);
}
private void populateContentAdView(NativeContentAd nativeContentAd,
NativeContentAdView adView) {
AppLog.Log("classmate5", "inside_populateContentAdView");
adView.setHeadlineView(adView.findViewById(R.id.contentad_headline));
adView.setImageView(adView.findViewById(R.id.contentad_image));
adView.setBodyView(adView.findViewById(R.id.contentad_body));
//adView.setCallToActionView(adView.findViewById(R.id.contentad_call_to_action));
//adView.setLogoView(adView.findViewById(R.id.contentad_logo));
//adView.setAdvertiserView(adView.findViewById(R.id.contentad_advertiser));
// Some assets are guaranteed to be in every NativeContentAd.
((MyTextView_medium) adView.getHeadlineView()).setText(nativeContentAd.getHeadline());
((MyTextView) adView.getBodyView()).setText(nativeContentAd.getBody());
//((TextView) adView.getCallToActionView()).setText(nativeContentAd.getCallToAction());
//((TextView) adView.getAdvertiserView()).setText(nativeContentAd.getAdvertiser());
List<NativeAd.Image> images = nativeContentAd.getImages();
if (images.size() > 0) {
((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable());
}
// Some aren't guaranteed, however, and should be checked.
//NativeAd.Image logoImage = nativeContentAd.getLogo();
/*if (logoImage == null) {
//adView.getLogoView().setVisibility(View.INVISIBLE);
} else {
((ImageView) adView.getLogoView()).setImageDrawable(logoImage.getDrawable());
//adView.getLogoView().setVisibility(View.VISIBLE);
}*/
// Assign native ad object to the native view.
adView.setNativeAd(nativeContentAd);
}
}
I want to know where to add a check to hide and unhide the items. Lets say I got 6 items. So, in the recyclerview it should show 9 items including the native ads. (every 3rd position).

if (position == 0 || (position + 1) % 3 == 0) { //add your nativeAdd here }

Related

java.lang.ArrayIndexOutOfBoundsException: length=22; index=-1

i am very new to Android Studio and making app with my WordPress website and using Rest Api to fetch data from my website , all works fine and i have added sort posts menu in my app and users can sort posts by "most views" "most commented" etc. and it also works fine but the problem occurred when i load all posts in app , like i have 12 posts in my website and when i go to end in app and then i click on button sort by "most views" or "most commented" then i see this error ..
java.lang.ArrayIndexOutOfBoundsException: length=22; index=-1
at java.util.ArrayList.remove(ArrayList.java:506)
at com.testtest.test.PostAdapter.showHideProgress(PostAdapter.java:158)
at com.testtest.test.MainActivity$7.onResponse(MainActivity.java:293)
this is PostAdapter
private List<Model> dataset;
private Context mContext;
int total_types;
int viewTypeData = 0,viewTypeProgress = 2 ;
private static final int AD = 1;
private static final int LIST_AD_DELTA = 4;
AdView adview;
public PostAdapter(List<Model> list){
dataset = list;
}
#NotNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NotNull ViewGroup parent, int viewType) {
mContext = parent.getContext();
if (viewType == AD) {
adview = new AdView(mContext);
adview.setAdSize( AdSize.LARGE_BANNER);
adview.setAdUnitId(mContext.getResources().getString(R.string.banner_ad_unit_id));
float density = mContext.getResources().getDisplayMetrics().density;
int height = Math.round(AdSize.LARGE_BANNER.getHeight() * density);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, height);
adview.setLayoutParams(params);
new RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("C5468C2B8941779E34B14EB796B9EA1B"));
AdRequest request = new AdRequest.Builder().build();
adview.loadAd(request);
return new Holder(adview);
}
if (viewType == viewTypeData){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_list,parent,false);
return new MyDataHolder(view);
}else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_loading,parent,false);
return new MyProgressHolder(view);
}
}
public void clearApplications() {
int size = this.dataset.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
dataset.remove(0);
}
this.notifyItemRangeRemoved(0, size);
}
}
#Override
public void onBindViewHolder(#NotNull RecyclerView.ViewHolder holder, int position) {
Model model = dataset.get(getRealPosition(position));
String tagID = model.tagsid;
if (holder instanceof MyDataHolder){
((MyDataHolder)holder).showModel(model);
Glide.with(mContext)
.load(model.imageUrl)
.dontAnimate()
.placeholder(R.drawable.icon)
.into( ((MyDataHolder)holder).imageView);
}
}
private int getRealPosition(int position) {
if (LIST_AD_DELTA == 0) {
return position;
} else {
return position - position / LIST_AD_DELTA;
}
}
#Override
public int getItemCount() {
return dataset.size();
}
#Override
public int getItemViewType(int position){
if (position > 0 && position % LIST_AD_DELTA == 0) {
return AD;
}
return viewTypeData;
}
void showHideProgress(boolean shouldShowProgress){
if (shouldShowProgress){
dataset.add(new Model("progress","","","","","","","","","",""));
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
notifyItemInserted(dataset.size()-1);
}
};
handler.post(r);
}else {
dataset.remove(dataset.size()-1);
notifyItemRemoved(dataset.size()-1);
}
}
void addItemsToList(List<Model> newItems){
if (dataset.isEmpty()){
dataset.addAll(newItems);
notifyDataSetChanged();
}else {
int lastItemPosition = dataset.size() -1;
dataset.addAll(newItems);
notifyItemRangeInserted(lastItemPosition,newItems.size());
}
}
class MyDataHolder extends RecyclerView.ViewHolder{
TextView titleView,subTitleView,postViews,tagsView,date;
ImageView imageView;
ImageButton sharebtn;
CardView rootView;
// Use Glide to load image to image view
public MyDataHolder(#NotNull View itemView) {
super(itemView);
titleView = itemView.findViewById(R.id.title);
date = itemView.findViewById(R.id.date);
subTitleView = itemView.findViewById(R.id.subtitle);
postViews = itemView.findViewById(R.id.postview);
tagsView = itemView.findViewById(R.id.tags);
imageView = itemView.findViewById(R.id.Icon);
sharebtn = itemView.findViewById(R.id.share);
rootView = itemView.findViewById(R.id.recycler_item_root_view);
tagsView.setOnClickListener(v -> {
Bundle bundle = new Bundle();
bundle.putParcelable("my_key",dataset.get(getRealPosition(getAdapterPosition())));
Model model = bundle.getParcelable("my_key");
if (model != null) {
String tagsID = model.tagsid;
tagsID = tagsID.replace("[", "");
tagsID = tagsID.replace("]", "");
String newTags = null;
if(tagsID.contains(".")){
newTags = tagsID.substring(0, tagsID.indexOf("."));
} else {
newTags = tagsID;
}
String tagsName = model.tags;
tagsID = tagsID.replace("[", "");
tagsID = tagsID.replace("]", "");
Intent webviewIntent = new Intent(mContext, TagsActivity.class);
webviewIntent.putExtra("TagsID", newTags);
webviewIntent.putExtra("TagsName", tagsName);
webviewIntent.putExtra("Toolbar", "Notification");
mContext.startActivity(webviewIntent);
}
});
sharebtn.setOnClickListener(v -> {
Bundle bundle = new Bundle();
bundle.putParcelable("my_key",dataset.get(getRealPosition(getAdapterPosition())));
Model model = bundle.getParcelable("my_key");
if (model != null) {
String content = String.valueOf((Html.fromHtml(model.content)));
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,
model.title + "\n" + content + "\n" +
mContext.getResources().getString(R.string.shareapp) + " " +
mContext.getResources().getString(R.string.shareurl));
sendIntent.setType("text/plain");
mContext.startActivity(sendIntent);
}
});
rootView.setOnClickListener(v -> {
Bundle bundle = new Bundle();
bundle.putParcelable("my_key",dataset.get(getRealPosition(getAdapterPosition())));
Intent intent = new Intent(mContext, PostDetails.class);
intent.putExtra("my_bundle",bundle);
mContext.startActivity(intent);
});
}
void showModel(Model model){
titleView.setText(model.title);
tagsView.setText(model.tags);
date.setText(model.date);
subTitleView.setText(model.subTitle);
postViews.setText(model.commentcount);
}
}
static class MyProgressHolder extends RecyclerView.ViewHolder{
ProgressBar progressBar;
public MyProgressHolder(#NotNull View itemView) {
super(itemView);
progressBar = itemView.findViewById(R.id.progressbar);
}
void show(){
progressBar.setVisibility(View.VISIBLE);
}
}
public class Holder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public Holder(View itemView) {
super(itemView);
}
}
}
and this is how i fetch most viewed posts
if (id == R.id.action_view) {
adapter.clearApplications();
yourURL = baseURL + mostView;
getRetrofit();
tv.setVisibility(View.VISIBLE);
return true;
}
i have tried this
public void clearApplications() {
int size = this.dataset.size() -1;
if (size > 0) {
for (int i = 0; i < size; i++) {
dataset.remove(0);
}
this.notifyItemRangeRemoved(0, size);
}
}
but it did not work properly ,, please help

Not able to display items properly in recycler view (pagination issue)

When I try to load more items when the user had scrolled to the end, a progress dialog is displayed and removed and then again displayed and then more items are displayed but progress dialog is still not removed from recycler view. And sometimes progress dialog is removed but still, it takes more time to display more items in recycler view. I'm using firestore addOnSuccessListener to retrieve items from cloud firestore and adding it to an arraylist and then adding native ads advanced to that arraylist and add the data to the recycler adapter.
Activity where recycler view items are displayed, MovieLists.java
public class MovieLists extends AppCompatActivity implements ItemInterface{
//nothing important just a comment
boolean runshowMovieOnce = true;
Query mQuery;
int loaderPosition;
DocumentSnapshot lastVisible;
String docId, catName, titleName;
ItemInterface itemInterface;
DatabaseReference listData;
String VideoId;
MovieListAdapter movieListAdapter;
RecyclerView movieLists;
// List of native ads that have been successfully loaded.
private List<UnifiedNativeAd> mNativeAds = new ArrayList<>();
List<Object> itemDatas = new ArrayList<>();
LinearLayoutManager layoutManager;
// The number of native ads to load.
public static final int NUMBER_OF_ADS = 2;
// The AdLoader used to load ads.
private AdLoader adLoader;
int index = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_lists);
MaterialToolbar mToolBar = (MaterialToolbar) findViewById(R.id.imagePreviewToolbar);
setSupportActionBar(mToolBar);
docId = getIntent().getStringExtra("documentId");
catName = getIntent().getStringExtra("catName");
titleName = getIntent().getStringExtra("titleName");
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_button);
getSupportActionBar().setTitle(titleName);
}
movieLists = (RecyclerView)findViewById(R.id.movie_lists);
layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
itemInterface = this;
loadMovies();
}
#Override
public void onFirebaseLoadSuccess(List<Object> itemDataList) {
loadNativeAds();
}
#Override
public void onFirebaseLoadFailed(String message) {
}
#SuppressLint("MissingPermission")
private void loadNativeAds() {
AdLoader.Builder builder = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/8135179316");
adLoader = builder.forUnifiedNativeAd(
new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// A native ad loaded successfully, check if the ad loader has finished loading
// and if so, insert the ads into the list.
mNativeAds.add(unifiedNativeAd);
if (!adLoader.isLoading()) {
insertAdsInMenuItems();
}
}
}).withAdListener(
new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
// A native ad failed to load, check if the ad loader has finished loading
// and if so, insert the ads into the list.
Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
+ " load another.");
if (!adLoader.isLoading()) {
insertAdsInMenuItems();
}
}
}).build();
// Load the Native ads.
adLoader.loadAds(new AdRequest.Builder().build(), NUMBER_OF_ADS);
}
private void insertAdsInMenuItems() {
if (mNativeAds.size() <= 0) {
return;
}
int offset = 3;
for (UnifiedNativeAd ad : mNativeAds) {
if (index<itemDatas.size()) {
itemDatas.add(index, ad);
}
index = index + offset;
}
//index = itemDatas.size() - 3;
if (runshowMovieOnce){
showMoviesList();
}
runshowMovieOnce = false;
}
private void showMoviesList() {
movieLists.setLayoutManager(layoutManager);
movieListAdapter = new MovieListAdapter(movieLists,MovieLists.this,itemDatas);
movieLists.setHasFixedSize(true);
movieLists.setAdapter(movieListAdapter);
movieListAdapter.setLoadMore(new ILoadMore() {
#Override
public void onLoadMore() {
itemDatas.add(null);
movieLists.post(new Runnable() {
public void run() {
movieListAdapter.notifyItemInserted(itemDatas.size() - 1);
Log.d("load position insert", ""+(itemDatas.size()-1));
}
});
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
itemDatas.remove(itemDatas.size()-1);
movieListAdapter.notifyItemRemoved(itemDatas.size() - 1);
Log.d("load position rem", ""+(itemDatas.size()-1));
int previousListSize = itemDatas.size();
Log.d("previousListSize",""+previousListSize);
loadMovies();
int newListSize = itemDatas.size();
Log.d("newListSize",""+newListSize);
movieListAdapter.notifyDataSetChanged();
movieListAdapter.setLoaded();
}
},5000);
}
});
}
private void loadMovies() {
FirebaseFirestore mFirestore = FirebaseFirestore.getInstance();
CollectionReference mPostsCollection = mFirestore.collection(catName).document(docId).collection("listItem");
//Defining Query cursors
if (lastVisible == null){
mQuery = mPostsCollection.orderBy("pr", Query.Direction.ASCENDING)
.limit(6);
}else {
mQuery = mPostsCollection.orderBy("pr", Query.Direction.ASCENDING)
.startAfter(lastVisible)
.limit(6);
}
mQuery.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot documentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : documentSnapshots){
ItemData itemData = documentSnapshot.toObject(ItemData.class);
itemDatas.add(itemData);
Log.d("movie name",itemData.getVn());
}
// Get the last visible document
if (documentSnapshots.size() > 0)
lastVisible = documentSnapshots.getDocuments()
.get(documentSnapshots.size() -1);
onFirebaseLoadSuccess(itemDatas);
}
});
}
}
MovieListAdapter.java
public class MovieListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<Object> itemDatas;
Context context;
String urlStart = "https://i1.ytimg.com/vi/";
String urlLast = "/mqdefault.jpg";
private static final int MOVIE_ITEM_VIEW_TYPE = 0;
private static final int UNIFIED_NATIVE_AD_VIEW_TYPE = 1;
private final int VIEW_TYPE_LOADING = 2;
boolean isLoading;
ILoadMore loadMore;
int visibleThreshold ;
int lastVisibleItem,totalItemCount;
View itemV;
public MovieListAdapter(RecyclerView recyclerView, Context context, #NonNull List<Object> itemData) {
this.itemDatas = itemData;
this.context = context;
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager)recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
visibleThreshold = linearLayoutManager.getChildCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (!isLoading && (visibleThreshold + lastVisibleItem) >= totalItemCount){
if (loadMore != null){
loadMore.onLoadMore();
isLoading = true;
}
}
}
});
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
switch (viewType) {
case UNIFIED_NATIVE_AD_VIEW_TYPE:
View unifiedNativeLayoutView = LayoutInflater.from(
parent.getContext()).inflate(R.layout.ad_unified,
parent, false);
return new UnifiedNativeAdViewHolder(unifiedNativeLayoutView);
case VIEW_TYPE_LOADING:
View viewL = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_loading, parent, false);
return new LoadingViewHolder(viewL);
case MOVIE_ITEM_VIEW_TYPE:
// Fall through.
default: {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_movie_list, parent, false);
return new MovieListAdapter.MovieListsMyViewHolder(view);
}
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder myViewHolder, int i) {
//int viewType = getItemViewType(i);
if (myViewHolder instanceof MovieListAdapter.MovieListsMyViewHolder){
MovieListsMyViewHolder movieListsMyViewHolder = (MovieListsMyViewHolder) myViewHolder;
ItemData model = (ItemData)itemDatas.get(i);
movieListsMyViewHolder.txt_item_title.setText(model.getVn());
String urlImage = urlStart + model.getVi() + urlLast;
Log.e("urlImage", urlImage);
Glide.with(context)
.load(urlImage)
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.into(movieListsMyViewHolder.img_title);
movieListsMyViewHolder.setiItemClickListener(new IItemClickListener() {
#Override
public void onItemclickListener(View view, int position) {
ItemData itemData = new ItemData(model.getVi(),model.getVn(),model.getVd(),model.getPr(),model.getTc(),model.getVvc());
Log.d("Tmdb ID",String.valueOf(model.getTc()));
MovieDetails.startActivity(context, ((MovieListsMyViewHolder) myViewHolder).transformationLayout, itemData);
}
});
}else if (myViewHolder instanceof UnifiedNativeAdViewHolder){
Object recyclerViewItems = itemDatas.get(i);
UnifiedNativeAd nativeAd = (UnifiedNativeAd) recyclerViewItems;
populateNativeAdView(nativeAd, ((UnifiedNativeAdViewHolder) myViewHolder).getAdView());
}else if (myViewHolder instanceof LoadingViewHolder){
LoadingViewHolder loadingViewHolder = (LoadingViewHolder)myViewHolder;
loadingViewHolder.progressBar.setIndeterminate(true);
}
}
#Override
public int getItemCount() {
return itemDatas.size();
}
public class MovieListsMyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
AppCompatTextView txt_item_title;
ImageView img_title;
IItemClickListener iItemClickListener;
CardView itemCardView;
TransformationLayout transformationLayout;
public void setiItemClickListener(IItemClickListener iItemClickListener) {
this.iItemClickListener = iItemClickListener;
}
public MovieListsMyViewHolder(#NonNull View itemView) {
super(itemView);
itemV = itemView;
txt_item_title = (AppCompatTextView) itemView.findViewById(R.id.item_movie_Title);
img_title = (ImageView)itemView.findViewById(R.id.item_movie_Image);
//itemCardView = (CardView)itemView.findViewById(R.id.item_cardview);
//itemCardView.setOnClickListener(this);
transformationLayout = (TransformationLayout)itemView.findViewById(R.id.transformationMovieLayout);
transformationLayout.setOnClickListener(this);
}
#Override
public void onClick(View view){
iItemClickListener.onItemclickListener(view,getAdapterPosition());
}
}
private void populateNativeAdView(UnifiedNativeAd nativeAd,
UnifiedNativeAdView adView) {
// Some assets are guaranteed to be in every UnifiedNativeAd.
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
((TextView) adView.getBodyView()).setText(nativeAd.getBody());
((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
// These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
// check before trying to display them.
NativeAd.Image icon = nativeAd.getIcon();
if (icon == null) {
adView.getIconView().setVisibility(View.INVISIBLE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(icon.getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
if (nativeAd.getPrice() == null) {
adView.getPriceView().setVisibility(View.INVISIBLE);
} else {
adView.getPriceView().setVisibility(View.VISIBLE);
((TextView) adView.getPriceView()).setText(nativeAd.getPrice());
}
if (nativeAd.getStore() == null) {
adView.getStoreView().setVisibility(View.INVISIBLE);
} else {
adView.getStoreView().setVisibility(View.VISIBLE);
((TextView) adView.getStoreView()).setText(nativeAd.getStore());
}
if (nativeAd.getStarRating() == null) {
adView.getStarRatingView().setVisibility(View.INVISIBLE);
} else {
((RatingBar) adView.getStarRatingView())
.setRating(nativeAd.getStarRating().floatValue());
adView.getStarRatingView().setVisibility(View.VISIBLE);
}
if (nativeAd.getAdvertiser() == null) {
adView.getAdvertiserView().setVisibility(View.INVISIBLE);
} else {
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
adView.getAdvertiserView().setVisibility(View.VISIBLE);
}
// Assign native ad object to the native view.
adView.setNativeAd(nativeAd);
}
#Override
public int getItemViewType(int position) {
//DocumentSnapshot itemData = getItem(position);
Object recyclerViewItem = itemDatas.get(position);
if (recyclerViewItem instanceof UnifiedNativeAd) {
return UNIFIED_NATIVE_AD_VIEW_TYPE;
}else if (itemDatas.get(position) == null){
return VIEW_TYPE_LOADING;
}else {
return MOVIE_ITEM_VIEW_TYPE;
}
}
public void setLoadMore(ILoadMore loadMore){
this.loadMore = loadMore;
}
private class LoadingViewHolder extends RecyclerView.ViewHolder {
ProgressBar progressBar;
public LoadingViewHolder(#NonNull View itemView) {
super(itemView);
progressBar = itemView.findViewById(R.id.progressBar);
}
}
public void setLoaded(){
isLoading = false;
}
}
The short term answer: There's a lot going on in your code. If you just want to remove the loadingProgress, add a void viewsLoaded() method inside your LoadingViewHolder with progressbar.setVisibility(View.GONE), and inside onBindViewHolder method, on the if(myViewHolder instanceof LoadingViewHolder), call the viewsLoaded() method if viewHolder is not positioned as the last one on the Adapter.
The long term answer: If you want to enhance the performance, read about how to use DiffUtil.ItemCallback<> instead of using notifydatasetchanged(), which is used for simple small datasets. You have a list of objectData on your activity and another one on your adapter, read about using LiveData, databinding and MVVM pattern.
i suggest first of all you should get The linearLayoutManager inside The Scroll View Listener.
another suggestion is to not call movieListAdapter.notifyItemInserted(itemDatas.size() - 1); Async inside the Runnable because after you add a null into list The Adapter should be notify:
itemsData.add(null);
movieListAdapter.notifyItemInserted(itemsData.size() - 1);
another is in the line: itemDatas.remove(itemDatas.size()-1)
after you remove the item from list, The List size will decrease by one.
so this would be correct:
movieListAdapter.notifyItemRemoved(itemDatas.size());
i Hope these would help.

add admob native ad in a recyclereview

I am trying to ad the admob's native ad in a recyclerview fragment, which is displayed inside a viewpager.
My fragment looks like:
public class SunFragment extends Fragment {
public static final int NUMBER_OF_ADS = 1;
private AdLoader adLoader;
private List<Object> mRecyclerViewItems = new ArrayList<>();
private List<UnifiedNativeAd> mNativeAds = new ArrayList<>();
ArrayList<Object> sunsList;
Typeface sunfont;
Double Dlat;
Double Dlang;
//to be called by the MainActivity
public SunFragment() {
// Required empty public constructor
}
private static final String KEY_LOCATION_NAME = "location_name";
public String TAG = "SunFragment";
public String location;//="No location name found";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
location = savedInstanceState.getCharSequence(KEY_LOCATION_NAME).toString();
}
setRetainInstance(true);
loadNativeAds();
//MainActivity activity = (MainActivity) getActivity();
mRecyclerViewItems = this.getRecyclerViewItems();
}
// public List<Object> getRecyclerViewItems() {
// return mRecyclerViewItems;
// }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
sunsList = new ArrayList<>();
sunsList.add(
new SunSession(...//Populate sunsList one by one//...)
View rootView = inflater.inflate(R.layout.fragment_sun, container, false);
RecyclerView rv = rootView.findViewById(R.id.rv_recycler_view);
rv.setNestedScrollingEnabled(false);
rv.setHasFixedSize(true);
RecyclerView.LayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
// System.out.println("location " + location);
setRetainInstance(true);
RecyclerView.Adapter adapter = new SunAdapter(getContext(), sunsList);
rv.setAdapter(adapter);
return rootView;
}
#Override
public void onSaveInstanceState(Bundle locState) {
super.onSaveInstanceState(locState);
if (location != null) {
locState.putCharSequence(KEY_LOCATION_NAME, location);
}
}
public List<Object> getRecyclerViewItems() {
return mRecyclerViewItems;
}
private void insertAdsInMenuItems() {
if (mNativeAds.size() <= 0) {
return;
}
int offset = (mRecyclerViewItems.size() / mNativeAds.size()) + 1;
int index = 0;
for (UnifiedNativeAd ad : mNativeAds) {
mRecyclerViewItems.add(index, ad);
index = index + offset;
}
//loadMenu();
}
private void loadNativeAds() {
Toast.makeText(getContext(), "Here I am", Toast.LENGTH_LONG).show();
AdLoader.Builder builder = new AdLoader.Builder(getContext(), getString(R.string.ad_unit_id));
adLoader = builder.forUnifiedNativeAd(
new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// A native ad loaded successfully, check if the ad loader has finished loading
// and if so, insert the ads into the list.
mNativeAds.add(unifiedNativeAd);
if (!adLoader.isLoading()) {
insertAdsInMenuItems();
}
}
}).withAdListener(
new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
// A native ad failed to load, check if the ad loader has finished loading
// and if so, insert the ads into the list.
Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
+ " load another.");
if (!adLoader.isLoading()) {
insertAdsInMenuItems();
}
}
}).build();
// Load the Native ads.
adLoader.loadAds(new AdRequest.Builder().build(), NUMBER_OF_ADS);
}
}
and my adapter is:
public class SunAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context mContext;
private final ArrayList<Object> mSunsList;
private static final int MENU_ITEM_VIEW_TYPE = 0;
private static final int UNIFIED_NATIVE_AD_VIEW_TYPE = 1;
public SunAdapter(Context context, ArrayList<Object> sunsList) {
this.mContext = context;
this.mSunsList = sunsList;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView textViewId;
private TextView textViewSicon;
private TextView textViewStime;
private TextView textViewEicon;
private TextView textViewEtime;
private TextView ntextViewSicon;
private TextView ntextViewStime;
private TextView ntextViewEicon;
private TextView ntextViewEtime;
MyViewHolder(View itemView) {
super(itemView);
textViewId = itemView.findViewById(R.id.tv_city);
textViewSicon = itemView.findViewById(R.id.tv_MSicon);
textViewStime = itemView.findViewById(R.id.tv_MStime);
textViewEicon = itemView.findViewById(R.id.tv_MEicon);
textViewEtime = itemView.findViewById(R.id.tv_MEtime);
ntextViewSicon = itemView.findViewById(R.id.tv_NSicon);
ntextViewStime = itemView.findViewById(R.id.tv_NStime);
ntextViewEicon = itemView.findViewById(R.id.tv_NEicon);
ntextViewEtime = itemView.findViewById(R.id.tv_NEtime);
// mSportsImage = itemView.findViewById(R.id.sportsImage);
}
}
#Override
public int getItemCount() {
return mSunsList.size();
}
#Override
public int getItemViewType(int position) {
Object sunList = mSunsList.get(position);
if (sunList instanceof UnifiedNativeAd) {
return UNIFIED_NATIVE_AD_VIEW_TYPE;
}
return MENU_ITEM_VIEW_TYPE;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
switch (viewType) {
case UNIFIED_NATIVE_AD_VIEW_TYPE:
View unifiedNativeLayoutView = LayoutInflater.from(
viewGroup.getContext()).inflate(R.layout.ad_unified,
viewGroup, false);
return new UnifiedNativeAdViewHolder(unifiedNativeLayoutView);
case MENU_ITEM_VIEW_TYPE:
// Fall through.
default:
View menuItemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.card_item, viewGroup, false);
return new MyViewHolder(menuItemLayoutView);
}
}
// #RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case UNIFIED_NATIVE_AD_VIEW_TYPE:
UnifiedNativeAd nativeAd = (UnifiedNativeAd) mSunsList.get(position);
populateNativeAdView(nativeAd, ((UnifiedNativeAdViewHolder) holder).getAdView());
break;
case MENU_ITEM_VIEW_TYPE:
// fall through
default:
MyViewHolder myViewHolder = (MyViewHolder) holder;
SunSession sunSession = (SunSession) mSunsList.get(position);
myViewHolder.textViewId.setText(sunSession.getId());
myViewHolder.textViewSicon.setText(HtmlCompat.fromHtml(sunSession.getSicon(), HtmlCompat.FROM_HTML_MODE_LEGACY));
myViewHolder.textViewStime.setText(sunSession.getStime());
myViewHolder.textViewEicon.setText(HtmlCompat.fromHtml(sunSession.getEicon(), HtmlCompat.FROM_HTML_MODE_LEGACY));
myViewHolder.textViewEtime.setText(sunSession.getEtime());
myViewHolder.ntextViewSicon.setText(HtmlCompat.fromHtml(sunSession.getNSicon(), HtmlCompat.FROM_HTML_MODE_LEGACY));
myViewHolder.ntextViewStime.setText(sunSession.getNStime());
myViewHolder.ntextViewEicon.setText(HtmlCompat.fromHtml(sunSession.getNEicon(), HtmlCompat.FROM_HTML_MODE_LEGACY));
myViewHolder.ntextViewEtime.setText(sunSession.getNEtime());
}
}
private void populateNativeAdView(UnifiedNativeAd nativeAd,
UnifiedNativeAdView adView) {
// Some assets are guaranteed to be in every UnifiedNativeAd.
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
((TextView) adView.getBodyView()).setText(nativeAd.getBody());
((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
// These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
// check before trying to display them.
NativeAd.Image icon = nativeAd.getIcon();
if (icon == null) {
adView.getIconView().setVisibility(View.INVISIBLE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(icon.getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
if (nativeAd.getPrice() == null) {
adView.getPriceView().setVisibility(View.INVISIBLE);
} else {
adView.getPriceView().setVisibility(View.VISIBLE);
((TextView) adView.getPriceView()).setText(nativeAd.getPrice());
}
if (nativeAd.getStore() == null) {
adView.getStoreView().setVisibility(View.INVISIBLE);
} else {
adView.getStoreView().setVisibility(View.VISIBLE);
((TextView) adView.getStoreView()).setText(nativeAd.getStore());
}
if (nativeAd.getStarRating() == null) {
adView.getStarRatingView().setVisibility(View.INVISIBLE);
} else {
((RatingBar) adView.getStarRatingView())
.setRating(nativeAd.getStarRating().floatValue());
adView.getStarRatingView().setVisibility(View.VISIBLE);
}
if (nativeAd.getAdvertiser() == null) {
adView.getAdvertiserView().setVisibility(View.INVISIBLE);
} else {
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
adView.getAdvertiserView().setVisibility(View.VISIBLE);
}
// Assign native ad object to the native view.
adView.setNativeAd(nativeAd);
}
}
The ad has been initialized in MainActivity as:
MobileAds.initialize(this, getString(R.string.admob_app_id));
The code shows no error, and the SunList array (i.e. MENU_ITEM_VIEW_TYPE) is shown properly.
But the ad view (i.e. UNIFIED_NATIVE_AD_VIEW_TYPE) is showing no sign, not even as a place holder.
The fragment is called by the MainActivity as a viewpager (as generated by default tabbed activity) as:
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this,
getSupportFragmentManager(), Lat, Long);
What I am doing wrong here?
NB: I am using firebase
Update
I suspect,
#Override
public int getItemViewType(int position) {
Object sunList = mSunsList.get(position);
if (sunList instanceof UnifiedNativeAd) {
System.out.println("==================Hello==============");
return UNIFIED_NATIVE_AD_VIEW_TYPE;
}
return MENU_ITEM_VIEW_TYPE;
}
is never true. The string has never printed
UPDATE Probably I have found the problem, but don't know how to solve it:
If I change the getItemViewType as:
public int getItemViewType(int position) {
Object sunList = mSunsList.get(position);
// if (sunList instanceof UnifiedNativeAd) {
if (position%3==0){
System.out.println("=================="+position+"==============");
return UNIFIED_NATIVE_AD_VIEW_TYPE;
}
return MENU_ITEM_VIEW_TYPE;
}
Then I get the error:
java.lang.ClassCastException: com.example.phocast.ui.main.SunSession cannot be cast to com.google.android.gms.ads.formats.UnifiedNativeAd
In this line in onBindViewHolder:
UnifiedNativeAd nativeAd = (UnifiedNativeAd) mSunsList.get(position);
Kindly help
you can load add when add show in recyclerview. not in activity
I think your native is not loading properly.
1) Make sure your ads inserting in recycleview. add log in all method and confirm it is loading properly.
2) During test app use testing ads id.
3) After LoadAds update recycleview with
adapter.notifyDataSetChanged();
4) There is no issue in below method.
#Override
public int getItemViewType(int position) {
Object sunList = mSunsList.get(position);
if (sunList instanceof UnifiedNativeAd) {
System.out.println("==================Hello==============");
return UNIFIED_NATIVE_AD_VIEW_TYPE;
}
return MENU_ITEM_VIEW_TYPE;
}
If still not getting ads please let me know.

RecycleView repeat action on one recycleview card on other

I am developing one quiz android application in which questions and their respective options and answer will be fetched from server and added to recycle view.
I have done coding of same and it is working except one problem that when i choose any option i turn its background green if it is right or i turn it to red if it is wrong but when i do same for any question same action is been transformed on other questions option in recycle view.
As i click on Option A
6th question from previous solved question have option A automatically selected
I am posting my Adapter and Fragment code for same.
Adapter code:-
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(context);
Log.e("I : ", "Creating " );
switch (viewType) {
case QUESTION:
View viewQuestion = inflater.inflate(R.layout.question_card, parent, false);
viewHolder = new QuestionFeed(viewQuestion);
break;
case LOADING:
View viewLoading = inflater.inflate(R.layout.item_progress, parent, false);
viewHolder = new LoadingVH(viewLoading);
break;
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final PostRead result = postResults.get(position);
String catname = null;
try {
catname = dataBaseHelper.getCatName(result.getCat());
} catch (SQLException e) {
e.printStackTrace();
}
Log.e("Position: ", "" + position);
switch (getItemViewType(position)) {
case QUESTION:
final QuestionFeed textFeed = (QuestionFeed) holder;
textFeed.question.setText(result.getQuestion());
textFeed.optionA.setText(result.getA());
textFeed.optionB.setText(result.getB());
textFeed.optionC.setText(result.getC());
textFeed.optionD.setText(result.getD());
textFeed.answerView.setText(catname);
textFeed.time.setText("" + TimeAgo.getTimeAgo((10000000000000L - result.getTimestamp())));
textFeed.optionA_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (result.getAnswer().equals("a")) {
Log.e("AAAAnswer- ", result.getAnswer());
textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else {
Log.e("Answer- ", result.getAnswer());
textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.wrong_answer));
if (result.getAnswer().equals("b")) {
textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else if (result.getAnswer().equals("c")) {
textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else if (result.getAnswer().equals("d")) {
textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
}
}
if (textFeed.descView.getVisibility() == View.GONE) {
textFeed.descView.setVisibility(View.VISIBLE);
}
}
});
textFeed.optionB_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (result.getAnswer().equals("b")) {
Log.e("BBBAnswer- ", result.getAnswer());
textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else {
textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.wrong_answer));
if (result.getAnswer().equals("a")) {
textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else if (result.getAnswer().equals("c")) {
textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else if (result.getAnswer().equals("d")) {
textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
}
}
if (textFeed.descView.getVisibility() == View.GONE) {
textFeed.descView.setVisibility(View.VISIBLE);
}
}
});
textFeed.optionC_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (result.getAnswer().equals("c")) {
Log.e("CCCAnswer- ", result.getAnswer());
textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else {
textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.wrong_answer));
if (result.getAnswer().equals("b")) {
textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else if (result.getAnswer().equals("a")) {
textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else if (result.getAnswer().equals("d")) {
textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
}
}
if (textFeed.descView.getVisibility() == View.GONE) {
textFeed.descView.setVisibility(View.VISIBLE);
}
}
});
textFeed.optionD_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (result.getAnswer().equalsIgnoreCase("d")) {
Log.e("DDDAnswer- ", result.getAnswer());
textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else {
Log.e("DDDAnswer- ", result.getAnswer());
textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.wrong_answer));
if (result.getAnswer().equals("b")) {
textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else if (result.getAnswer().equals("c")) {
textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
} else if (result.getAnswer().equals("a")) {
textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.right_answer));
}
}
if (textFeed.descView.getVisibility() == View.GONE) {
textFeed.descView.setVisibility(View.VISIBLE);
}
}
});
break;
}
}
#Override
public int getItemCount() {
return postResults == null ? 0 : postResults.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return QUESTION;
}
public void add(PostRead r) {
postResults.add(r);
notifyItemInserted(postResults.size() - 1);
}
public void addAll(List<PostRead> moveResults) {
for (PostRead result : moveResults) {
add(result);
}
notifyDataSetChanged();
}
public void remove(PostRead r) {
int position = postResults.indexOf(r);
if (position > -1) {
postResults.remove(position);
notifyItemRemoved(position);
}
}
public void clear() {
isLoadingAdded = false;
while (getItemCount() > 0) {
remove(getItem(0));
}
}
public boolean isEmpty() {
return getItemCount() == 0;
}
public void addLoadingFooter() {
isLoadingAdded = true;
add(new PostRead());
}
public void removeLoadingFooter() {
isLoadingAdded = false;
int position = postResults.size() - 1;
PostRead result = getItem(position);
if (result != null) {
postResults.remove(position);
notifyItemRemoved(position);
}
}
public PostRead getItem(int position) {
return postResults.get(position);
}
protected class QuestionFeed extends RecyclerView.ViewHolder {
private ImageView back, share;
private MyTextView question;
private MyTextView optionA, optionB, optionC, optionD, answerView, time;
private LinearLayout optionA_layout, optionB_layout, optionC_layout, optionD_layout, errorLayout, descView;
private ProgressBar mProgress;
private Button btnRetry;
private TextView txtError;
private RelativeLayout mainLayout, backButton;
public QuestionFeed(View itemView) {
super(itemView);
back = (ImageView) itemView.findViewById(R.id.backView);
back = (ImageView) itemView.findViewById(R.id.share);
question = (MyTextView) itemView.findViewById(R.id.question);
optionA = (MyTextView) itemView.findViewById(R.id.optionA);
optionB = (MyTextView) itemView.findViewById(R.id.optionB);
optionC = (MyTextView) itemView.findViewById(R.id.optionC);
optionD = (MyTextView) itemView.findViewById(R.id.optionD);
answerView = (MyTextView) itemView.findViewById(R.id.subject);
time = (MyTextView) itemView.findViewById(R.id.time);
optionA_layout = (LinearLayout) itemView.findViewById(R.id.optionbuttonA);
optionB_layout = (LinearLayout) itemView.findViewById(R.id.optionbuttonB);
optionC_layout = (LinearLayout) itemView.findViewById(R.id.optionbuttonC);
optionD_layout = (LinearLayout) itemView.findViewById(R.id.optionbuttonD);
mProgress = (ProgressBar) itemView.findViewById(R.id.main_progress);
mainLayout = (RelativeLayout) itemView.findViewById(R.id.content_sign_up);
backButton = (RelativeLayout) itemView.findViewById(R.id.back_button);
descView = (LinearLayout) itemView.findViewById(R.id.desc);
errorLayout = (LinearLayout) itemView.findViewById(R.id.error_layout);
btnRetry = (Button) itemView.findViewById(R.id.error_btn_retry);
txtError = (TextView) itemView.findViewById(R.id.error_txt_cause);
}
}
Fragment Code
private void loadFirstPage() {
hideErrorView();
Query query = postRef.orderByChild("timestamp").startAt(firstTime).limitToFirst(14);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<PostRead> allPosts = new ArrayList<>();
childCount = (int) dataSnapshot.getChildrenCount();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
// TODO: handle the post
Log.e("Online ", "" + firstTime);
PostRead catItems = postSnapshot.getValue(PostRead.class);
catItems.setPostkey(postSnapshot.getKey());
allPosts.add(catItems);
childCount--;
if (childCount == 0) {
firstStart = 0;
dataBaseHelper.updatePosts(allPosts);
hideErrorView();
progressBar.setVisibility(View.GONE);
adapter.addAll(allPosts);
isLoading = false;
if (allPosts.size() == 0) {
isLastPage = true;
} else adapter.addLoadingFooter();
}
firstTime = catItems.getTimestamp();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
That is because Views are recycled by RecyclerView and you are setting background color to different color manually.
The solution is to set default color for background of your textFeed in BindViewHolder as:
textFeed.optionA_layout.setBackgroundColor(context.getResources().getColor(R.color.default_color));
textFeed.optionB_layout.setBackgroundColor(context.getResources().getColor(R.color.default_color));
textFeed.optionC_layout.setBackgroundColor(context.getResources().getColor(R.color.default_color));
textFeed.optionD_layout.setBackgroundColor(context.getResources().getColor(R.color.default_color));

How can I limit the number of native ads in recyclerview

How can I limit the number of native ads in recyclerview
I only want 5 native ads in first 100 lines in recyclerview (per every 20 lines).
My recyclerview have got 10000 row. It annoys the user on every 20 lines of ad output. So I want to limit the number of ads to 5. I made a lot of changes, but I could not.
Thank you in advance for your help
advertising related section in my activity
public static final int ITEMS_PER_AD = 20;
private static final int NATIVE_EXPRESS_AD_HEIGHT = 100;
private void addNativeExpressAds() {
// Loop through the items array and place a new Native Express ad in every ith position in
// the items List.
for (int i = 0; i <= mRecyclerViewItems.size(); i += ITEMS_PER_AD) {
final NativeExpressAdView adView = new NativeExpressAdView(getActivity());
mRecyclerViewItems.add(i, adView);
}
}
private void setUpAndLoadNativeExpressAds() {
mRecyclerView.post(new Runnable() {
#Override
public void run() {
try {
for (int i = 0; i <= mRecyclerViewItems.size(); i += ITEMS_PER_AD) {
final NativeExpressAdView adView = (NativeExpressAdView) mRecyclerViewItems.get(i);
AdSize adSize = new AdSize(AdSize.FULL_WIDTH, NATIVE_EXPRESS_AD_HEIGHT);
adView.setAdSize(adSize);
adView.setAdUnitId(AD_UNIT_ID);
}
loadNativeExpressAd(0);
}catch (Exception e){
}
}
});
}
private void loadNativeExpressAd(final int index) {
// if (index >= mRecyclerViewItems.size()) {
// return;
// }
if (index >= mRecyclerViewItems.size()) {
return;
}
Object item = mRecyclerViewItems.get(index);
if (!(item instanceof NativeExpressAdView)) {
throw new ClassCastException("Expected item at index " + index + " to be a Native"
+ " Express ad.");
}
final NativeExpressAdView adView = (NativeExpressAdView) item;
// Set an AdListener on the NativeExpressAdView to wait for the previous Native Express ad
// to finish loading before loading the next ad in the items list.
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
// The previous Native Express ad loaded successfully, call this method again to
// load the next ad in the items list.
if (index<24){
loadNativeExpressAd(index + ITEMS_PER_AD);
}
}
#Override
public void onAdFailedToLoad(int errorCode) {
// The previous Native Express ad failed to load. Call this method again to load
// the next ad in the items list.
Log.e("MainActivity", "The previous Native Express ad failed to load. Attempting to"
+ " load the next Native Express ad in the items list.");
//loadNativeExpressAd(index + ITEMS_PER_AD);
}
});
try {
adView.loadAd(new AdRequest.Builder().build());
}catch (Exception e){}
}
and my RecyclerViewAdapter.java
class RecyclerViewAdapter extends FastScrollRecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int MENU_ITEM_VIEW_TYPE = 0;
private static final int NATIVE_EXPRESS_AD_VIEW_TYPE = 1;
private final Context mContext;
private final List<Object> mRecyclerViewItems;
ItemClickListener listener;
public RecyclerViewAdapter(Context context, List<Object> recyclerViewItems, ItemClickListener listener) {
this.mContext = context;
this.mRecyclerViewItems = recyclerViewItems;
this.listener=listener;
}
public class MenuItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView menuItemName;
private TextView menuItemDescription;
private TextView menuItemPrice;
private TextView menuItemCategory;
private ImageView menuItemImage;
ItemClickListener itemClickListener;
MenuItemViewHolder(View view) {
super(view);
menuItemImage = (ImageView) view.findViewById(R.id.cinsiyetresmi);
menuItemName = (TextView) view.findViewById(R.id.menu_item_name);
menuItemPrice = (TextView) view.findViewById(R.id.menu_item_price);
menuItemCategory = (TextView) view.findViewById(R.id.menu_item_category);
menuItemDescription = (TextView) view.findViewById(R.id.menu_item_description);
view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
this.itemClickListener.onItemClick(v,getLayoutPosition());
}
public void setItemClickListener(ItemClickListener ic)
{
this.itemClickListener=ic;
}
}
public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {
NativeExpressAdViewHolder(View view) {
super(view);
}
}
#Override
public int getItemCount() {
return mRecyclerViewItems.size();
}
#Override
public int getItemViewType(int position) {
return (position % isimler_erkek.ITEMS_PER_AD == 0) ? NATIVE_EXPRESS_AD_VIEW_TYPE
: MENU_ITEM_VIEW_TYPE;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
switch (viewType) {
case MENU_ITEM_VIEW_TYPE:
View menuItemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.menu_item_container, viewGroup, false);
return new MenuItemViewHolder(menuItemLayoutView);
case NATIVE_EXPRESS_AD_VIEW_TYPE:
// fall through
default:
View nativeExpressLayoutView = LayoutInflater.from(
viewGroup.getContext()).inflate(R.layout.native_express_ad_container,
viewGroup, false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
MenuItem menuItem;
MenuItemViewHolder menuItemHolder;
switch (viewType) {
case MENU_ITEM_VIEW_TYPE:
menuItemHolder = (MenuItemViewHolder) holder;
menuItem = (MenuItem) mRecyclerViewItems.get(position);
// Get the menu item image resource ID.
String imageName = menuItem.getImageName();
int imageResID = mContext.getResources().getIdentifier(imageName, "drawable",
mContext.getPackageName());
// Add the menu item details to the menu item view.
menuItemHolder.menuItemImage.setImageResource(imageResID);
menuItemHolder.menuItemName.setText(menuItem.getName());
menuItemHolder.menuItemPrice.setText(menuItem.getPrice());
menuItemHolder.menuItemCategory.setText(menuItem.getCategory());
menuItemHolder.menuItemDescription.setText(menuItem.getDescription());
final int idsi= Integer.parseInt(menuItem.getDescription());
menuItemHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onItemClick(View v, int pos) {
//Snackbar.make(v,String.valueOf(players.get(pos).getId()), Snackbar.LENGTH_SHORT).show();
try {
listener.onItemClick(v,idsi);
}
catch (Exception e){
}
//Toast.makeText(this.getApplicationContext(), "bak", Toast.LENGTH_LONG).show();
}
});
break;
case NATIVE_EXPRESS_AD_VIEW_TYPE:
// fall through
default:
NativeExpressAdViewHolder nativeExpressHolder =
(NativeExpressAdViewHolder) holder;
NativeExpressAdView adView =
(NativeExpressAdView) mRecyclerViewItems.get(position);
ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView;
if (adCardView.getChildCount() > 0) {
adCardView.removeAllViews();
}
if (adView.getParent() != null) {
((ViewGroup) adView.getParent()).removeView(adView);
}
// Add the Native Express ad to the native express ad view.
adCardView.addView(adView);
}
}
}
private View root_view, parent_view;
private RecyclerView recyclerView;
private AdapterVideo mAdapter;
private SwipeRefreshLayout swipe_refresh;
private Call<CallbackRecent> callbackCall = null;
private int post_total = 0;
private int failed_page = 0;
private InterstitialAd interstitialAd;
int counter = 1;
private List<Object> ViewItems = new ArrayList<>();
// List of native ads that have been successfully loaded.
private List<UnifiedNativeAd> mNativeAds = new ArrayList<>();
// The AdLoader used to load ads.
private AdLoader adLoader;
// The number of native ads to load.
public static final int NUMBER_OF_ADS = 4;
// The number of Items to Show Native Between Items.
public static final int NUMBER_OF_ROW = 2;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root_view = inflater.inflate(R.layout.fragment_video, null);
parent_view = getActivity().findViewById(R.id.main_content);
loadInterstitialAd();
// Update the RecyclerView item's list with native ads.
loadNativeAds();
swipe_refresh = root_view.findViewById(R.id.swipe_refresh_layout_home);
swipe_refresh.setColorSchemeResources(R.color.colorOrange, R.color.colorGreen, R.color.colorBlue, R.color.colorRed);
recyclerView = root_view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true);
//set data and list adapter
// mAdapter = new AdapterVideo(getActivity(), recyclerView, new ArrayList<News>());
mAdapter = new AdapterVideo(getActivity(), recyclerView, ViewItems);
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
if (Config.ENABLE_RTL_MODE) {
recyclerView.setRotationY(180);
}
// on item list clicked
mAdapter.setOnItemClickListener(new AdapterVideo.OnItemClickListener() {
#Override
public void onItemClick(View v, News obj, int position) {
ActivityNewsDetail.navigate((MainActivity) getActivity(), v.findViewById(R.id.image), obj);
showInterstitialAd();
}
});
// detect when scroll reach bottom
mAdapter.setOnLoadMoreListener(new AdapterVideo.OnLoadMoreListener() {
#Override
public void onLoadMore(int current_page) {
if (post_total > mAdapter.getItemCount() && current_page != 0) {
int next_page = current_page + 1;
requestAction(next_page);
} else {
mAdapter.setLoaded();
}
}
});
// on swipe list
swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
if (callbackCall != null && callbackCall.isExecuted()) callbackCall.cancel();
mAdapter.resetListData();
requestAction(1);
}
});
requestAction(1);
getAllVideo();
return root_view;
}
private void getAllVideo(){
if (callbackCall != null && callbackCall.isExecuted()) callbackCall.cancel();
mAdapter.resetListData();
requestAction(1);
}
private void displayApiResult(final List<News> posts) {
mAdapter.insertData(posts);
swipeProgress(false);
if (posts.size() == 0) {
showNoItemView(true);
}
}
private void requestListPostApi(final int page_no) {
ApiInterface apiInterface = RestAdapter.createAPI();
callbackCall = apiInterface.getVideoPost(page_no, Config.LOAD_MORE);
callbackCall.enqueue(new Callback<CallbackRecent>() {
#Override
public void onResponse(Call<CallbackRecent> call, Response<CallbackRecent> response) {
CallbackRecent resp = response.body();
if (resp != null && resp.status.equals("ok")) {
post_total = resp.count_total;
displayApiResult(resp.posts);
} else {
onFailRequest(page_no);
}
}
#Override
public void onFailure(Call<CallbackRecent> call, Throwable t) {
if (!call.isCanceled()) onFailRequest(page_no);
}
});
}
private void onFailRequest(int page_no) {
failed_page = page_no;
mAdapter.setLoaded();
swipeProgress(false);
if (NetworkCheck.isConnect(getActivity())) {
showFailedView(true, getString(R.string.msg_no_network));
} else {
showFailedView(true, getString(R.string.msg_offline));
}
}
private void requestAction(final int page_no) {
showFailedView(false, "");
showNoItemView(false);
if (page_no == 1) {
swipeProgress(true);
} else {
mAdapter.setLoading();
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
requestListPostApi(page_no);
}
}, Constant.DELAY_TIME);
}
#Override
public void onDestroy() {
super.onDestroy();
swipeProgress(false);
if (callbackCall != null && callbackCall.isExecuted()) {
callbackCall.cancel();
}
}
private void showFailedView(boolean show, String message) {
View lyt_failed = root_view.findViewById(R.id.lyt_failed_home);
((TextView) root_view.findViewById(R.id.failed_message)).setText(message);
if (show) {
recyclerView.setVisibility(View.GONE);
lyt_failed.setVisibility(View.VISIBLE);
} else {
recyclerView.setVisibility(View.VISIBLE);
lyt_failed.setVisibility(View.GONE);
}
root_view.findViewById(R.id.failed_retry).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
requestAction(failed_page);
}
});
}
private void showNoItemView(boolean show) {
View lyt_no_item = root_view.findViewById(R.id.lyt_no_item_home);
((TextView) root_view.findViewById(R.id.no_item_message)).setText(R.string.msg_no_news);
if (show) {
recyclerView.setVisibility(View.GONE);
lyt_no_item.setVisibility(View.VISIBLE);
} else {
recyclerView.setVisibility(View.VISIBLE);
lyt_no_item.setVisibility(View.GONE);
}
}
private void swipeProgress(final boolean show) {
if (!show) {
swipe_refresh.setRefreshing(show);
return;
}
swipe_refresh.post(new Runnable() {
#Override
public void run() {
swipe_refresh.setRefreshing(show);
}
});
}
private void loadInterstitialAd() {
if (Config.ENABLE_ADMOB_INTERSTITIAL_ADS) {
MobileAds.initialize(getActivity(), getResources().getString(R.string.admob_app_id));
interstitialAd = new InterstitialAd(getActivity());
interstitialAd.setAdUnitId(getResources().getString(R.string.admob_interstitial_unit_id));
interstitialAd.loadAd(Tools.getAdRequest(getActivity()));
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
interstitialAd.loadAd(new AdRequest.Builder().build());
}
});
}
}
private void showInterstitialAd() {
if (Config.ENABLE_ADMOB_INTERSTITIAL_ADS) {
if (interstitialAd != null && interstitialAd.isLoaded()) {
if (counter == Config.ADMOB_INTERSTITIAL_ADS_INTERVAL) {
interstitialAd.show();
counter = 1;
} else {
counter++;
}
}
}
}
private void loadNativeAds() {
AdLoader.Builder builder = new AdLoader.Builder(getActivity(), getString(R.string.admob_native_unit_id));
adLoader = builder.forUnifiedNativeAd(
new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// A native ad loaded successfully
mNativeAds.add(unifiedNativeAd);
if (!adLoader.isLoading()) {
insertAdsInMenuItems(); //Show Native Inside Items RecyclerView
}
}
}).withAdListener(
new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
// A native ad failed to load, check if the ad loader has finished loading
Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
+ " load another.");
if (!adLoader.isLoading()) {
insertAdsInMenuItems(); //Show Native Inside Items RecyclerView
}
}
}).build();
// Load the Native ads.
adLoader.loadAds(new AdRequest.Builder().build(), NUMBER_OF_ADS);
}
// Method To Add native Inside Items :
private void insertAdsInMenuItems() {
if (mNativeAds.size() <= 0) {
return;
}
int offset = (ViewItems.size() / mNativeAds.size()) + NUMBER_OF_ROW;
int index = 0;
for (UnifiedNativeAd ad : mNativeAds) {
ViewItems.add(index, ad);
index = index + offset;
}
}

Categories

Resources