我正在尝试制作一个类似于Instagram滤镜布局的布局。基本上,当您选择一个滤镜时,它将滚动到您选择的项目+1,向您显示还有更多滤镜可供选择。
我目前正在尝试为我的水平RecyclerView构建自定义LinearLayoutManager:
滚动向左正常工作,但是当我向右滚动时,它似乎只跳到列表的末尾而不是newItem + 1,我不知道为什么会发生这种情况。
我目前正在尝试为我的水平RecyclerView构建自定义LinearLayoutManager:
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,我不知道为什么会发生这种情况。
getScrollRightPos
时,将旧的(未更新的)位置传递给了 mCurrentPos,因此它会滚动到旧位置 + 1(可能在 RecyclerView 的末尾)。此外,我认为您在点击不相邻的当前项时存在问题,这些情况下滚动是否正常工作? - FabioRsnap()
?int position
是从哪里来的? - Kirill