Overview:
The recycler view has a horizontal layout manager, in that linear layout manager i override the canScrollHorizontally method, so that the user can not scroll by himself.
Also i have a RecyclerView.SmoothScroller to change visible item programmatically, (the recycler view has a PagerSnapHelper attached, each item of the recylcler cover the entire screen, with this feature the recycler view works like ViewPager).
But when i call the method myRecyclerView.getLayoutManager().startSmoothScroll(smoothScrollerForward) the scroll can not been performed because the linear layout manager canScrollHorizontally return always false.
If i make canScrollHorizontally method returns true before call startSmoothScroll it works, but how to know when myRecyclerView.getLayoutManager().startSmoothScroll(smoothScrollerForward) ends? so that i can move the variable returned by canScrollHorizontally to false again.
Here is the code:
private RecyclerView rvCards;
private LinearLayoutManager manager;
private RecyclerView.SmoothScroller smoothScrollerForward;
private int currentPosition;
private boolean horizontalScroll = false;
rvCards = findViewById(R.id.rvCards);
manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,
false)
{
@Override
public boolean canScrollHorizontally() {
return horizontalScroll;
}
};
rvCards.setLayoutManager(manager);
PagerSnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(rvCards);
smoothScrollerForward = new LinearSmoothScroller(this){
@Override
protected int getHorizontalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_END;
}
};
ivForwardCard.setOnClickListener(v -> {
smoothScrollerForward.setTargetPosition(++currentPosition);
rvCards.getLayoutManager().startSmoothScroll(smoothScrollerForward);
});
public void changeItem(int position) {
horizontalScroll = true;
ivForwardCard.performClick();
}