2016-07-01 15 views
6

mi hanno un layout con un RecyclerView con l'adattatore:aggiornamenti RecyclerView solo su rotolo

public class SimpleRecyclerAdapter extends RecyclerView.Adapter<SimpleViewHolder> { 

    protected List<JSONObject> data = new ArrayList<>(); 

    public List<JSONObject> getData() { return data; } 
}  

aggiorno E 'adattatore come:

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(); 
} 

Il problema è, che riesco a vedere il nuovo elemento solo quando tocchi o scorri la vista.

Cosa mi manca?

UPDATE:

per risolvere il problema ho sostituito le linee

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

con

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

e ha funzionato

risposta

3

provare a specificare la posizione dell'elemento inserito o rimosso invece di notifyDataSetChanged();. puoi usare

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