0
我試圖創建一個無限滾動RecyclerView。我正在使用PHP和MySQL。在我的PHP腳本請求中,每次使用RecyclerView時都會觸及2個項目。RecyclerView無限滾動排球
$query = "SELECT * FROM beichten WHERE id BETWEEN ($id+1) AND ($id+2)";
Volley能做到這一點嗎?
我創建了一個類,每當RecyclerView到達底部時觸發我的請求類。
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
if (recyclerView.getAdapter().getItemCount() != 0) {
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1){
Toast.makeText(Menue.this, "BOTTOM", Toast.LENGTH_SHORT).show();
requestJsonObject();
}
}
return false;
}
它的工作原理,但RecyclerView負荷再次,只顯示了兩個新加載的對象。
private void requestJsonObject() {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, getbeichtenphp, new Response.Listener<String>() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onResponse(String response) {
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
List<ItemObject> posts = new ArrayList<ItemObject>();
posts = Arrays.asList(mGson.fromJson(response, ItemObject[].class));
adapter = new RecyclerViewAdapter(Menue.this, posts);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
recyclerView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
isLastItemDisplaying(recyclerView);
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("id", String.valueOf(adapter.getItemCount()));
return parameters;
}
};
queue.add(stringRequest);
}
您正在創建一個新的'Adapter'實例,每次只有當前的響應項目。相反,將這些項目添加到當前'Adapter'中的列表中。 –