我知道以前曾经问过,但我没有赢。我的网络服务器上有一组包含数千条记录的数据,我想一次填充我的Recyclerview列表20。我该如何从阵列加载数据?我不认为一次加载所有一千个是最佳选择,因为它耗费时间并且还使用大量数据。我有以下代码,允许我检测是否已使用静态数据到达当前列表的末尾,但我不知道如何实现异步任务。
这是我的滚动监听器:
public abstract class EndlessRecyclerScroll extends RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerScroll.class.getSimpleName();
private int previousTotal = 0;
private Boolean loading = true;
private int visibleThreshold = 5;
int firstVisibleItem, visibleItemCount,totalItemCount;
private int current_page = 1;
private LinearLayoutManager linearLayoutManager;
public EndlessRecyclerScroll(LinearLayoutManager linearLayoutManager){
this.linearLayoutManager = linearLayoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy){
super.onScrolled(recyclerView,dx,dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = linearLayoutManager.getItemCount();
firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
if(loading){
if(totalItemCount > previousTotal){
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading
&& (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
current_page++;
onLoadMore(current_page);
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}
这是我的主要活动,提供静态数据:
public class MainActivity extends AppCompatActivity {
List<String> list;
RecyclerView recyclerView;
LinearLayoutManager linearLayoutManager;
RVAdapter rvAdapter;
private static int current_page = 1;
private int ival = 1;
private int loadLimit = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<>();
loadData(current_page);
setRecyclerView();
recyclerView.setOnScrollListener(new EndlessRecyclerScroll(linearLayoutManager) {
@Override
public void onLoadMore(int current_page) {
loadMoreData(current_page);
}
});
}
void setRecyclerView(){
recyclerView = (RecyclerView)findViewById(R.id.itemRV);
recyclerView.setHasFixedSize(true);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
rvAdapter = new RVAdapter(list);
recyclerView.setAdapter(rvAdapter);
}
void loadData(int current_page){
for (int i = ival; i <= loadLimit; i++) {
list.add("Item "+i);
ival++;
}
}
private void loadMoreData(int current_page) {
// I have not used current page for showing demo, if u use a webservice
// then it is useful for every call request
loadLimit = ival + 10;
for (int i = ival; i <= loadLimit; i++) {
list.add("Item "+i);
ival++;
}
rvAdapter.notifyDataSetChanged();
}
我从这里遵循指南来实现这一目标:http://android-pratap.blogspot.com/2015/01/endless-recyclerview-onscrolllistener.html?m=1
帮助?请
答案 0 :(得分:0)
好的,我设法解决了这个问题,我猜我起初只是懒惰。所以我所要做的就是理解PHP分页的概念,并将增加的页码发送到php脚本。
对于那些可能对这件事情不熟悉的人,请按照以下两个链接:
http://android-pratap.blogspot.com/2015/01/endless-recyclerview-onscrolllistener.html?m=1