Recyclerview changing items when is scrolled
Try to initialize your view on RecyclerView.ViewHolder
and inflate your layout on onCreateViewHolder()
and don't inflate again the layout in onBindViewHolder()
.
public class AcademicAdapter extends RecyclerView.Adapter<AcademicAdapter.AcademicVH> {
private LayoutInflater inflater;
private List<AcademicStatus> data;
public AcademicAdapter(List<AcademicStatus> data) {
this.data = data;
}
@Override
public AcademicVH onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_academic_level, parent, false);
AcademicVH mViewHold = new ViewHolder(mView);
return mViewHold;
}
@Override
public void onBindViewHolder(AcademicVH holder, int position) {
final AcademicStatus mItems=this.data.get(position);
holder.txtSubject.setText(mItems.getSubject());
holder.txtGroup.setText(mItems.getGroup());
holder.txtStatus.setText(mItems.getStatus());
holder.txtFinalNote.setText(mItems.getFinalNote());
holder.txtYear.setText(mItems.getAcademicYear());
}
@Override
public int getItemCount() {
return data.size();
}
public static class AcademicVH extends RecyclerView.ViewHolder {
public LinearLayout containerData;
public TextView txtSubject,txtGroup,txtStatus,txtFinalNote,txtYear;
public AcademicVH(View itemView) {
super(itemView);
containerData = (LinearLayout) itemView.findViewById(R.id.containerData);
txtSubject = (TextView) itemView.findViewById(R.id.txtSubjectValue);
txtGroup = (TextView) itemView.findViewById(R.id.txtGroupValue);
txtStatus = (TextView) itemView.findViewById(R.id.txtStatusValue);
txtFinalNote = (TextView) itemView.findViewById(R.id.txtFinalNoteValue);
txtYear = (TextView) itemView.findViewById(R.id.txtYearValue);
}
}
}
Android RecyclerView - scrolling changes items
As pointed out by Samuel Robert, you need to track the expanded items visibility in your model class :
Say you are using a model class item, have a field inside it to track the visibility of nested layout.
Try this out :
Item item = itemList.get(position);
final ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.tv.setText("TV1");
viewHolder.nestedLayout.setVisibility(!item.isVisible ? View.VISIBLE : View.GONE );
viewHolder.btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int position = getAdapterPosition();
if( position != RecyclerView.NO_POSITION ) {
Item item = itemList.get(position)
item.isVisible = !item.isVisible;
notifyItemChanged(position);
}
}
});
Recyclerview on Scrolling values changing from adapter
adding this in the adapter solved the problem
@Override
public int getItemViewType(int position)
{
return position;
}
When I am Scrolling my RecyclerView, the item inside is automatically changing
This happening because this is how RecyclerView
works it recycle your view when ever you scroll and only use current loaded screen data in to memory.
As soon as you scroll bottom and come up your position mis match and you see some weird and unexpected result in Item.
You can solve this issue simply by providing else
block to your design changing condition Like in your onBindViewHolder
if (holder.textView.text.toString().equals(timeSlots.time.toString())) {
Log.i("time1",timeSlots.time.toString())
Log.i("time2",holder.textView.text.toString())
holder.card.setBackground(context.resources.getDrawable(R.drawable.card_background))
holder.itemView.isEnabled=false
}
else{
holder.card.setBackground(context.resources.getDrawable(//your another drawable))
holder.itemView.isEnabled=true
}
That's how you can solve it.
recyclerview data changed on scrolling (Not Duplicate) and dynamic textview got doubled on scrolling
Add this overriding method to your custom adapter
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
Related Topics
Difference Between Surfaceview and View
Disconnect a Bluetooth Socket in Android
Toolbar Overlapping Below Status Bar
Get Cursor Position in Android in Edit Text
How to Use the Same Keystore File to Sign Two Different Applications
How to Attach Multiple Files to Email Client in Android
How to Use Tools:Overridelibrary in a Build.Gradle File
Adding Ripple Effect to Recyclerview Item
Android Device Chooser - My Device Seems Offline
Error: No Toolchains Found in the Ndk Toolchains Folder for Abi with Prefix: Llvm
Android, Make an Image at a Url Equal to Imageview's Image
What Is the Intent of the Methods Getitem and Getitemid in the Android Class Baseadapter
Android Intent for Playing Video
Textview with Different Textsize
How to Share a Sharedpreferences File Across Two Different Android Apps
Android Device Manager Fails to Launch After Updating to MACos Big Sur 11.3