listView.setonRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
Log.e(TAG, "---下拉刷新---");
new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... params) {
try {
mylineslistlist.clear();
mylinesorderlist.clear();
list.clear();
getMylines(0);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
adapter.notifyDataSetChanged();
listView.onRefreshComplete();
}
}.execute();
}
});
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(final AbsListView view,
int scrollState) {
listView.onScrollStateChanged(view, scrollState);
// 數據為空--不用繼續下面代碼了
if (list.size() == 0)
return;
// 判斷是否滾動到底部
boolean scrollEnd = false;
try {
if (view.getPositionForView(loadingLayout) == view
.getLastVisiblePosition())
scrollEnd = true;
} catch (Exception e) {
scrollEnd = false;
}
if (scrollEnd) {
// 如果是自動加載,可以在這裡放置異步加載數據的代碼
Log.e("LOADMORE", "loading...");
if (currentPage < pageNum) {
// Log.e(TAG, "---currentPage is " + currentPage);
currentPage++;
// Log.e(TAG, "---currentPage is " + currentPage);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Log.d(TAG, "---loadData page is" +
// currentPage);
getMylines(currentPage);
// listView.setSelection(view
// .getLastVisiblePosition() - 1); // 設置選中項
// setLoadView("加載更多"); //恢復按鈕文字
}
}, 1000);
} else {
listView.removeFooterView(loadingLayout);
}
}
}
mDataArrays.add(entity);
mAdapter.notifyDataSetChanged(); //及時通知listview更新
lv_info_contain.setSelection(mAdapter.getCount());
這些代碼是將listview最新那條數據添在最後面,你可以修改下就能用。