给recyclerview设置滑动监听,直接上代码.
private int _firstItemPosition = -1, _lastItemPosition;
private View fistView, lastView;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//判断是当前layoutManager是否为LinearLayoutManager
// 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
//获取最后一个可见view的位置
int lastItemPosition = linearManager.findLastVisibleItemPosition();
//获取第一个可见view的位置
int firstItemPosition = linearManager.findFirstVisibleItemPosition();
//获取可见view的总数
int visibleItemCount = linearManager.getChildCount();
if (_firstItemPosition < firstItemPosition) {
_firstItemPosition = firstItemPosition;
_lastItemPosition = lastItemPosition;
GCView(fistView);
fistView = recyclerView.getChildAt(0);
lastView = recyclerVi