我正在使用RecyclerView并从10个批量中获取API中的对象。对于分页,我使用EndlessRecyclerOnScrollListener并遵循本教程Endless RecyclerView。
我的问题:在滚动程序时添加进度条并在从服务器获取数据后删除它。但就我而言,它不起作用。 ProgressBar仍在下载
获取数据的方法
private void makeJsonObjectRequest(int startList,int endList,final JsonObjectListener listener) {
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
i = startLsit;
while(i < endList) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
/***** TONS OF CODE *****/
movieList.add(movie);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
i++;
lastNumber = i;
}
mAdapter.notifyDataSetChanged();
listener.onDone(lastNumber);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(movieReq);
}
我使用方法的地方:
makeJsonObjectRequest(0, 9, new JsonObjectListener() {
@Override
public void onDone(int lastNumberI) {
lastNumber = lastNumberI;
}
@Override
public void onError(String error) {
}
}); }
mAdapter = new RVAdapter(getContext(),movieList,this,mRecyclerView);
mAdapter.setClickListener(this);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
movieList.add(null);
mAdapter.notifyItemInserted(movieList.size()-1);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// remove progress item
movieList.remove(movieList.size()-1);
mAdapter.notifyItemRemoved(movieList.size());
//add items one by one
int start = lastNumber;
int end = start + 9;
makeJsonObjectRequest(start, end, new JsonObjectListener() {
@Override
public void onDone(int lastNumberI) {
lastNumber = lastNumberI;
}
@Override
public void onError(String error) {
}
});
mAdapter.setLoaded();
}
}, 2000);
}
});
我的适配器就像这个Endless RecyclerView
答案 0 :(得分:1)
获得响应后,您必须删除该空影片对象并通知适配器
private void makeJsonObjectRequest(int startList,int endList,final JsonObjectListener listener) {
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
movieList.remove(movieList.size()-1);
mAdapter.notifyItemRemoved(movieList.size());
i = startLsit;
while(i < endList) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
/***** TONS OF CODE *****/
movieList.add(movie);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
i++;
lastNumber = i;
}
mAdapter.notifyDataSetChanged();
listener.onDone(lastNumber);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(movieReq);
}