转:SwipeRefreshLayout + RecyclerView 实现 下拉刷新

mRecyclerView = (RecyclerView) findViewById(android.R.id.list);
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

@Override
public void onScrollStateChanged(RecyclerView recyclerView,
int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE
&& lastVisibleItem + 1 == adapter.getItemCount()) {
mSwipeRefreshWidget.setRefreshing(true);
// 此处在现实项目中,请换成网络请求数据代码,sendRequest .....
handler.sendEmptyMessageDelayed(0, 3000);
}
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
}
});

mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

adapter = new SampleAdapter();
mRecyclerView.setAdapter(adapter);

你可能感兴趣的:(转:SwipeRefreshLayout + RecyclerView 实现 下拉刷新)