NotifyDataSetChanged does not update RecyclerView

I have a strange problem. I switched to a RecyclerView from a ListView , and I cannot update or notify about a change in the ListView . I tried calling Item.this.notifyDataSetChanged(); and other ways to update View , but this will not work. Instead, the RecyclerView updated as it scrolls (regardless of direction). How can I notify my RecyclerView when changing?

CODE:

 @Override public void onBindViewHolder(Ids holder, final int position) { rowItemClass = (ListViewRow) rowItems.get(position); Log.e("swag", "OYOYOYOYOYO"); if (Globals.isPlaying && Globals.pos == position) { if (pausedSamePos == true) { holder.pauseed_play.setVisibility(View.VISIBLE); holder.playing_pause.setVisibility(View.GONE); } else { holder.pauseed_play.setVisibility(View.GONE); holder.playing_pause.setVisibility(View.VISIBLE); } holder.song_currenttime_sb.setActive(); holder.song_duration.setVisibility(View.INVISIBLE); holder.song_duration_sb.setVisibility(View.VISIBLE); holder.seekbar.setActive(); } else { holder.seekbar.setInactive(); holder.song_currenttime_sb.setInactive(); holder.song_icon.setImageResource(rowItemClass.getImageId()); holder.song_duration_sb.setVisibility(View.INVISIBLE); holder.song_duration.setVisibility(View.VISIBLE); holder.pauseed_play.setVisibility(View.GONE); holder.playing_pause.setVisibility(View.GONE); } sharedPreference = new SharedPreference(); holder.song_duration.setTypeface(Globals .getTypefaceSecondary(context)); holder.song_duration_sb.setTypeface(Globals .getTypefaceSecondary(context)); holder.song_name.setTypeface(Globals.getTypefacePrimary(context)); holder.song_currenttime_sb.setTypeface(Globals .getTypefaceSecondary(context)); holder.song_name.setText(rowItemClass.getTitle()); holder.song_duration.setText(rowItemClass.getDesc()); holder.song_duration_sb.setText(rowItemClass.getDesc()); holder.favorite.setTag(position); holder.song_currenttime_sb.setTag(position); holder.seekbar.setTag(position); holder.clickRegister.setTag(position); holder.song_icon.setTag(position); holder.song_name.setTag(position); holder.song_duration.setTag(position); holder.song_duration_sb.setTag(position); holder.more_options.setTag(position); // int task_id = (Integer) holder.seekbar.getTag(); final Ids finalHolder = holder; holder.clickRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { if ((Globals.isPlaying.booleanValue()) && (Globals.pos == position)) { pausePlaying(); } else { Globals.stopPlaying(); pausedSamePos = false; Globals.pos = position; Globals.isPlaying = true; Item.this.notifyDataSetChanged(); Globals.mp = MediaPlayer.create(context, Integer .valueOf(Item.this.songPos[position]) .intValue()); Globals.mp.start(); Globals.pos = position; Globals.isPlaying = Boolean.valueOf(true); Item.this.notifyDataSetChanged(); Globals.mp .setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion( MediaPlayer mpOnComplete) { mpOnComplete.release(); Globals.isPlaying = false; pausedSamePos = false; Globals.isPlaying = Boolean .valueOf(false); finalHolder.menu_options .startAnimation(new ViewExpandAnimation( finalHolder.menu_options)); Item.this.notifyDataSetChanged(); } }); } } catch (Exception localException) { } } }); holder.clickRegister .setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Globals.stopPlaying(); Item.this.notifyDataSetChanged(); return true; } }); } 
+5
source share
4 answers

I'm not sure why this is so, but notifyDataSetChanged(); does not work. Therefore, I tried to track the changed elements and update them manually using notifyItemChanged(int); and while it works. I'm still not sure why updating the entire RecyclerView did not work.

+2
source

I struggled with a similar problem, trying to handle a use-case where the entire contents of the adapter should be replaced and the recycler should start from scratch: calling notifyDataSetChanged() , swapAdapter() using numerous combinations of subsequent calls to view / layout-manager invalidation requests led to a simple (apparently) empty form of recyclers. The view did not even try to double-check the owners of the views.

What seemed to work is a hack-ish fix:

 view.swapAdapter(sameAdapter, true); view.scrollBy(0, 0); 

As it turned out, scrollBy (even with 0 offsets) controls the viewing of the recycler, laying out its views and performing rewritable viewfinders.

+3
source

if you want your recycleListView to notify just a simple call to notifyDataSetChanged (); in your class adapter. this is my method in my adapter class:

 public class ListAdapter extends RecyclerView.Adapter<Listdapter.ViewHolder> { .... private List<DesaObject> desaObjects= new ArrayList<DesaObject>(); public void setListObjects(List<DesaObject> desaObjects){ if(this.desaObjects.size()>0) this.desaObjects.clear(); this.desaObjects = desaObjects; notifyDataSetChanged(); } .... public static class ViewHolder extends RecyclerView.ViewHolder { public final View mView; public final AppCompatTextView desa; public ViewHolder(View view) { super(view); mView = view; desa = (AppCompatTextView) view.findViewById(R.id.desa); } } } 

in your code, you actually do not set or change your list objects in OnClickListener and please try notifyDataSetChanged (); instead of Item.this.notifyDataSetChanged ();

+2
source

Initialize your adapter again with the changed data and use the swapadapter method. hope this helps.

0
source

Source: https://habr.com/ru/post/1237455/


All Articles