Reputation: 11
How can I keep my scroll position when I refresh my data in my RecyclerView ?
The problem in my code, is that the data I get is coming from an API, and now when I refresh my data, I use the clear()
method to delete the current data, and I use a getData ()
method to retrieve the new data.
getData method :
private void getData() {
JsonArrayRequest arrayRequest = new JsonArrayRequest(url_get, response -> {
JSONObject jsonObject;
for (int i = 0; i < response.length(); i++) {
try {
jsonObject = response.getJSONObject(i);
Category cat = new Category();
cat.setId(jsonObject.getString("id"));
cat.setCategory(jsonObject.getString("name"));
cat.setPvisited(jsonObject.getString("person_visited"));
cat.setCompany(jsonObject.getString("company"));
cat.setDate(jsonObject.getString("date"));
category.add(cat);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapterPush(category);
refresh.setRefreshing(false);
}, error -> {
});
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(arrayRequest);
}
category detail :
private final ArrayList<Category> category = new ArrayList<>();
onRefresh method :
@Override
public void onRefresh() {
category.clear();
getData();
}
I think the getdata() method has a problem, when I remove
category.clear();
of my code, the data is added to the string but the position still returns to the beginning.
Or maybe I should used other thing like a refresh method rather than clear()
?
Upvotes: 1
Views: 221