第一个androidAPP项目总结—ListView的上拉和下拉

时间:2022-04-15 17:48:36

1.下拉刷新

需继承implements SwipeRefreshLayout.OnRefreshListener

@Override
public void onRefresh() {
new WeakHandler().postDelayed(new Runnable() {
@Override
public void run() {
if (getArguments() != null) {
mSwipeLayout.setRefreshing(true);
          //请求数据
getDateNet(0);
}
}
}, 300);
}

请求数据成功之后调用以下方法
private void refresh(int page, KaQuanMod[] m) {
//判断是为空
List<KaQuanMod> mMods = null;
if (m != null)
mMods = Arrays.asList(m);
else
mMods = new ArrayList<KaQuanMod>();
//page = 0 首次 刷新 ,>0 加载更多
if (page == 0) {
//首次加载不需要判断,因为adapter需要判断是否有数据,用于显示没有数据的样式
mylist.clear();
mylist.addAll(mMods);
mListAdapter = new KaQuanListViewAdapter(mActivity, mylist,useType,couponType);
listview.setAdapter(mListAdapter);
} else if (page > 0) {
if (ListUtils.isEmpty(mMods)) {
showToast(getString(R.string.no_more_data_find));
isHasMore = false;
return;
}
mylist.addAll(mMods);
mListAdapter.notifyDataSetChanged();
  }

}

2.上拉加载更多

lv为ListView
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
lastItem = firstVisibleItem + visibleItemCount;


}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (lastItem == ListUtils.getSize(mylist) && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
//滑动到底部,加载数据
        //请求加载下一页数据,加载成功后调上面的refresh方法
getDingDan(1, mylist.get(ListUtils.getSize(mylist) - 1).getCreateDate(), 1);

}
}
});