1. 到底部自动加载更多
//加载更多
mRecyclerView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
lastVisibleItemPos=mRecyclerView.getLastVisibleItemPosition();
super.onScrolled(recyclerView, dx, dy);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//每次加载10条数据,lastVisibleItemPos为当前总数-1
if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleItemPos==adapter.getItemCount()-1){
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
int size=lists.size();
newLists=dbHelper.getLimitList(lists,size);
lists.addAll(newLists);
//改变adapter中list,刷新数据
adapter.setList(lists);
adapter.notifyDataSetChanged();
}
},1000);
}
super.onScrollStateChanged(recyclerView, newState);
}
});
数据库用的GreenDAO,offset是已取出的list数
![Uploading 从数据库中取10条_958131.png . . .]
Adapter类:
public void setList(ArrayList<Entity> lists){
this.lists=lists;
}