RecyclerView updates only when scrolling

I have a layout with RecyclerView with an adapter:

 public class SimpleRecyclerAdapter extends RecyclerView.Adapter<SimpleViewHolder> { protected List<JSONObject> data = new ArrayList<>(); public List<JSONObject> getData() { return data; } } 

I am updating it as:

 public void udpdateFromJson( JSONObject payload ) { JSONArray msgs = payload.optJSONArray( "messages" ); for( int ix = 0; null != msgs && ix < msgs.length(); ix++ ){ JSONObject o = msgs.optJSONObject( ix ); if( loadingMore ) adapter.getData().add( 1 + ix, o ); else adapter.getData().add( o ); } adapter.notifyDataSetChanged(); recyclerView.invalidate(); } 

The problem is that I only see a new element when I touch or scroll the view.

What am I missing?

UPDATE:

to fix the problem, I replaced the lines

 adapter.notifyDataSetChanged(); recyclerView.invalidate(); 

from

 adapter.notifyItemRangeInserted( loadingMore ? 1 : 0, msgs.length() ); 

and he worked

+7
android android-recyclerview recycler-adapter
source share
1 answer

try specifying the position of the inserted or deleted item instead of notifyDataSetChanged(); . you can use

 notifyItemInserted(int pos); notifyItemRangeChanged(int start, int end);` 
+3
source share

All Articles