【问题标题】:Snapping RecyclerView android捕捉 RecyclerView android
【发布时间】:2016-03-31 03:53:05
【问题描述】:

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

但是我的所有子项目之间似乎有一些空间。请看下面的截图(我还不能嵌入图片,所以请检查链接):

我不确定为什么我的子项之间存在间距。我认为它与下面的代码有关:

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 项不会对齐到屏幕的中心。有人可以帮我指出正确的方向吗?

【问题讨论】:

  • 有什么帮助吗??有什么建议可以为我指明正确的方向吗?

标签: android android-recyclerview snappy snapping


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    更多信息和示例请查看here

    【讨论】:

    • 谢谢。一定会检查一下并让您知道
    猜你喜欢
    • 2018-07-16
    • 1970-01-01
    • 2018-10-21
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多