14

I have a layout with a RecyclerView with the adapter:

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

I update it's adapter like:

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 get to see the new item only when I touch or scroll the view.

What am I missing?

UPDATE:

to fix the problem I replaced the lines

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

with

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

and it worked

4 Answers 4

6

Most likely you are calling notifyDataSetChanged from background thread.

Simply (and ugly) fix you can do is to use:

recyclerView.post(new Runnable(){ @Override public void run(){ adapter.notifyDataSetChanged(); } }); 

If notifyDataSetChanged is called correctly from main thread then the list is updated without touching it.

Sign up to request clarification or add additional context in comments.

1 Comment

Very Good observation and fine abservation.
3

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

notifyItemInserted(int pos); notifyItemRangeChanged(int start, int end);` 

Comments

0

One possible solution is use Realm Adapter. Thant will manage your scroll view position based on insert/update/delete automatically. There is no flickering effects. Drawback is you have extend Object as Realm Object.

Comments

-1

As the question is already answered you can use one more line to scroll to the last position of your recyclerView using:

recyclerView.smoothScrollToPosition(msgs.length()-1) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.