每当有人滚动到顶部时,我都会更新列表,但我希望它一次添加20个项目。 因为我将项目添加到顶部,所以它始终假设该情况并一次性添加所有数据。
我可以让它等待再次执行代码之前还是使smoothScrollToPosition()结束。
谢谢
listMensages.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.d(TAG, "onScroll: " +view.getLastVisiblePosition() +" "+ listMensages.getCount());
if(view.getFirstVisiblePosition() == 0 && listMensages.getCount()>=19*(k+1)-visibleItemCount && k<numberLists){
k++;
adapter.appendMessages(messagesList.subList(messagesList.size()-20*(k+1),messagesList.size()-21-((k-1)*20)));
listMensages.smoothScrollToPosition(19);
adapter.notifyDataSetChanged();
//loaded=true;
}
if(view.getFirstVisiblePosition() == 0 && listMensages.getCount()>=19-visibleItemCount && k==numberLists-1){
k++;
adapter.appendMessages(messagesList.subList(messagesList.indexOf(messagesList.get(0)),messagesList.size()-21-((k-1)*20)));
adapter.notifyDataSetChanged();
listMensages.smoothScrollToPosition(19);
}
}
});