监听RecyclerView ListView首次加载数据以及滑动时的item曝光的类
package com.zhaopin.ihr.home.view;
import android.widget.AbsListView;
import android.widget.ListView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
/**
* 监听RecyclerView ListView item曝光
*/
public class OnItemEnterOrExitVisibleHelper {
private int lastStart = -1;
private int lastEnd;
private OnScrollStatusListener listener;
public void setOnScrollStatusListener(OnScrollStatusListener listener) {
this.listener = listener;
}
public interface OnScrollStatusListener {
void onSelectEnterPosition(int position);
void onSelectExitPosition(int position);
}
private void dealScrollEvent(int firstVisible, int lastVisible) {
int visibleItemCount = lastVisible - firstVisible;
if (visibleItemCount > 0) {
if (lastStart == -1) {
lastStart = firstVisible;
lastEnd = lastVisible;
for (int i = lastStart; i < lastEnd + 1; i++) {
if (listener != null) {
listener.onSelectEnterPosition(i);
}
}
} else {
if (firstVisible != lastStart) {
if (firstVisible > lastStart) {//向上滑动
for (int i = lastStart; i < firstVisible; i++) {
if (listener != null) {
listener.onSelectExitPosition(i);
}
}
} else {//向下滑动
for (int i = firstVisible; i < lastStart; i++) {
if (listener != null) {
listener.onSelectEnterPosition(i);
}
}
}
lastStart = firstVisible;
}
//
if (lastVisible != lastEnd) {
if (lastVisible > lastEnd) {//向上滑动
for (int i = lastEnd; i < lastVisible; i++) {
if (listener != null) {
listener.onSelectEnterPosition(i + 1);
}
}
} else {//向下滑动
for (int i = lastVisible; i < lastEnd; i++) {
if (listener != null) {
listener.onSelectExitPosition(i + 1);
}
}
}
lastEnd = lastVisible;
}
}
}
}
public void setListViewScrollListener(ListView listView) {
listView.setOnScrollListener(listViewScrollListener);
}
public void setRecyclerScrollListener(RecyclerView recyclerView) {
recyclerView.addOnScrollListener(recyclerScrollListener);
}
public AbsListView.OnScrollListener getListViewScrollListener() {
return listViewScrollListener;
}
public RecyclerView.OnScrollListener getRecyclerScrollListener() {
return recyclerScrollListener;
}
private AbsListView.OnScrollListener listViewScrollListener = new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int paramVisibleItemCount, int totalItemCount) {
int firstVisible = view.getFirstVisiblePosition();
int lastVisible = view.getLastVisiblePosition();
dealScrollEvent(firstVisible, lastVisible);
}
};
private RecyclerView.OnScrollListener recyclerScrollListener = new RecyclerView.OnScrollListener() {
//RecyclerView
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
if (layoutManager != null) {
int firstVisible = layoutManager.findFirstVisibleItemPosition();
int lastVisible = layoutManager.findLastVisibleItemPosition();
int visibleItemCount = lastVisible - firstVisible;
if (lastVisible == 0) {
visibleItemCount = 0;
}
if (visibleItemCount != 0) {
dealScrollEvent(firstVisible, lastVisible);
}
}
}
};
}
item滑入或者滑出屏幕的回调
/**
* 列表Item曝光
*/
private OnItemEnterOrExitVisibleHelper.OnScrollStatusListener listener = new OnItemEnterOrExitVisibleHelper.OnScrollStatusListener() {
public void onSelectEnterPosition(int position) {
//此处写item滑入屏幕的曝光代码
}
public void onSelectExitPosition(int position) {
//此处写item滑出屏幕的曝光代码
}
}
代码中使用
OnItemEnterOrExitVisibleHelper helper = new OnItemEnterOrExitVisibleHelper();
helper.setRecyclerScrollListener(mRecyclerView);
helper.setOnScrollStatusListener(listener);