struggling with Fragment and RecycleView implement OnItemClick - java

I tried to implement OnItemClickListener in Fragment with RecycleView Adapter, but my code seems not to work well, OnItemClickListener keeps turning red when I try to change it to getContext().
I've tried to find many ways to get it done, but that makes me find another error in my code.
.
your help will make my day brighter,
public class MountainFragment extends Fragment {
private final int LOCATION_SIZE = 10;
private final int LOCATION_TYPE = 1;
public MountainFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.word_list_activity, container, false);
String name, imageName, address;
Drawable image;
int resourceId, imageId;
final ArrayList<WorldDataModel> mountainArrays = new ArrayList<WorldDataModel>();
for (int n = 1; n <= LOCATION_SIZE; n++) {
//get Location Name
resourceId = getResources().getIdentifier("location_name_" + LOCATION_TYPE
+ "_" + n, "string", getActivity().getPackageName());
name = getResources().getString(resourceId);
//get address
resourceId = getResources().getIdentifier("location_address_" + LOCATION_TYPE
+ "_" + n, "string", getActivity().getPackageName());
address = getResources().getString(resourceId);
//get image thumbnail
resourceId = getResources().getIdentifier("location_thumbnail_" + LOCATION_TYPE
+ "_" + n, "string", getActivity().getPackageName());
imageName = getResources().getString(resourceId);
imageId = getResources().getIdentifier(imageName, "drawable", getActivity().getPackageName());
image = getResources().getDrawable(imageId);
//add new data to list
mountainArrays.add(new WorldDataModel(name, address, image));
}
// using simple recycle view
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.word_list_activity);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
RecycleViewWorldAdapter adapter = new RecycleViewWorldAdapter(mountainArrays);
recyclerView.setAdapter(adapter);
//on list item click
recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
WorldDataModel worldDataModel = mountainArrays.get(position);
Intent intent = new Intent(getActivity(), DetailsActivity.class);
intent.putExtra("category", LOCATION_TYPE);
intent.putExtra("location", position + 1);
getActivity().startActivity(intent);
}
});
// Inflate the layout for this fragment
return rootView;
}
and here's my adapter class
package com.example.mytourguideapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class RecycleViewWorldAdapter extends RecyclerView.Adapter<RecycleViewWorldAdapter.MyViewHolder> {
private ArrayList<WorldDataModel> worldDataModelArrayList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textViewLocationName, textViewPlacesName;
public ImageView imageOnCard;
public MyViewHolder(View itemView) {
super(itemView);
textViewLocationName = (TextView) itemView.findViewById(R.id.location_name);
textViewPlacesName = (TextView) itemView.findViewById(R.id.places_name);
imageOnCard = (ImageView) itemView.findViewById(R.id.image_on_cardview);
}
}
public RecycleViewWorldAdapter(ArrayList<WorldDataModel> worldDataModelArrayList) {
this.worldDataModelArrayList = worldDataModelArrayList;
}
#Override
public MyViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_layout, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(RecycleViewWorldAdapter.MyViewHolder holder, int position) {
WorldDataModel worldDataModel = worldDataModelArrayList.get(position);
holder.textViewPlacesName.setText(worldDataModel.getName());
holder.textViewLocationName.setText(worldDataModel.getAddress());
holder.imageOnCard.setImageDrawable(worldDataModel.getImageThumbnail());
}
#Override
public int getItemCount() {
return worldDataModelArrayList.size();
}
}

You can send the callback from your Adapter class to your fragment/activity.
First create a interface class
public interface OnItemClickListener {
void onItemClick(WorldDataModel item,int position);
}
Next modify your Adapter class , in your case RecycleViewWorldAdapter
i) Send listener as a parameter in your constructor
private final OnItemClickListener listener;
public RecycleViewWorldAdapter(ArrayList<WorldDataModel> worldDataModelArrayList, OnItemClickListener listener) {
this.worldDataModelArrayList = worldDataModelArrayList;
this.listener = listener;
}
ii) Set onCLick listener on some view in your ViewHolder class
public MyViewHolder(View itemView) {
super(itemView);
textViewLocationName = (TextView) itemView.findViewById(R.id.location_name);
textViewPlacesName = (TextView) itemView.findViewById(R.id.places_name);
imageOnCard = (ImageView) itemView.findViewById(R.id.image_on_cardview);
// change your listener on any other view if you want
imageOnCard..setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick(worldDataModelArrayList.get(getAdapterPosition()),getAdapterPosition());
}
});
}
Now modify your Activity/Fragment class to listen to your interface callback , in your case MountainFragment in onCreateView
// using simple recycle view
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.word_list_activity);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
RecycleViewWorldAdapter adapter = new RecycleViewWorldAdapter(mountainArrays,new RecycleViewWorldAdapter.OnItemClickListener() {
#Override public void onItemClick(WorldDataModel item, int position) {
// implement click listener as per your requirement
WorldDataModel worldDataModel = item;
Intent intent = new Intent(getActivity(), DetailsActivity.class);
intent.putExtra("category", LOCATION_TYPE);
intent.putExtra("location", position + 1);
getActivity().startActivity(intent);
});
recyclerView.setAdapter(adapter);

Related

How to make 2 buttons for multiple listview elements

I'm creating an app for ordering food, and I created a Dish class and a Dish array adapter.
Currently you can add quantity for a specific dish by clicking on the view.
However, I wish to have 2 buttons to add or sub the quantity.
How can I make the 2 buttons for each dish, without the need to write for each element its ows button code? is there a way to make an "add" and "sub" methods and that the listview will know on which view it was clicked and by that update its quantity?
public Dish(String dishName, int dishPrice, int Image, int quantity) {
mdishName = dishName;
mdishPrice = dishPrice;
mdishPic = Image;
mquantity = quantity;
}
public class DishAdapter extends ArrayAdapter<Dish> {
public DishAdapter(Activity context, ArrayList<Dish> dishes){
super(context, 0, dishes);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_layout, parent, false);
}
final Dish currenDish = getItem(position);
TextView dishName = (TextView) listItemView.findViewById(R.id.dishName);
dishName.setText(currenDish.getDishName());
TextView dishPrice = (TextView) listItemView.findViewById(R.id.dishPrice);
dishPrice.setText(String.valueOf(currenDish.getDishPrice()));
ImageView image = (ImageView) listItemView.findViewById(R.id.dishPic);
image.setImageResource(currenDish.getDishPic());
image.setVisibility(View.VISIBLE);
TextView quantity = (TextView) listItemView.findViewById(R.id.quantity);
quantity.setText(String.valueOf(currenDish.getQuantity()));
return listItemView;
}
public class DesertsActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deserts_activity);
final ArrayList<Dish> dishes = new ArrayList<Dish>();
dishes.add(new Dish("Number Cake",180, R.drawable.cake_number, 0));
dishes.add(new Dish("Ear of Haman", 40, R.drawable.ozen_haman, 0));
dishes.add(new Dish("Alphachores", 35, R.drawable.alphachores, 0));
dishes.add(new Dish("Snow Cookies", 35, R.drawable.snow_cookies, 0));
DishAdapter adapter = new DishAdapter(this, dishes);
final ListView listView = (ListView) findViewById(R.id.deserts_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
dishes.set(position, new Dish(dishes.get(position).getDishName(),
dishes.get(position).getDishPrice(), dishes.get(position).getDishPic(),
dishes.get(position).getQuantity()+1));
TextView quantity = (TextView) view.findViewById(R.id.quantity);
quantity.setText(String.valueOf(dishes.get(position).getQuantity()));
}
});
}
}
You have to create a separate Adaptor Class and then apply button click listeners on the buttons. I am attaching the image for the reference.
and My product Adaptor Is
public class ProductAdaptor extends RecyclerView.Adapter<ProductAdaptor.ViewHolder> {
List<ProductsItem> productsItems;
Context context;
public ProductAdaptor(Context context, List<ProductsItem> productsItems)
{
this.context = context;
this.productsItems = productsItems;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.single_product_design,viewGroup, false);
return new ProductAdaptor.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
final ProductsItem productsItem = productsItems.get(i);
viewHolder.productname.setText(productsItem.getProductName());
Glide.with(context).load(productsItem.getProductImageUrl()).into(viewHolder.productImage);
viewHolder.stock.setText(productsItem.getStock());
viewHolder.farmername.setText(productsItem.getFarmerName());
// add listener on single Item
viewHolder.btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show();
}
});
viewHolder.btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// apply code to increment the number
// first of all get the value form text counter and increment after that bind on the UI
}
});
viewHolder.btnMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// first of all get the value form text counter and decrement after that bind on the UI
}
});
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getItemCount() {
return productsItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView productname;
ImageView productImage;
TextView stock, farmername, specility,qty;
Button btnPlus, btnMinus;
LinearLayout btnAdd;
public ViewHolder(#NonNull View itemView)
{
super(itemView);
productname = itemView.findViewById(R.id.productname);
productImage = itemView.findViewById(R.id.productimage);
stock = itemView.findViewById(R.id.stcokValue);
farmername = itemView.findViewById(R.id.farmerName);
qty = itemView.findViewById(R.id.qty);
specility = itemView.findViewById(R.id.specility);
btnPlus = itemView.findViewById(R.id.plusbutton);
btnMinus = itemView.findViewById(R.id.minusbtn);
btnAdd = itemView.findViewById(R.id.add_button);
}
}
}

Loading images in gridView sometimes mixes up

I am having a problem with my GridView wherein the images loads in different places when scrolling. I know this has something to do with recycling but as I am new to Android, I have no idea about this.
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
final Listing listing = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listing_items, parent, false);
}
final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
FirebaseStorage.getInstance().getReference().child(listing.authorUid).child(listing.adId).child(listing.images.get(0)).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
try {
GlideApp.with(getContext())
.load(new URL(uri.toString()))
.into(imageView);
} catch(Exception e) {
e.printStackTrace();
}
}
});
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), ItemInfoActivity.class);
intent.putExtra("title", listing.title);
intent.putExtra("location", listing.city);
intent.putExtra("price", listing.price);
intent.putExtra("desc", listing.description);
intent.putExtra("category", listing.category);
intent.putExtra("author", listing.authorUid);
intent.putExtra("adId", listing.adId);
intent.putExtra("authorId", listing.authorUid);
startActivity(intent);
}
});
TextView price = (TextView)convertView.findViewById(R.id.price_txt);
TextView title = (TextView)convertView.findViewById(R.id.title_txt);
title.setText(listing.title);
price.setText(listing.price +"DH");
return convertView;
}
Please advise on what to do.
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter {
ArrayList personNames;
ArrayList personImages;
Context context;
public CustomAdapter(Context context, ArrayList personNames, ArrayList personImages) {
this.context = context;
this.personNames = personNames;
this.personImages = personImages;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// infalte the item Layout
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
return vh;
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
// set the data in items
holder.name.setText(personNames.get(position));
holder.image.setImageResource(personImages.get(position));
// implement setOnClickListener event on item view.
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// open another activity on item click
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("image", personImages.get(position)); // put image data in Intent
context.startActivity(intent); // start Intent
}
});
}
#Override
public int getItemCount() {
return personNames.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
// init the item view's
TextView name;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
// get the reference of item view's
name = (TextView) itemView.findViewById(R.id.name);
image = (ImageView) itemView.findViewById(R.id.image);
}
}
}
Use this adapter structure.
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter {
ArrayList personNames;
ArrayList personImages;
Context context;
public CustomAdapter(Context context, ArrayList personNames, ArrayList personImages) {
this.context = context;
this.personNames = personNames;
this.personImages = personImages;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// infalte the item Layout
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
return vh;
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
// set the data in items
holder.name.setText(personNames.get(position));
holder.image.setImageResource(personImages.get(position));
// add your firebase code here
// implement setOnClickListener event on item view.
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// open another activity on item click
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("image", personImages.get(position)); // put image data in Intent
context.startActivity(intent); // start Intent
}
});
}
#Override
public int getItemCount() {
return personNames.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
// init the item view's
TextView name;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
// get the reference of item view's
name = (TextView) itemView.findViewById(R.id.name);
image = (ImageView) itemView.findViewById(R.id.image);
}
}
}
Here is the updated answer of above answer.
if you are using recyclerview you can not override getView() method.
i would suggest you to use recyclerview.

ViewPager inside RecycleView Adapter

I tried to add Viewpager inside RecycleView item, then I created the adapter for the recycle view and I created a Pager Adapter that extends from FragmentStatePagerAdapter class inside the the adapter.
Just the first item of the recycle works fine, but the rest items of recycle view do not show the view pager.
When I Log inside the Pager Adapter I can see all the fragment position.
When I scroll horizontally inside any pager of any recycle view item I can see the Log statement, but nothing appear on the screen.
Could any one help me in that case ?
Here is my code
public class RentCategoryItemAdapter extends RecyclerView.Adapter<RentCategoryItemAdapter.ViewHolder> {
Context mContext;
ArrayList<RentCategoryItem> categoryItems;
ArrayList<MyPagerAdapter> pagerAdapters = new ArrayList<>();
/*ArrayList<ViewPager> pagerArrayList = new ArrayList<>();
ArrayList<MyPagerAdapter> myPagerAdapterArrayList = new ArrayList<>();*/
public RentCategoryItemAdapter(Context context, ArrayList<RentCategoryItem> categoryItems) {
mContext = context;
this.categoryItems = categoryItems;
AppCompatActivity activity = (AppCompatActivity) mContext;
for (int i = 0; i < categoryItems.size(); i++) {
pagerAdapters.add(new MyPagerAdapter(activity.getSupportFragmentManager(), categoryItems.get(i).catItems));
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView;
itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rent_category_item,parent, false);
RentCategoryItemAdapter.ViewHolder viewHolder = new RentCategoryItemAdapter.ViewHolder(itemLayoutView);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.catName.setText(categoryItems.get(position).catName);
//AppCompatActivity activity = (AppCompatActivity) mContext;
//holder.mPagerAdapter = new MyPagerAdapter(activity.getSupportFragmentManager(), categoryItems.get(position).catItems);
holder.pager.setAdapter(pagerAdapters.get(position));
Log.d("Tag", pagerAdapters.get(position).getCount() + " position " + position);
/*holder.mPagerAdapter = myPagerAdapterArrayList.get(position);
holder.pager.setAdapter(myPagerAdapterArrayList.get(position));*/
int margin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10*16, mContext.getResources().getDisplayMetrics());
holder.pager.setPageMargin(-margin);
}
#Override
public int getItemCount() {
return categoryItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private PagerAdapter mPagerAdapter;
TextView catName;
ViewPager pager;
public ViewHolder(View itemView) {
super(itemView);
pager = itemView.findViewById(R.id.pager);
catName = itemView.findViewById(R.id.cat_name);
/*pagerArrayList.add(new ViewPager(mContext));
AppCompatActivity activity = (AppCompatActivity) mContext;
myPagerAdapterArrayList.add(new MyPagerAdapter(activity.getSupportFragmentManager()));*/
}
}
private class MyPagerAdapter extends FragmentStatePagerAdapter {
ArrayList<RentItem> items;
public MyPagerAdapter(FragmentManager fm, ArrayList<RentItem> items) {
super(fm);
this.items = items;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new RentCategoryForPagerFragment();
Bundle bundle = new Bundle();
bundle.putString("first_last", "first");
bundle.putString("product_id", items.get(position).productId);
bundle.putString("product_name", items.get(position).productName);
bundle.putString("product_price", items.get(position).productPrice);
bundle.putString("product_image", items.get(position).productImage);
fragment.setArguments(bundle);
Log.d("Tag", "inside my pager adapter " + position);
return fragment;
}
#Override
public int getCount() {
return items.size();
}
}
}
And here is RentCategoryForPagerFragment class
public class RentCategoryForPagerFragment extends Fragment {
String firstOrLast = "";
ImageView productImage;
TextView productName, productPrice;
String productId;
Button showDetailsButton;
public RentCategoryForPagerFragment() {
//firstOrLast = getArguments().getString("first_last");
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.rent_item_in_category_layout, container, false);
productImage = rootView.findViewById(R.id.product_image);
productName = rootView.findViewById(R.id.product_name);
productPrice = rootView.findViewById(R.id.product_price);
showDetailsButton = rootView.findViewById(R.id.show_details_button);
Bundle bundle = getArguments();
String imageURL = bundle.getString("product_image");
Glide.with(getActivity()).load(imageURL).into(productImage);
productId = bundle.getString("product_id");
productName.setText(bundle.getString("product_name"));
productPrice.setText(bundle.getString("product_price"));
showDetailsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked ", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
The result I got in this
Finally I found the solution. I was using the wrong class because it sound that FragmentStatePagerAdapter and FragmentPagerAdapter Classes do not serve my idea.
So You should extend from PagerAdapter instead to implement more than one pager in the same screen. And you should override some methods to complete it.
I hope this answer helps someone in someday.

Facebook Native Ads in RycyclerView android

I found similar issue Facebook Native ads in recycler view android , but had some problems with integration with Custom Ad.
For the first I tried to describe NativeAdsManager :
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
manager = new NativeAdsManager(getActivity(), "892769720837476_892772047503910", 5);
manager.setListener(this);
manager.loadAds();
nativeAd = new NativeAd(getActivity(), "892769720837476_892772047503910");
nativeAd.setAdListener(this);
nativeAd.loadAd();
...
}
Then I included Native ad as a parameter in RecyclerAdapter constructor:
adapter = new RecyclerAdapter(foodDataList, getActivity(), nativeAd);
In this Class I also implement AdListener and NativeAdsManager.Listener methods:
#Override
public void onError(Ad ad, AdError adError) {
}
#Override
public void onAdLoaded(Ad ad) {
}
#Override
public void onAdClicked(Ad ad) {
}
#Override
public void onAdsLoaded() {
System.out.println("Loaded in fragment");
nativeAd = manager.nextNativeAd();
nativeAd.setAdListener(this);
adapter.notifyDataSetChanged();
}
#Override
public void onAdError(AdError adError) {
}
After that in RecyclerAdapter class :
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
switch (holder.getItemViewType()) {
...
case 2:
AdditionalHolder new_holder = (AdditionalHolder) holder;
adView = NativeAdView.render(context, nativeAd, NativeAdView.Type.HEIGHT_100);
new_holder.templateContainer.addView(adView);
return;
...
public class AdditionalHolder extends RecyclerView.ViewHolder {
protected LinearLayout templateContainer;
public AdditionalHolder(View view) {
super(view);
templateContainer = (LinearLayout) view.findViewById(R.id.ad_test2);
}
}
After all of that my Fragment with RecyclerView becomes very "luggy" and every time I can see more and more ads items (1 - from the beginning, 2 - after 10 items, 3 - after next 10 and so on).
It's my first time of using multiple RecyclerView holders, and find this issue difficult.
Provide you with gist of 2 Classes
https://gist.github.com/burnix/6af8196ee8acf5c8f94e - RecyclerAdapter.class
https://gist.github.com/burnix/53dc2ed7446969b78f07 - FragmentList.class
Help me please to solve lugs problem and to place AudienceNetwork as it needs to be.
Thanks in advance!
After several hours of headache I solved my problem.
I needed to change the constructor of RecyclerView:
public RecyclerAdapter(List<FoodData> food, Context context, NativeAd nativeAd, NativeAdsManager manager) {
this.foodDataList = food;
this.context = context;
this.nativeAd = nativeAd;
this.manager = manager;
}
And implement the behavior of Native Ads in onBindViewHolder:
AdditionalHolder new_holder = (AdditionalHolder) holder;
try {
new_holder.templateContainer.removeViewInLayout(adView);
} catch (Exception e) {
e.printStackTrace();
}
nativeAd = manager.nextNativeAd();
try {
adView = NativeAdView.render(context, nativeAd, NativeAdView.Type.HEIGHT_300);
} catch (NullPointerException e) {
e.printStackTrace();
}
new_holder.templateContainer.addView(adView);
new_holder.blank_holder.setVisibility(View.GONE);
But it hasn't solved my problem entirely (micro-lags while scrolling), but still it could be useful for someone, I hoped.
Youur adapter==>
import android.app.Activity;
import android.support.v7.widget.CardView;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.romantic.pictures.hd.love.wallpaper.download.R;
import com.romantic.pictures.hd.love.wallpaper.download.activity.ROMANTIC_LOVE_WALLPAPER;
import com.romantic.pictures.hd.love.wallpaper.download.model.ImageStorage;
import java.util.ArrayList;
/**
* Created by abc on 4/11/2018.
*/
public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public final int CONTENT_TYPE = 0;
public final int AD_TYPE = 1;
private OnItemClickListener mOnItemClickListener;
Activity context;
ArrayList<ImageStorage> catagorilist;
public ImageAdapter(Activity context, ArrayList<ImageStorage> catagorilist, OnItemClickListener mOnItemClickListener) {
this.mOnItemClickListener = mOnItemClickListener;
this.context = context;
this.catagorilist = catagorilist;
}
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
#Override
public int getItemViewType(int position) {
Log.e("TAG", "getItemViewType position : " + position);
Log.e("TAG", "getItemViewType position% : " + position % 5);
if (position % 7 == 0)
return AD_TYPE;
return CONTENT_TYPE;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case CONTENT_TYPE:
Log.e("TAG", "content type : ");
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_layout2, parent, false);
return new MyHolder(view);
case AD_TYPE:
Log.e("TAG", "ad type : ");
View nativeView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_nativead, parent, false);
return new NativeAd(nativeView);
default:
Log.e("TAG", "default : ");
LayoutInflater inflater1 = LayoutInflater.from(parent.getContext());
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_layout2, parent, false);
return new MyHolder(v);
}
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
holder.setIsRecyclable(false);
int viewType = getItemViewType(position);
ImageStorage listitem = catagorilist.get(position);
switch (viewType) {
case CONTENT_TYPE:
Log.e("TAG", "bind CONTENT_TYPE : ");
final MyHolder menuItemHolder = (MyHolder) holder;
// holder.imagview.setText(listitem.getId());
/*holder.cat_name.setText(listitem.getName());*/
Glide.with(context).load("http://learntodraw.in/ImageApp/new_" + listitem.getCat_imagem()).listener(new RequestListener<String, GlideDrawable>() {
#Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
menuItemHolder.progressBar.setVisibility(View.GONE);
return false;
}
}).into(menuItemHolder.imagview);
Log.e("TAG", "onBindViewHolder: " + "http://learntodraw.in/ImageApp/new_" + listitem.getCat_imagem());
Log.e("TAG", "onBindViewHolder: " + position);
menuItemHolder.mainLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(v, position);
}
});
break;
case AD_TYPE:
Log.e("TAG", "bind AD_TYPE : ");
final NativeAd nativeHolder = (NativeAd) holder;
ROMANTIC_LOVE_WALLPAPER.showNativeVidAd(context, nativeHolder.container);
nativeHolder.container_height.post(new Runnable() {
public void run() {
// int height = nativeHolder.container_height.getHeight();
ViewGroup.LayoutParams params = nativeHolder.container.getLayoutParams();
// params.height = height;
params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
nativeHolder.container.setLayoutParams(params);
// nativeHolder.linearlayout_title.setVisibility(View.GONE);
}
});
break;
}
}
#Override
public int getItemCount() {
return catagorilist.size();
}
public class MyHolder extends RecyclerView.ViewHolder {
public ImageView imagview;
public LinearLayout mainLayout;
public CardView cardview;
public ProgressBar progressBar;
public MyHolder(View itemView) {
super(itemView);
imagview = (ImageView) itemView.findViewById(R.id.imagview);
mainLayout = (LinearLayout) itemView.findViewById(R.id.mainLayout);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
cardview = (CardView) itemView.findViewById(R.id.cardview);
}
}
public static class NativeAd extends RecyclerView.ViewHolder {
LinearLayout container;
LinearLayout container_height;
// LinearLayout linearlayout_title;
public NativeAd(View view) {
super(view);
container = view.findViewById(R.id.native_ad_container);
container_height = view.findViewById(R.id.container_height);
/* linearlayout_title = view.findViewById(R.id.linearlayout_title);*/
}
}
}
public static void showNativeVidAd(final Activity context, final LinearLayout nativeContainer) {
final NativeAd nativeAd = new NativeAd(context, context.getString(R.string.fb_test_ad_img) + context.getString(R.string.native_ads3));
// final NativeAd nativeAd = new NativeAd(context, context.getString(R.string.facebook_native_ad_unit_id));
nativeAd.setAdListener(new com.facebook.ads.AdListener() {
#Override
public void onError(Ad ad, AdError error) {
// Ad error callback
Log.e("TAG", "facebook native error : " + error.getErrorMessage());
nativeContainer.setVisibility(View.GONE);
}
#Override
public void onAdLoaded(Ad ad) {
// Ad loaded callback
Log.e("TAG", "facebook native onAdLoaded : ");
if (nativeAd != null) {
nativeAd.unregisterView();
}
try {
LinearLayout nativeAdContainer = nativeContainer;
nativeAdContainer.setVisibility(View.VISIBLE);
// Add the Ad view into the ad container.
nativeAdContainer = (LinearLayout) context.findViewById(R.id.native_ad_container);
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the Ad view. The layout referenced should be the one you created in the last step.
RelativeLayout adView = (RelativeLayout) inflater.inflate(R.layout.custom_nativead, nativeAdContainer, false);
nativeAdContainer.removeAllViews();
nativeAdContainer.addView(adView);
// Create native UI using the ad metadata.
MediaView nativeAdMedia = (MediaView) adView.findViewById(R.id.native_ad_media);
/* //TextView nativeAdSocialContext = (TextView) adView.findViewById(R.id.native_ad_social_context);
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "ProximaNova-Semibold.otf");
nativeAdSocialContext.setTypeface(typeface);*/
// TextView nativeAdCallToAction = (TextView) adView.findViewById(R.id.native_ad_call_to_action);
// Set the Text.
// nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
// nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
// Download and display the ad icon.
// NativeAd.Image adIcon = nativeAd.getAdIcon();
// Download and display the cover image.
nativeAdMedia.setNativeAd(nativeAd);
// Add the AdChoices icon
LinearLayout adChoicesContainer = (LinearLayout) adView.findViewById(R.id.ad_choices_container);
AdChoicesView adChoicesView = new AdChoicesView(context, nativeAd, true);
adChoicesContainer.addView(adChoicesView);
// Register the Title and CTA button to listen for clicks.
List<View> clickableViews = new ArrayList<>();
// clickableViews.add(nativeAdCallToAction);
clickableViews.add(nativeAdMedia);
clickableViews.add(adChoicesContainer);
nativeAd.registerViewForInteraction(nativeAdContainer, clickableViews);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onAdClicked(Ad ad) {
// Ad clicked callback
}
#Override
public void onLoggingImpression(Ad ad) {
// Ad impression logged callback
Log.e("TAG", "facebook native onLoggingImpression");
}
});
// Request an ad
nativeAd.loadAd(NativeAd.MediaCacheFlag.ALL);
}

recyclerview onclicklistener java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

when I dynamic remove an item, or when I refresh the adapter(swipe to fresh) i get these erros:
recyclerview onclicklistener java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
Really can't figure out why this is happening, Really Appreciate any feed back.
MainActivity:
adapter.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
GroupModel selectedList = mGroupModels.get(position);
if (selectedList != null) {
Log.d(TAG, "setAdpterListner > view.getId: " + view.getId() +
" | P: " + position +
" | data ID: " + selectedList.getGroupName()
//" | viewID: " + viewID
);
Intent intent = new Intent(this, DetailsActivity.class);
String listId = selectedList.getGroupID();
String listName = selectedList.getGroupName();
intent.putExtra(Constant.KEY_LIST_ID, listId);
intent.putExtra(Constant.KEY_LIST_NAME, listName);
startActivity(intent);
}
}
});
Adapter:
public class GroupListAdapter extends RecyclerView.Adapter<GroupListAdapter.StatusViewHolder> {
Context context;
private List<GroupModel> mGroupModels;
private static OnItemClickListener listener;
public GroupListAdapter(Context context, List<GroupModel> groupList) {
this.context = context;
this.mGroupModels = groupList;
}
public void setOnItemClickListener(OnItemClickListener listener) {
GroupListAdapter.listener = listener;
}
#Override
public StatusViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_group_list, parent, false);
return new StatusViewHolder(view);
}
#Override
public void onBindViewHolder(StatusViewHolder holder, int position) {
final GroupModel data = mGroupModels.get(position);
holder.text_view_list_name.setText(data.getGroupName());
}//end onBindViewHolder
#Override
public int getItemCount() {
return mGroupModels.size();
}
public class StatusViewHolder extends RecyclerView.ViewHolder {
public TextView text_view_list_name;
public TextView created_by;
public StatusViewHolder(final View itemView) {
super(itemView);
text_view_list_name = (TextView) itemView.findViewById(R.id.text_view_list_name);
created_by = (TextView) itemView.findViewById(R.id.created_by);
// Setup the click listener
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null)
listener.onItemClick(itemView, getLayoutPosition());
}
});
}
}//end StatusViewHolder
}//end GroupListAdapter
UPDATE:
I taken into consideration Yurii Tsap Feedback. I check the code again. I think the problem is somewhere below:
Whenever I swipe to Fresh, and If i click on the list straight way, the app Crash with the error from above.
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// Refresh items
mRecyclerView.invalidate();
adapter = null;
initGetGroupList();
mSwipeRefreshLayout.setRefreshing(false);
}
});
In the API call
...
GroupModel groupModel = new GroupModel(groupID, groupName, groupCreatedBy);
mGroupModels.add(groupModel);
}
updateUI(true);
if (adapter == null) {
adapter = new GroupListAdapter(MainActivity.this, mGroupModels);
mRecyclerView.setAdapter(adapter);
setAdapterListener(adapter);
}
The problem is definitely not in the static listener. And also the listener is just an interface callback(related to the comment above) not a AdapterView.OnItemClickListener(). I think the problem is somewhere behind this code, maybe you are clearing the item list somewhere else or something like that?
And also as for me in your case it's better to use getAdapterPosition() instead of getLayoutPosition().
As mentioned in docs :
If LayoutManager needs to call an external method that requires the adapter position of the item, it can use getAdapterPosition() or RecyclerView.Recycler.convertPreLayoutPositionToPostLayout(int).
Make OnItemClickListener non static .Like
private OnItemClickListener listener;
your code have alot of problem
i will explain my way to handle recycle view
very simple adapter
package com.pentavalue.ongo.transportway.adapter;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import com.pentavalue.ongo.R;
import com.pentavalue.ongo.Utilts.ImageLoaderHelper;
import com.pentavalue.ongo.register.model.Vechiles;
import com.pentavalue.ongo.widget.ArabicTextView;
import java.util.ArrayList;
/**
* Created by hamada on 19/09/2015.
*/
public class TransportTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public LayoutInflater inflater = null;
Activity activity;
ArrayList<Vechiles> vechileList;
ImageLoaderHelper imageLoaderHelper;
public TransportTypeAdapter(Activity activity, ArrayList<Vechiles> vechileList) {
this.activity = activity;
this.vechileList = vechileList;
imageLoaderHelper = new ImageLoaderHelper(activity, null);
inflater = LayoutInflater.from(activity);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View vi = inflater.inflate(R.layout.single_transport_item, parent, false);
RecyclerView.ViewHolder vh = new VechileListHolder(vi);
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int pos) {
VechileListHolder mHolder = (VechileListHolder) viewHolder;
mHolder.renderDate(vechileList.get(pos));
}
#Override
public int getItemCount() {
if (vechileList != null)
return this.vechileList.size();
else
return 0;
}
public class VechileListHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
ArabicTextView vecName;
RelativeLayout img_layout;
ProgressBar progress;
ImageView imgView;
public VechileListHolder(View vi) {
super(vi);
vi.setOnClickListener(this);
img_layout = (RelativeLayout) vi.findViewById(R.id.img_layout);
vecName = (ArabicTextView) vi.findViewById(R.id.TVVecName);
progress = (ProgressBar) vi.findViewById(R.id.progress);
imgView = (ImageView) vi.findViewById(R.id.imgView);
}
public void renderDate(Vechiles item) {
imageLoaderHelper.loadImage(imgView, progress, item.getImg());
vecName.setText(item.getVecName());
}
#Override
public void onClick(View v) {
if (mItemClickListener != null)
mItemClickListener.onItemClickListener(getPosition(), v);
}
}
public void setOnItemClickListener(ItemClickListener itemClick) {
this.mItemClickListener = itemClick;
}
public ItemClickListener mItemClickListener;
public interface ItemClickListener {
public void onItemClickListener(int pos, View v);
}
public void updateList(ArrayList<Vechiles> vechileList) {
this.vechileList = vechileList;
notifyDataSetChanged();
}
public void removeItem(int pos){
this.vechileList.remove(pos);
notifyItemRemoved(pos);
}
}
and in Fragment or activity can
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initListView();
}
private void initRecycleView(){
listView = (RecyclerView) v.findViewById(R.id.listView);
listView.getItemAnimator().setAddDuration(1000);
listView.getItemAnimator().setChangeDuration(1000);
listView.getItemAnimator().setMoveDuration(1000);
listView.getItemAnimator().setRemoveDuration(1000);
listView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
listView.setAdapter(new TransportTypeAdapter(getActivity(), vecList));
}
then after data back from API can update adapter with new data by use method update
((VechileListHolder ) listView.getAdapter).updateList( vechileList);
in case went to remove any item by using Pos can do call method that in adapter removeItem(pos)
((VechileListHolder ) listView.getAdapter).removeItem(1);
hope this code help you

Categories

Resources