Snapping RecyclerView android

Snapping RecyclerView android,android,android-recyclerview,snappy,snapping,Android,Android Recyclerview,Snappy,Snapping,我正在尝试为我的水平recyclerView实现一种捕捉机制,我正在使用代码来实现它 但我的所有子项之间似乎都有一些空间。请参见下面的屏幕截图(我还不允许嵌入图像,所以请检查链接): 我不确定我的子项之间为什么有间隔。我认为这与下面的代码有关: private void setMarginsForChild(View child) { int lastItemIndex = getLayoutManager().getItemCount() - 1; int childInde

我正在尝试为我的水平recyclerView实现一种捕捉机制,我正在使用代码来实现它

但我的所有子项之间似乎都有一些空间。请参见下面的屏幕截图(我还不允许嵌入图像,所以请检查链接):

我不确定我的子项之间为什么有间隔。我认为这与下面的代码有关:

private void setMarginsForChild(View child) {
    int lastItemIndex = getLayoutManager().getItemCount() - 1;
    int childIndex = getChildPosition(child);
    if(childIndex != -1) ((SampleAdapter)getAdapter()).setSelection(childIndex);
    int startMargin = childIndex == 0 ? getMeasuredWidth() / 2 : getMeasuredWidth() / 2;
    int endMargin = childIndex == lastItemIndex ? getMeasuredWidth() / 2 : getMeasuredWidth() / 2;

    //RTL works for API 17+
    if (ViewCompat.getLayoutDirection(child) == ViewCompat.LAYOUT_DIRECTION_RTL) {
        // The view has RTL layout
        ((ViewGroup.MarginLayoutParams) child.getLayoutParams()).setMargins(endMargin, 0, startMargin, 0);
    } else {
        // The view has LTR layout
        ((ViewGroup.MarginLayoutParams) child.getLayoutParams()).setMargins(startMargin, 0, endMargin, 0);
    }
    child.requestLayout();
}

但是如果我没有将边距设置为子项,则recyclerView项不会捕捉到屏幕的中心。有人能帮我指出正确的方向吗

所以我用这个来工作。
它是一个自定义ScrollListener,可用于在滚动后将列表项捕捉到中心。以防万一将来有人会遇到这个问题

,所以我用这个解决了这个问题。 它是一个自定义ScrollListener,可用于在滚动后将列表项捕捉到中心。为了防止将来有人遇到这个问题,还有另一个选项,特别是它将此行为与其他方便的功能一起包装在布局管理器中

有关更多信息和示例,请检查

还有另一个选项,特别是它将此行为与其他方便的功能一起包装在布局管理器中


有关更多信息和示例,请查看

任何帮助??有什么建议给我指出正确的方向吗?有什么帮助吗??有什么建议给我指出正确的方向吗?谢谢。我一定会看看这个,让你知道谢谢。我一定会检查一下,让你知道