我有recyclerview并从网络服务获取信息。
public void Load() {
counter = 0;
adapterComment = new AdapterComment(G.context, comments);
mRecyclerView.setAdapter(adapterComment);
VolleyRequestHelper volleyRequestHelper = new VolleyRequestHelper(Request.Method.POST, G.URL_ADDRESS_MUSIC + "get_song_comment/page/" + counter, map, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//get data
}
adapterComment.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Volley.newRequestQueue(G.context).add(volleyRequestHelper);
} 每次回收者显示20项时我都会使用无限滚动,如果数据较大则没有问题如果数据较小20回收者在打开键盘设备之前不显示数据(我在图层中有edittext)。
public void LoadMore() {
counter = (counter + 1);
adapterComment = new AdapterComment(G.context, comments);
mRecyclerView.setAdapter(adapterComment);
VolleyRequestHelper volleyRequestHelper = new VolleyRequestHelper(Request.Method.POST, G.URL_ADDRESS_MUSIC + "get_song_comment/page/" + counter, map, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//get data
}
adapterComment.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Volley.newRequestQueue(G.context).add(volleyRequestHelper);
}
并在onCreatView中:
Load();
mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int current_page) {
int lastFirstVisiblePosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager()).findFirstVisibleItemPosition();
((LinearLayoutManager) mRecyclerView.getLayoutManager()).scrollToPosition(lastFirstVisiblePosition);
LoadMore();
}
});