我設法讓這個工作。我不確定這是否是最好的解決方案,但這是我所做的:
private int selectedPosition = -1;
private LinearLayoutManager layoutManager;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
LinearSnapHelper snapHelper = new LinearSnapHelper() {
@Override
public View findSnapView(RecyclerView.LayoutManager layoutManager) {
View view = super.findSnapView(layoutManager);
if (view != null) {
final int newPosition = layoutManager.getPosition(view);
if (newPosition != selectedPosition && recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE) {
onViewSnapped(newPosition);
selectedPosition = newPosition;
}
}
return view;
}
};
...
}
private void onViewSnapped(int index) {
// YOUR CODE HERE
}