我的应用有一个水平滚动的回收视图。 当我滚动列表并到达最后位置时,我想触发另一个事件。
答案 0 :(得分:1)
只需在recyclerview上实现addOnScrollListener()。然后在滚动侦听器内部实现下面的代码。
RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (mIsLoading)
return;
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
if (pastVisibleItems + visibleItemCount >= totalItemCount) {
//End of list
}
}
};
答案 1 :(得分:0)
private RecyclerView.OnScrollListener nRecyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
@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);
int visibleItemCount = notificationLayoutManager.getChildCount();
int totalItemCount = notificationLayoutManager.getItemCount();
int firstVisibleItemPosition = notificationLayoutManager.findFirstVisibleItemPosition();
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount) {
nLoadMoreItems();
}
}
};
private void nLoadMoreItems() {
if (defaultCurrentPage < defaultpageCount)
layout_load_more_loadingNotification.setVisibility(View.VISIBLE);
int pageCount = (defaultCurrentPage + 1);
//fetch second page data
}
试试吧
答案 2 :(得分:0)
private RecyclerView.OnScrollListener nRecyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == 2) {
try {
if (lastVisibleItemPosition + 1 == totalItemCount) {
//Write you code here
}
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, e.getMessage());
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = mLayoutManager.getItemCount();
lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
}
};
将监听器添加到回收站视图
recyclerView.addOnScrollListener(nRecyclerViewOnScrollListener);