问题
Thanks for giving your precious time to read this thread. I am developing an android app where there is recyclerview within recyclerview's item.
I want to perform some task when on scrolling the recyclerview(parent recyclerview) reaches to the upper position of the first item of parent (i.e; if the upper position of the first item is completely visible).
I've tried this so far but it is not giving the desired result what I need
recyclerView.addOnScrollListener(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);
LinearLayoutManager linearLayoutManager1 = (LinearLayoutManager) recyclerView.getLayoutManager();
int displayedPosition = linearLayoutManager1.findFirstCompletelyVisibleItemPosition();
if(displayedPosition==0){
//Is this the place where top position of first item is reached ?
((ControllableAppBarLayout) getActivity().findViewById(R.id.app_bar_layout)).expandToolbar(true);
}
}
});
回答1:
By using two combination of these method you can archive what you needed.
if (linearLayoutManager1.findFirstVisibleItemPosition() == 0
&& linearLayoutManager1.findFirstCompletelyVisibleItemPosition() == 0) {
// reach first fully visible item.
}
from op question code snippet..
- beware of NullPointerException for linearLayoutManager1, check null
- beware of ClassCastException for linearLayoutManager1, check instanceof
来源:https://stackoverflow.com/questions/36113642/how-to-get-top-item-of-recyclerview-on-scroll