在滚动时检测回收视图何时到达底部的大部分位置

我有一个回收视图的代码。

    recyclerView = (RecyclerView)rootview.findViewById(R.id.fabric_recyclerView);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new RV_Item_Spacing(5));
FabricAdapter fabricAdapter=new FabricAdapter(ViewAdsCollection.getFabricAdsDetailsAsArray());
recyclerView.setAdapter(fabricAdapter);

我需要知道当回收视图到达底部的大部分位置,同时滚动。有可能吗?如果有,怎么做?

122462 次浏览

只需在回收视图上实现 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
}
}
};

这是我的实现,对于 StaggeredGridLayout非常有用。

用法:

private EndlessScrollListener scrollListener =
new EndlessScrollListener(new EndlessScrollListener.RefreshList() {
@Override public void onRefresh(int pageNumber) {
//end of the list
}
});


rvMain.addOnScrollListener(scrollListener);

监听器实现:

class EndlessScrollListener extends RecyclerView.OnScrollListener {
private boolean isLoading;
private boolean hasMorePages;
private int pageNumber = 0;
private RefreshList refreshList;
private boolean isRefreshing;
private int pastVisibleItems;


EndlessScrollListener(RefreshList refreshList) {
this.isLoading = false;
this.hasMorePages = true;
this.refreshList = refreshList;
}


@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
StaggeredGridLayoutManager manager =
(StaggeredGridLayoutManager) recyclerView.getLayoutManager();


int visibleItemCount = manager.getChildCount();
int totalItemCount = manager.getItemCount();
int[] firstVisibleItems = manager.findFirstVisibleItemPositions(null);
if (firstVisibleItems != null && firstVisibleItems.length > 0) {
pastVisibleItems = firstVisibleItems[0];
}


if (visibleItemCount + pastVisibleItems >= totalItemCount && !isLoading) {
isLoading = true;
if (hasMorePages && !isRefreshing) {
isRefreshing = true;
new Handler().postDelayed(new Runnable() {
@Override public void run() {
refreshList.onRefresh(pageNumber);
}
}, 200);
}
} else {
isLoading = false;
}
}


public void noMorePages() {
this.hasMorePages = false;
}


void notifyMorePages() {
isRefreshing = false;
pageNumber = pageNumber + 1;
}


interface RefreshList {
void onRefresh(int pageNumber);
}  }

还有一个简单的方法

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);


if (!recyclerView.canScrollVertically(1)) {
Toast.makeText(YourActivity.this, "Last", Toast.LENGTH_LONG).show();


}
}
});

方向整数: -1表示向上,1表示向下,0总是返回 false。

我也在寻找这个问题,但是我没有找到令我满意的答案,所以我创建了自己的实现,回收视图。

其他解决方案不如我的精确。例如: 如果最后一个项目是相当大的(大量的文本) ,然后回调其他解决方案将来得更早,然后回收视图实际达到底部。

我的解决方案解决了这个问题。

class CustomRecyclerView: RecyclerView{


abstract class TopAndBottomListener{
open fun onBottomNow(onBottomNow:Boolean){}
open fun onTopNow(onTopNow:Boolean){}
}




constructor(c:Context):this(c, null)
constructor(c:Context, attr:AttributeSet?):super(c, attr, 0)
constructor(c:Context, attr:AttributeSet?, defStyle:Int):super(c, attr, defStyle)




private var linearLayoutManager:LinearLayoutManager? = null
private var topAndBottomListener:TopAndBottomListener? = null
private var onBottomNow = false
private var onTopNow = false
private var onBottomTopScrollListener:RecyclerView.OnScrollListener? = null




fun setTopAndBottomListener(l:TopAndBottomListener?){
if (l != null){
checkLayoutManager()


onBottomTopScrollListener = createBottomAndTopScrollListener()
addOnScrollListener(onBottomTopScrollListener)
topAndBottomListener = l
} else {
removeOnScrollListener(onBottomTopScrollListener)
topAndBottomListener = null
}
}


private fun createBottomAndTopScrollListener() = object :RecyclerView.OnScrollListener(){
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
checkOnTop()
checkOnBottom()
}
}


private fun checkOnTop(){
val firstVisible = linearLayoutManager!!.findFirstCompletelyVisibleItemPosition()
if(firstVisible == 0 || firstVisible == -1 && !canScrollToTop()){
if (!onTopNow) {
onTopNow = true
topAndBottomListener?.onTopNow(true)
}
} else if (onTopNow){
onTopNow = false
topAndBottomListener?.onTopNow(false)
}
}


private fun checkOnBottom(){
var lastVisible = linearLayoutManager!!.findLastCompletelyVisibleItemPosition()
val size = linearLayoutManager!!.itemCount - 1
if(lastVisible == size || lastVisible == -1 && !canScrollToBottom()){
if (!onBottomNow){
onBottomNow = true
topAndBottomListener?.onBottomNow(true)
}
} else if(onBottomNow){
onBottomNow = false
topAndBottomListener?.onBottomNow(false)
}
}




private fun checkLayoutManager(){
if (layoutManager is LinearLayoutManager)
linearLayoutManager = layoutManager as LinearLayoutManager
else
throw Exception("for using this listener, please set LinearLayoutManager")
}


private fun canScrollToTop():Boolean = canScrollVertically(-1)
private fun canScrollToBottom():Boolean = canScrollVertically(1)
}

然后在你的活动/片段:

override fun onCreate() {
customRecyclerView.layoutManager = LinearLayoutManager(context)
}


override fun onResume() {
super.onResume()
customRecyclerView.setTopAndBottomListener(this)
}


override fun onStop() {
super.onStop()
customRecyclerView.setTopAndBottomListener(null)
}

希望它能帮助某人; -)

试试这个

我已经使用了上面的答案,它总是运行,当你将去在回收视图的结尾,

如果您只想检查一次它是否在底部? 例如:-如果我有10个项目的列表,每当我去底部它会显示我,如果我滚动从上到下它不会再次打印,如果你添加更多的列表,你去那里,它会再次显示。

注意:- 在命中 API 时使用此方法处理 偏移

  1. 创建一个名为 Endles迴 clViewScrollListener 的类

        import android.support.v7.widget.GridLayoutManager;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.StaggeredGridLayoutManager;
    
    
    public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
    // The minimum amount of items to have below your current scroll position
    // before loading more.
    private int visibleThreshold = 5;
    // The current offset index of data you have loaded
    private int currentPage = 0;
    // The total number of items in the dataset after the last load
    private int previousTotalItemCount = 0;
    // True if we are still waiting for the last set of data to load.
    private boolean loading = true;
    // Sets the starting page index
    private int startingPageIndex = 0;
    
    
    RecyclerView.LayoutManager mLayoutManager;
    
    
    public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
    this.mLayoutManager = layoutManager;
    }
    
    
    //    public EndlessRecyclerViewScrollListener() {
    //        this.mLayoutManager = layoutManager;
    //        visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
    //    }
    
    
    public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
    this.mLayoutManager = layoutManager;
    visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
    }
    
    
    public int getLastVisibleItem(int[] lastVisibleItemPositions) {
    int maxSize = 0;
    for (int i = 0; i < lastVisibleItemPositions.length; i++) {
    if (i == 0) {
    maxSize = lastVisibleItemPositions[i];
    }
    else if (lastVisibleItemPositions[i] > maxSize) {
    maxSize = lastVisibleItemPositions[i];
    }
    }
    return maxSize;
    }
    
    
    // This happens many times a second during a scroll, so be wary of the code you place here.
    // We are given a few useful parameters to help us work out if we need to load some more data,
    // but first we check if we are waiting for the previous load to finish.
    @Override
    public void onScrolled(RecyclerView view, int dx, int dy) {
    int lastVisibleItemPosition = 0;
    int totalItemCount = mLayoutManager.getItemCount();
    
    
    if (mLayoutManager instanceof StaggeredGridLayoutManager) {
    int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
    // get maximum element within the list
    lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
    } else if (mLayoutManager instanceof GridLayoutManager) {
    lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
    } else if (mLayoutManager instanceof LinearLayoutManager) {
    lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
    }
    
    
    // If the total item count is zero and the previous isn't, assume the
    // list is invalidated and should be reset back to initial state
    if (totalItemCount < previousTotalItemCount) {
    this.currentPage = this.startingPageIndex;
    this.previousTotalItemCount = totalItemCount;
    if (totalItemCount == 0) {
    this.loading = true;
    }
    }
    // If it’s still loading, we check to see if the dataset count has
    // changed, if so we conclude it has finished loading and update the current page
    // number and total item count.
    if (loading && (totalItemCount > previousTotalItemCount)) {
    loading = false;
    previousTotalItemCount = totalItemCount;
    }
    
    
    // If it isn’t currently loading, we check to see if we have breached
    // the visibleThreshold and need to reload more data.
    // If we do need to reload some more data, we execute onLoadMore to fetch the data.
    // threshold should reflect how many total columns there are too
    if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
    currentPage++;
    onLoadMore(currentPage, totalItemCount, view);
    loading = true;
    }
    }
    
    
    // Call this method whenever performing new searches
    public void resetState() {
    this.currentPage = this.startingPageIndex;
    this.previousTotalItemCount = 0;
    this.loading = true;
    }
    
    
    // Defines the process for actually loading more data based on page
    public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);
    
    
    }
    
  2. 像这样使用这个类

         LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener( linearLayoutManager) {
    @Override
    public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
    Toast.makeText(getActivity(),"LAst",Toast.LENGTH_LONG).show();
    }
    });
    

它在我这边运行完美,如果你有任何问题,请评论我

答案是在 Kotlin,它将在 Java 中工作。如果你复制和粘贴,IntelliJ 应该会为你转换它。

recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){


override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {


// 3 lines below are not needed.
Log.d("TAG","Last visible item is: ${gridLayoutManager.findLastVisibleItemPosition()}")
Log.d("TAG","Item count is: ${gridLayoutManager.itemCount}")
Log.d("TAG","end? : ${gridLayoutManager.findLastVisibleItemPosition() == gridLayoutManager.itemCount-1}")


if(gridLayoutManager.findLastVisibleItemPosition() == gridLayoutManager.itemCount-1){
// We have reached the end of the recycler view.
}


super.onScrolled(recyclerView, dx, dy)
}
})

这也适用于 LinearLayoutManager,因为它具有与上面使用的相同的方法。即 findLastVisibleItemPosition()getItemCount()(Kotlin 的 itemCount)。

这是我的解决办法:

    val onScrollListener = object : RecyclerView.OnScrollListener() {


override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
directionDown = dy > 0
}


override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (recyclerView.canScrollVertically(1).not()
&& state != State.UPDATING
&& newState == RecyclerView.SCROLL_STATE_IDLE
&& directionDown) {
state = State.UPDATING
// TODO do what you want  when you reach bottom, direction
// is down and flag for non-duplicate execution
}
}
}

我们可以使用接口获得的位置

界面: 为侦听器创建接口

public interface OnTopReachListener { void onTopReached(int position);}

活动:

mediaRecycleAdapter = new MediaRecycleAdapter(Class.this, taskList); recycle.setAdapter(mediaRecycleAdapter); mediaRecycleAdapter.setOnSchrollPostionListener(new OnTopReachListener() {
@Override
public void onTopReached(int position) {
Log.i("Position","onTopReached "+position);
}
});

适配器:

public void setOnSchrollPostionListener(OnTopReachListener topReachListener) {
this.topReachListener = topReachListener;}@Override public void onBindViewHolder(MyViewHolder holder, int position) {if(position == 0) {
topReachListener.onTopReached(position);}}

使用此代码可避免重复调用

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);


if (!recyclerView.canScrollVertically(1) && newState==RecyclerView.SCROLL_STATE_IDLE) {
Log.d("-----","end");
                

}
}
});

我没有得到一个完美的解决方案,以上的答案,因为它是触发两次,即使在 onScrolled

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if( !recyclerView.canScrollVertically(RecyclerView.FOCUS_DOWN))
context?.toast("Scroll end reached")
}

我几天前找到的替代方案,

  rv_repatriations.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (!recyclerView.canScrollVertically(RecyclerView.FOCUS_DOWN) && recyclerView.scrollState == RecyclerView.SCROLL_STATE_IDLE
&& !isLoaded
) {
isLoaded = true
//do what you want here and after calling the function change the value of boolean
Log.e("RepatriationFragment", "Scroll end reached")
}
}
})

使用布尔值来确保在触底时不会被多次调用。

在对这个帖子中的大多数其他答案不满意之后,我发现了一些我认为更好的东西,但是在这里没有任何地方。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (!recyclerView.canScrollVertically(1) && dy > 0)
{
//scrolled to BOTTOM
}else if (!recyclerView.canScrollVertically(-1) && dy < 0)
{
//scrolled to TOP
}
}
});

这很简单,当你滚动到顶部或底部的时候,在所有条件下只会触发一次。

利用科特林

   recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (!recyclerView.canScrollVertically(1)) {
Toast.makeText(context, "Last", Toast.LENGTH_LONG).show();
}
}
})

对于这个问题,我已经看到了很多回答,我认为所有的回答都没有给出准确的行为结果。然而,如果你遵循这种方法,我肯定你会得到最好的行为。

RvCategory 是您的回收视图

TypeoriesList 是传递给适配器的列表

binding.rvCategories.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
val position = (recyclerView.layoutManager as LinearLayoutManager).findLastCompletelyVisibleItemPosition()
if (position + 1 == categoriesList.size) {
// END OF RECYCLERVIEW IS REACHED
} else {
// END OF RECYCLERVIEW IS NOT REACHED
}
}
})

你可以使用这个,如果你把1,这将指示当你停留在列表的结束,如果你想现在当你停留在列表的开始,你改变1为 -1

recyclerChat.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (!recyclerView.canScrollVertically(1)) {
               

}
}
})

Kotlin 答案

对于 底部滚动跟随最佳实践,您可以使用这个 Kotlin 函数来创建 无穷无尽永无止境滚动。

// Scroll listener.
private fun setupListenerPostListScroll() {
val scrollDirectionDown = 1 // Scroll down is +1, up is -1.
var currentListSize = 0


mRecyclerView.addOnScrollListener(
object : RecyclerView.OnScrollListener() {


override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)


if (!recyclerView.canScrollVertically(scrollDirectionDown)
&& newState == RecyclerView.SCROLL_STATE_IDLE
) {
val listSizeAfterLoading = recyclerView.layoutManager!!.itemCount


// List has more item.
if (currentListSize != listSizeAfterLoading) {
currentListSize = listSizeAfterLoading


// Get more posts.
postListScrollUpAction(listSizeAfterLoading)
}
else { // List comes limit.
showToastMessageShort("No more items.")
}
}
}
})
}

这是我在阅读了这篇文章中的所有答案后的解决方案。 我只想在列表末尾显示最后一个项目时显示 loading,而且 listview的长度大于屏幕高度,这意味着如果列表中只有一个或两个项目,就不会显示 loading

private var isLoadMoreLoading: Boolean = false


mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {


override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)


if (!isLoadMoreLoading) {
if (linearLayoutManager.findLastCompletelyVisibleItemPosition() == (list.size-1)) {
if (recyclerView.canScrollVertically(-1)) {


adapter?.addLoadMoreView()
loadMore()
}
}
}
}
})


private fun loadMore() {
isLoadMoreLoading = true
//call loadMore api
}

由于这个 linearLayoutManager.findLastCompletelyVisibleItemPosition() == (list.size-1),我们可以知道最后一个项目是显示,但我们也需要知道列表视图可以滚动或不。

因此我添加了 recyclerView.canScrollVertically(-1)。一旦你点击列表的底部,它就不能再向下滚动了。-1意味着列表可以向上滚动。这意味着 listview的长度大于屏幕的高度。

答案就在 Kotlin。

最简单的方法是像这样在适配器中实现:

@Override
public void onBindViewHolder(HistoryMessageListAdapter.ItemViewHolder holder, int position) {
if (position == getItemCount()-1){
listener.onLastItemReached();
}
}

因为只要回收了最后一项,侦听器就会被触发。

大多数答案构造不良,并存在一些问题。一个常见的问题是,如果用户滚动很快,那么到达的结束块会执行多次,我已经找到了一个解决方案,其中结束块只运行一次。

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (yourLayoutManager.findLastVisibleItemPosition() ==
yourLayoutManager.itemCount - 1 && !recyclerView.canScrollVertically(1)) {
Logger.log("End reached")
// Do your operations
}


super.onScrolled(recyclerView, dx, dy)
}

附注: 有时候如果 RecyclerView为空,最终侦听器可能会被调用。作为解决方案,您还可以在上面的代码中添加此检查。

if (recyclerView.adapter?.itemCount!! > 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);
               

if(dy > 0){ // Scrolling Down
                 



//**Look at the condition inside if, this is how you can check, either you //have scrolled till last element of your recyclerView or not.**


//------------------------------------------------------------------------------


LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();


if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == recyclerViewList.size() - 1) {
//bottom of list!
}
//------------------------------------------------------------------------------
                    

}else if(dy < 0){
// Scrolling Up
                   

}


}
});

经过长时间的搜索,我找到了一个完美的解决方案,只有当你想要滚动到底部时,它还能保持你通过使用 ListAdapter 得到的平滑滚动行为:

    adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
super.onItemRangeInserted(positionStart, itemCount)


scrollToBottomIfUserIsAtTheBottom(linearLayoutManager, positionStart)
}
})


private fun scrollToBottomIfUserIsAtTheBottom(linearLayout: LinearLayoutManager, positionStart: Int) {
val messageCount = adapter.itemCount
val lastVisiblePosition = linearLayout.findLastCompletelyVisibleItemPosition()
if (lastVisiblePosition == -1 ||
(positionStart >= (messageCount - 1) &&
lastVisiblePosition == (positionStart - 1)))
{
recyclerView.scrollToPosition(positionStart)
}
}

一些注意事项:

  • 如果使用列表适配器,并在每次更新时向列表中添加一个新项,则必须每次创建一个新列表,以便平滑滚动工作。
  • 不要在列表适配器中使用 smoothScrollToPosition!它破坏了 ListAdapter 的“平滑”行为,当 diff 工作正常时,它会检测到新旧列表之间的更改出现在新添加的项目中。
  • 线性布局管理器是 adapter.layoutManager as LinearLayoutManager

试试这个,

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);


int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
int totalItemCount = recyclerView.getLayoutManager().getItemCount();
int firstVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();


final int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount) {
if(previousLast != lastItem) {
previousLast = lastItem;
load();
}
}
}
});