我上面有RecyclerView,我有一个AppBarLayout,其高度大于255像素。当用户滚动RecyclerView时,AppBarLayout会出现问题。为了避免这种情况,我决定手动扩展AppBarLayout。我的RecyclerView由GridLayoutManager组成,跨度为3.我使用下面的代码来监听RecyclerView的最大范围
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
int firstVisiblePosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
if (firstVisiblePosition == 0) {
appBarLayout.setExpanded(true, true);
}
}
}
});
答案 0 :(得分:3)
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstVisibleItem = gridLayoutManager.findFirstCompletelyVisibleItemPosition();
if(firstVisibleItem == 0){
// your code
}
}
});
答案 1 :(得分:0)
此答案将为您提供帮助。真的是你的
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = layoutManager.getItemCount();
visibleItemCount = layoutManager.getChildCount();
pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount ) {
Log.d(TAG, "scroll down");
}
else{
Log.d(TAG, "scroll up");
}
}
});