Android:如何在recyclerView的后台加载更多数据 [英] Android: How can I load more data in the background in recyclerView
问题描述
我正在创建一个新的android应用程序,我几乎已经完成了第一个屏幕.但是,只要我到达屏幕底部,应用程序就会立即将我带到页面的开头,而不是加载新数据.
I am creating a new android app, I have nearly completed the first screen. However, as soon as I reach the bottom of the screen, instead of loading new data, the app throws me right to the beginning of the page.
当我发送第二页的请求时,我正在使用的API将发送下一页.您能告诉我如何制作该应用程序,以便它在用户到达第一页末尾之前自动发送对第二页的请求.
The API that I'm using will send the next page when I send a request for the second page. Can you please tell me how can I make the app such that, it automatically sends the request for the second page, before the user reaches the end of the first page.
我正在使用volley库以及recyclerView和cardView.您能否清楚说明我应该在哪里进行更改,我是应用程序开发的新手.
I'm using volley library along with recyclerView and cardView. Can you please explain clearly as to where am I supposed to make changes, I am new to app development.
以下是排球要求:
private void discoverMovies() {
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Fetching Data From DB");
progressDialog.show();
//Requesting the data.....
StringRequest stringRequest = new StringRequest(Request.Method.GET, tempURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
progressDialog.dismiss();
JSONObject parentObject = new JSONObject(response);
JSONArray parentArray = parentObject.getJSONArray("results");
for (int i = 0; i < parentArray.length(); i++) {
JSONObject object = parentArray.getJSONObject(i);
movieModel model = new movieModel(
object.getString("title"),
object.getString("overview"),
object.getString("release_date"),
object.getString("poster_path"),
object.getInt("id"),
object.getBoolean("adult"),
object.getInt("vote_count"),
object.getDouble("vote_average"),
object.getString("original_language"),
object.getString("backdrop_path")
);
movieModels.add(model);
adapter = new cardViewAdapter(getContext(), movieModels);
recyclerView.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getActivity(), error.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
推荐答案
Refer this. Its explained in a cool fashion here.
基本上,您需要一个onScrollChangeListener
来检测recyclerview中的滚动变化,但是仅此一项是无济于事的.因此,您需要一种方法来检查您当前是否到达了recyclerview的底部.所以这是您的操作方式:
Basically you need an onScrollChangeListener
to detect scroll changes in your recyclerview but that alone wont help. So you need a way to check whether you are currently reaching the bottom of your recyclerview. so here's how you do it:
在上面的链接中选中此行
Check this line in the above link
// If it isn't currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
if (!loading && (firstVisibleItem + visibleItemCount + visibleThreshold) >= totalItemCount ) {
loading = onLoadMore(currentPage + 1, totalItemCount);
}
查看变量 visibleThreshold ,您可以在其中修改并应用自己的自定义值来进行第二次调用.
See the variable visibleThreshold that is where you modify and apply you own custom value to make the second call.
让我们使用以下值进行检查:
lets check that with values:
如果visibleThreshold
为0,并且列表中有10个项目,而您位于第6个项目上,则第一个项目是第0个项目:
这条线
if visibleThreshold
is 0 and you have 10 items in your list and you`re on the 6 item and the first item is the 0th item:
the this line
if (!loading && (firstVisibleItem + visibleItemCount + visibleThreshold) >= totalItemCount )
解释为-> if(true&&(0 + 6 + 0)> = 10)解释为false!
interprets as -> if (true && (0+6+0)>=10) which interprets as false!
因此,第二个呼叫现在不会发出.当您的lastitem是第十个项目(因为您的阈值为0)时,它将完成
thus the second call wont be made now. it will be made when you lastitem is the 10th item (as your threshold is 0)
但是现在,如果您将阈值添加为4,则在满足条件的情况下进行下一个调用: 解释为-> if(true&&(0 + 6 + 4)> = 10)解释为true!
But now if you add a threshold of 4, the next call is made as the condition now is satisfied: interprets as -> if (true && (0+6+4)>=10) which interprets as true!
总结一下,visibleThreshold是您添加逻辑的地方,您可以通过链接实现样板代码!
Summarizing, the visibleThreshold is where you add your logic, rest you can implement the boiler plate code from the link!
这篇关于Android:如何在recyclerView的后台加载更多数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!