RecyclerView messes up child view on scrolling - java

Well i tried to implement the recyclerview into Android Studio like this :
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> {
public String[] dataSet;
public Context context;
//---------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------//
public class ViewHolder extends RecyclerView.ViewHolder{
public ImageView image;
public TextView name;
public ViewHolder(View itemView) {
super(itemView);
this.image = itemView.findViewById(R.id.image);
this.name = itemView.findViewById(R.id.name);
}
public void bindData(Object data){
String extractedData = (String)data;
this.name.setText("");
this.name.setText(extractedData);
this.image.getLayoutParams().height = getRandomIntInRange(250, 100);
}
}
//---------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------//
public RecycleViewAdapter(Context context, String[] dataSet){
this.context = context;
this.dataSet = dataSet;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.food_view,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
// Resolves the messed up views after recycling.
viewHolder.setIsRecyclable(false);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bindData(dataSet[position]);
}
#Override
public int getItemCount() {
return dataSet.length;
}
// Custom method to get a random number between a range
protected int getRandomIntInRange(int max, int min){
Random rdm = new Random();
return rdm.nextInt((max-min)+min)+min;
}
}
Somehow i noticed that once i scroll up and down the recycled views get messed up...
Heres a Picture without Scrolling :
normal
And here is one after Scrolling... as you can see totally messed up :
messed up
Why does this happen and how can i prevent this ?
Does anyone got a solution ?

Well
Adapter is calling onBindViewHolder() all times when he must recreate view on the screen. You need to call getRandomIntInRange() outside of onBindViewHolder()
You could see it:

Related

Facing error in onBindView() method in recyclerView

I am just practicing the RecyclerView. when I run the App my App crashed I check the log but i cant understand the problem can some help me im waiting for someone responce?
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private model[] localDataSet;
private TextView name,number;
public static class ViewHolder extends RecyclerView.ViewHolder {
private final TextView textView;
public ViewHolder(View view) {
super(view);
textView = (TextView) view.findViewById(R.id.name);
}
public TextView getTextView() {
return textView;
}
}
public CustomAdapter(model[] dataSet) {
localDataSet = dataSet;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
// Create a new view, which defines the UI of the list item
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.contacx, viewGroup, false);
view.findViewById(R.id.name);
view.findViewById(R.id.number);
return new ViewHolder(view);
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
name.setText(localDataSet[position].getName());
number.setText(localDataSet[position].getPhone());
}
#Override
public int getItemCount() {
return localDataSet.length;
}
}
You need to change the following things in your code:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private model[] localDataSet;
//remove this from here.
//private TextView name,number;
public static class ViewHolder extends RecyclerView.ViewHolder {
//This should be from the layout you have passed in ViewHolder method. in your case from R.layout.contacx. This layout should have two textview with ids:- name and phone respectively.
private final TextView name, phone;
public ViewHolder(View view) {
super(view);
name = view.findViewById(R.id.name);
phone = view.findViewById(R.id.phone);
}
}
public CustomAdapter(model[] dataSet) {
localDataSet = dataSet;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
// Create a new view, which defines the UI of the list item
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.contacx, viewGroup, false);
//remvoe this from here.
//view.findViewById(R.id.name);
//view.findViewById(R.id.number);
return new ViewHolder(view);
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
//here you should call those view to bind your data like:
viewHolder.name.setText(localDataSet[position].getName());
viewHolder.phone.setText(String.valueOf(localDataSet[position].getPhone()));
}
#Override
public int getItemCount() {
return localDataSet.length;
}
}
Use ViewHolder for data bind. More safe
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private model[] localDataSet;
public static class ViewHolder extends RecyclerView.ViewHolder {
private final TextView name, number;
public ViewHolder(View view) {
super(view);
name = view.findViewById(R.id.name);
number = view.findViewById(R.id.phone);
}
public void bindData(model data) {
name.setText(data.getName());
number.setText(String.valueOf(data.getPhone()));
}
}
public CustomAdapter(model[] dataSet) {
localDataSet = dataSet;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
/* No Need
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.contacx, viewGroup, false);
view.findViewById(R.id.name);
view.findViewById(R.id.number);
*/
return new ViewHolder(LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.contacx, viewGroup, false););
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
/*
name.setText(localDataSet[position].getName());
number.setText(localDataSet[position].getPhone());
*/
viewHolder.bindData(localDataSet[position]);
}
#Override
public int getItemCount() {
return localDataSet.length;
}
}

Multiple recyclerviews using one adapter

I have an app where I want to use recyclerviews in different parts of the application and I was thinking of using the same adapter for the same two (or more) recyclerviews, I was thinking this would be a good practice, or am I mistaken and better off making a new adapter for every recyclerview in the app?
Here is the code for my adapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter <RecyclerViewAdapter.MyViewHolder> {
private ArrayList<Task> list;
//This is the list of the second recyclerview which I want to implement
private ArrayList<String> categories;
private OnItemListener mOnItemListener;
RecyclerViewAdapter(ArrayList<Task> list, OnItemListener onItemListener)
{
this.list = list;
this.mOnItemListener = onItemListener;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
TextView textView = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view, parent, false);
return new MyViewHolder(textView, mOnItemListener);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.textView.setText(list.get(position).getTaskName());
}
#Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView textView;
OnItemListener onItemListener;
MyViewHolder(#NonNull View itemView, OnItemListener onItemListener) {
super(itemView);
textView = itemView.findViewById(R.id.singleView);
this.onItemListener = onItemListener;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
onItemListener.onItemClick(getAdapterPosition());
}
}
public interface OnItemListener
{
void onItemClick(int position);
}
}
So how would I go about doing this so that it can switch depending on which recyclerview in the app is setting/initializing the adapter, so that the getItemCount for example, would return the size of categories instead of list and so on.

What might be the problem with my adapter i seem not to find it``

The view seems not to bind to the ArrayList(ie private ArrayList mEvents;) that i passed to the class ,on the onBindViewHolder(ie holder.mTextView2.setText(current.getcity());) i can seem to access the textView variables(ie public TextView mTextview2;), that i declared in the view holder class. I have tried to make the variables as a member variable but it seems not to work well
public class eventsAdapter extends RecyclerView.Adapter {
private ArrayList<recycleview_items> mEvents;
public eventsAdapter(ArrayList<recycleview_items> passedArray){
//constructor for the adapter calls for which i created
mEvents = passedArray;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//we pass the layout of our card to the adapter here
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_listitem, parent, false);
eventViewHolder events = new eventViewHolder(v);
return events;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
//we pass data from the array list we created to the views
recycleview_items current = mEvents.get(position);
holder.mImageView.setImageResource(current.getImageResource());
holder.mTextView1.setText(current.getname());
holder.mTextView2.setText(current.getcity());
}
#Override
public int getItemCount() {
return mEvents.size();
};
public static class eventViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageview ;
public TextView mTextview;
public TextView mTextview2;
public eventViewHolder(#NonNull View itemView) {
super(itemView);
mImageview = itemView.findViewById(R.id.ImageView1);
mTextview = itemView.findViewById(R.id.text_view1);
mTextview2 = itemView.findViewById(R.id.text_view2);
}
}
}
public class eventsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<recycleview_items> mEvents;
public eventsAdapter(ArrayList<recycleview_items> passedArray){
//constructor for the adapter calls for which i created
mEvents = passedArray;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//we pass the layout of our card to the adapter here
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_listitem, parent, false);
eventViewHolder events = new eventViewHolder(v);
return events;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
//we pass data from the array list we created to the views
recycleview_items current = mEvents.get(position);
holder.bind(current);
}
#Override
public int getItemCount() {
return mEvents.size();
};
class eventViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageview ;
public TextView mTextview;
public TextView mTextview2;
public eventViewHolder(#NonNull View itemView) {
super(itemView);
mImageview = itemView.findViewById(R.id.ImageView1);
mTextview = itemView.findViewById(R.id.text_view1);
mTextview2 = itemView.findViewById(R.id.text_view2);
public void bind(recycleview_items item){
//bind your items here
holder.mImageView.setImageResource(item.getImageResource());
holder.mTextView1.setText(item.getname());
holder.mTextView2.setText(item.getcity());
}
}
}
I've rewritten your code for you with slight modifications.
You should create adapter like this
extends RecyclerView.Adapter<eventsAdapter.eventViewHolder >
Also you should edit
public eventViewHolder onCreateViewHolder
And
onBindViewHolder(eventViewHolder holder, int position)
Try this
public class eventsAdapter extends RecyclerView.Adapter<eventsAdapter.eventViewHolder>{
private ArrayList<recycleview_items> mEvents;
public eventsAdapter(ArrayList<recycleview_items> passedArray){
//constructor for the adapter calls for which i created
mEvents = passedArray;
}
#NonNull
#Override
public eventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_listitem, parent, false);
return new eventViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull eventViewHolder holder, int position) {
recycleview_items current = mEvents.get(position);
holder.mImageview.setImageResource(current.getImageResource());
holder.mTextview.setText(current.getname());
holder.mTextview2.setText(current.getcity());
}
#Override
public int getItemCount() {
return 0;
}
class eventViewHolder extends RecyclerView.ViewHolder {
ImageView mImageview;
TextView mTextview;
TextView mTextview2;
public eventViewHolder(View itemView) {
super(itemView);
mImageview = itemView.findViewById(R.id.ImageView1);
mTextview = itemView.findViewById(R.id.text_view1);
mTextview2 = itemView.findViewById(R.id.text_view2);
}
}
}
Almost every thing was in Your code that's i have done some modification.
Mate see your Holder View Class name its 'eventViewHolder' and for onBindViewHolder and onCreateViewHolder you're accessing the Recycler.ViewHolder so it's very obvious that you can't access those variable just change this..
#Override
public void onBindViewHolder(#NonNull eventsAdapter.eventViewHolder holder, int position) {
.....
}
and in
#NonNull
#Override
public eventsAdapter.eventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
......
}

RecyclerView Adapter won't inflate the correct layout

I'm trying to have my application display a specific layout when the arraylist PinnedSongs is empty.
I've created different viewholders and tried to inflate them depending on the PinnedSongs.size. Meaning, if it's 0, then the onCreateViewHolder should inflate a special layout. If it's different, then inflate another one.
public class RecyclerAdapterHome extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public class Viewholder extends RecyclerView.ViewHolder implements View.OnClickListener{
CardView home_cardView;
TextView home_textView;
TextView home_textView_2;
Button home_button;
Button home_button_2;
RelativeLayout parentlayout;
OnNoteListener_2 onNoteListener2;
public Viewholder(#NonNull View itemView, OnNoteListener_2 onNoteListener_2) {
super(itemView);
home_cardView = itemView.findViewById(R.id.cardview_2);
home_textView = itemView.findViewById(R.id.cardview_2_textview);
home_textView_2 = itemView.findViewById(R.id.cardview_2_textview_sub);
home_button = itemView.findViewById(R.id.play_button);
home_button_2 = itemView.findViewById(R.id.push_button);
parentlayout = itemView.findViewById(R.id.home_parentlayout);
home_button.setOnClickListener(this);
this.onNoteListener2 = onNoteListener_2;
}
#Override
public void onClick(View v) {
onNoteListener2.onClick(getAdapterPosition());
}
}
public class Viewholder_2 extends RecyclerView.ViewHolder {
TextView textView_3;
ImageView imageView;
RelativeLayout parentlayout;
public Viewholder_2(#NonNull View itemView) {
super(itemView);
textView_3 = itemView.findViewById(R.id.text_warning);
imageView = itemView.findViewById(R.id.icon);
parentlayout = itemView.findViewById(R.id.home_parentlayout);
}
}
public RecyclerAdapterHome(OnNoteListener_2 mOnNoteListener2, ArrayList<String> pinnedSongs, ArrayList<String> pinnedSongsArtists, Context mContext) {
this.mOnNoteListener2 = mOnNoteListener2;
this.PinnedSongs = pinnedSongs;
this.PinnedSongsArtists = pinnedSongsArtists;
this.mContext = mContext;
}
private ArrayList<String> PinnedSongs = new ArrayList<>();
private ArrayList<String> PinnedSongsArtists = new ArrayList<>();
private Context mContext;
private OnNoteListener_2 mOnNoteListener2;
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = null;
if (PinnedSongs.size()==0){
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.nothing, parent, false);
return new Viewholder_2(v);
}else{
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.homeviewholder,
parent, false);
return new Viewholder(v,mOnNoteListener2 );
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
if (PinnedSongs.size()==0){
Viewholder_2 viewholder_2 = (Viewholder_2) holder;
}else{
Viewholder viewholder = (Viewholder) holder;
viewholder.home_textView.setText(PinnedSongs.get(position));
viewholder.home_textView_2.setText(PinnedSongsArtists.get(position));
}
}
#Override
public int getItemCount() {
return PinnedSongs.size();
}
public interface OnNoteListener_2 {
void onClick(int position);
}
}
So far, the recyclerview shows nothing when the PinnedSongs list is at zero. But it shows something when it's above zero. Any idea on how I can fix it?
(I'm using the androidx libraries if it has anything to do with that).
It doesn't work because the getItemCount method returns 0 and the RecyclerView never tries to instantiate a view.
It means that onBindViewHolder is not called when the size=0.
You have different options to do it. One of these is:
#Override
public int getItemCount() {
return PinnedSongs.size() > 0 ? PinnedSongs.size() : 1;
}
#Override
public int getItemViewType(int position) {
if (PinnedSongs.size() == 0) {
return EMPTY_VIEW;
}
return super.getItemViewType(position);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if (viewType == EMPTY_VIEW) {
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.nothing, parent, false);
EmptyViewHolder emptyViewHolder = new EmptyViewHolder(v);
return emptyViewHolder;
}
//normal case
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.homeviewholder, parent, false);
ViewHolder viewholder = new ViewHolder(v);
return viewholder;
}
public class EmptyViewHolder extends RecyclerView.ViewHolder {
public EmptyViewHolder(View itemView) {
super(itemView);
}
}
private static final int EMPTY_VIEW = 1000;

RecyclerView Elements repeating when i use ion library to load images

I am using ion library to load images in recyclerview. But i am facing with a problem. RecyclerView is repeating items. When the image is not loaded in new item it shows previously loaded images. I need to show ProgressBar if image is not loaded. How to achieve this.
Here is my Adapter class:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private String[] links;
private Context context;
private int screen_width;
public RecyclerViewAdapter(Context context, String[] links, int screen_width) {
this.links = links;
this.context = context;
this.screen_width = screen_width;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, final int i) {
loadImage(i, viewHolder);
}
private void loadImage(final int pos, final ViewHolder holder) {
Ion.with(context)
.load(links[pos])
.withBitmap()
.placeholder(R.drawable.icon_download_active)
.error(R.drawable.icon_could_not_download)
.fadeIn(true)
.asBitmap()
.setCallback(new FutureCallback<Bitmap>() {
#SuppressLint("SetTextI18n")
#Override
public void onCompleted(Exception e, Bitmap result) {
if ( e != null ) {
Log.i("IMAGE_LOADER_EV","Failed to load image \n Error:"+e.toString());
} else {
int h = (screen_width * result.getHeight()) / result.getWidth();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(screen_width,h);
params.setMargins(0,0,0,dpToPx(context));
holder.image.setLayoutParams(params);
holder.image.setImageBitmap(result);
}
}
});
}
private static int dpToPx(Context context) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) 5, context.getResources().getDisplayMetrics());
}
#Override
public int getItemCount() {
return links.length;
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView dimens;
ProgressBar progressBar;
ViewHolder(View view) {
super(view);
image = view.findViewById(R.id.image);
dimens = view.findViewById(R.id.dimens);
progressBar = view.findViewById(R.id.progress_bar);
}
}
}
Recycler shows previous items when the new image is not loaded.
So how to solve this problem?
Finally found solution. Just add following codes to Adapter class:
#Override
public long getItemId(int position) {
return (long) (position);
}
#Override
public int getItemViewType(int position) {
return position;
}
Use:
Ion.with(imageView)
Then remove where you manually set the image in the ImageView in the callback.
.setCallback(...)
Since you are using the callback to manage populating the ImageView, you're creating a race condition where the ImageView is loaded by multiple different items as it is recycled.
Using Ion.with(imageView) allows Ion to track the ImageViews in your RecyclerView and cancel image loads as the ImageViews get recycled.
See sample:
https://github.com/koush/ion#load-an-image-into-an-imageview

Categories

Resources