【发布时间】: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