我正在尝试制作类似于 Instagram 过滤器布局的布局。基本上,当您选择一个过滤器时,它会滚动到您选择的项目 + 1,向您显示还有更多过滤器。
我目前正在尝试在这里LinearLayoutManager
为我的水平建立一个自定义RecyclerView
:
public class LinearLayoutSnapManager extends LinearLayoutManager {
private int mCurrentPos = 0;
public LinearLayoutSnapManager(Context context) {
super(context);
}
public LinearLayoutSnapManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public LinearLayoutSnapManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void snap(RecyclerView rv, int position) {
if (mCurrentPos == position) {
// No move
return;
}
boolean goingRight = true;
if (position < mCurrentPos) {
goingRight = false;
}
mCurrentPos = position;
smoothScrollToPosition(rv, new RecyclerView.State(), goingRight ? getScrollRightPos(): getScrollLeftPos());
}
private int getScrollLeftPos() {
int newPos = mCurrentPos - 1;
return (newPos > 0) ? newPos : 0;
}
private int getScrollRightPos() {
return mCurrentPos + 1;
}
}
向左滚动按预期工作,但是当我向右滚动时,它似乎只是跳到列表的末尾而不是 newItem + 1,我不知道为什么会发生这种情况。