I am trying to add some date in the scroll list up in recyclerview. this is a job, except for scrolling, it will ruin everything when new data is added.
I saw the following topics:
Link 1
Link 2
but not one of them solved my problem.
can someone help me with this ...
I get some data using a volley, for example:
try {
json = array.getJSONObject(i);
PostObj.setImageUrl(json.getString(Config.TAG_PPIC));
...
} catch (JSONException e) {
e.printStackTrace();
}
listLf.add(0, PostObj);
}
adapter.notifyItemRangeChanged(0, adapter.getItemCount());
}
I tried to add adapter.notifyItemRangeChanged(0, adapter.getItemCount());, but still the same thing.
also i tried:
private Parcelable recyclerViewState;
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
and it doesn't work either. maybe I'll put it in the wrong place or something like that.
I want some suggestions about what I'm doing wrong, and how can I get new data, notify the adapter without damaging the scroll position?
edit ---------------------------------- full class:
public class Comments extends BaseActivity {
private List<LfGetSet> listLf;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
private RequestQueue requestQueue;
private String requestCount = "0";
private String lastrequestCount = "0";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewGroup content = (ViewGroup) findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.activity_comments, content, true);
recyclerView = (RecyclerView) findViewById(R.id.recyclerViewLf);
recyclerView.setHasFixedSize(true);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
listLf = new ArrayList<>();
requestQueue = Volley.newRequestQueue(this);
getData();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (isLastItemDisplaying(recyclerView)) {
getData();
}
}
});
adapter = new LfAdapter(listLf, this);
recyclerView.setAdapter(adapter);
}
private JsonArrayRequest getDataFromServer(String requestCount) {
...
return jsonArrayRequest;
}
private void getData() {
requestQueue.add(getDataFromServer(requestCount));
lastrequestCount = requestCount;
requestCount++;
}
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
LfGetSet PostObj = new LfGetSet();
JSONObject json = null;
try {
json = array.getJSONObject(i);
PostObj.setImageUrl(json.getString(Config.TAG_PPIC));
...
} catch (JSONException e) {
e.printStackTrace();
}
listLf.add(0, PostObj);
}
adapter.notifyDataSetChanged();
}
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
if (recyclerView.getAdapter().getItemCount() != 0) {
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION
&& lastVisibleItemPosition == 1
&& lastrequestCount != requestCount
&& requestCount != "0")
return true;
}
return false;
}
}