3

我有一个回调,它是通过编程滚动到 RecyclerView 的某个项目后触发的LinearLayoutManager.smoothScrollToPosition()。用户点击一个项目,右边的项目滚动到 RecyclerView 的顶部。我对 LinearLayoutManager 进行了子类化,以使其始终捕捉到项目的顶部。
这在触发滚动事件的情况下有效,但是当 RecyclerView 已经在正确的位置时,我没有收到onScrollStateChanged回调,因为没有发生滚动。有没有办法得到那个事件?比如事先决定 RecyclerView 是否需要滚动?

4

2 回答 2

1

希望下面的代码会有所帮助

if(LinearLayoutManager.findFirstCompletelyVisibleItem() == yourDesiredPosition) {
  //do your stuff
} else {
  LinearLayoutManager.scrollToPositionWithOffset(yourDesiredPosition, offset);
  //onScrollStateChanged would be trigger then.
}
于 2016-07-12T07:04:32.010 回答
-1

我自己找到了以下解决方案:

// get the view the user selected
View view = mLayoutManager.findViewByPosition(index);
// get top offset 
int offset = view.getTop();
if (offset == 0) { // the view is at the top of the scrollview
    showDetailViewInternal(event);
} else {
    // scrolling
}
于 2016-07-13T07:49:16.600 回答