我有一个管理间距和跨度的 GridSpacingItemDecoration 类。
这是代码:
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration
{
private int spanCount;
private int spacing;
private boolean includeEdge;
private boolean rtl;
public GridSpacingItemDecoration(boolean rtl, int spanCount, int spacing, boolean includeEdge)
{
this.rtl = rtl;
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
{
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge)
{
if (rtl)
{
outRect.right = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.left = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
}else {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
}
if (position < spanCount)
{ // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else
{
if (rtl){
outRect.right = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.left = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
}else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
}
if (position >= spanCount)
{
outRect.top = spacing; // item top
}
}
}
}
当我想要一个或多个列时,它工作得很好。 (如下图所示 - 所有间距和跨度都有效)
问题是我想使用具有不同 ViewTypes 和不同 spanCount 的 RecyclerView。这是我尝试这样做的方法:
在类中定义:
public static ArrayList<Integer> type = new ArrayList<>();
private int getTypeForPosition(int position)
{
return type.get(position);
}
private final int HEADER = 0;
private final int CHILD = 1;
private int dpToPx(int dp)
{
Resources r = getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
在方法中定义:
type.add(HEADER);
type.add(CHILD);
type.add(CHILD);
type.add(HEADER);
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(getTypeForPosition(position)) {
case HEADER:
return 2;
default:
return 1;
}
}
});
recyclerView.setLayoutManager(glm);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(true, 1, dpToPx(8), true));
ClassAdapter classAdapter = new ClassAdapter(getContext(), classes);
recyclerView.setAdapter(classAdapter);
问题是:同一行中两列之间的空间(如图所示)。好像是16,是我选的两倍。
问题:如何自定义 GridSpacingItemDecoration 类以在所有项目之间具有相同的空间?
最佳答案
这样做的方法是读取 View 的布局参数。
GridLayoutManager.LayoutParams params =
(GridLayoutManager.LayoutParams) view.getLayoutParameters()
那些layout parameters具有以下属性:
// Returns the current span index of this View.
int getSpanIndex()
// Returns the number of spans occupied by this View.
int getSpanSize()
通过这种方式,您可以检查 View 是哪一列以及它跨越多少列。
如果它在
0
列中,则在开始端应用完整偏移量,否则仅应用一半如果
spanIndex + spanSize
等于spanCount
(它占据最后一列),您将在末尾应用完整的偏移量,否则只应用一半。
为了更好的可重用性,您还应该考虑使用
((GridLayoutManager) parent.getLayoutManager()).getSpanCount()
获取总跨度的计数而不是在构造函数中设置它。通过这种方式,您可以动态更改/更新跨度计数,它仍然有效。
请不要忘记在转换和抛出适当的异常或其他东西之前检查 instanceof
;)
严格按照这些说明,我们最终得到以下装饰:
class GridSpanDecoration extends RecyclerView.ItemDecoration {
private final int padding;
public GridSpanDecoration(int padding) {
this.padding = padding;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager();
int spanCount = gridLayoutManager.getSpanCount();
GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();
int spanIndex = params.getSpanIndex();
int spanSize = params.getSpanSize();
// If it is in column 0 you apply the full offset on the start side, else only half
if (spanIndex == 0) {
outRect.left = padding;
} else {
outRect.left = padding / 2;
}
// If spanIndex + spanSize equals spanCount (it occupies the last column) you apply the full offset on the end, else only half.
if (spanIndex + spanSize == spanCount) {
outRect.right = padding;
} else {
outRect.right = padding / 2;
}
// just add some vertical padding as well
outRect.top = padding / 2;
outRect.bottom = padding / 2;
if(isLayoutRTL(parent)) {
int tmp = outRect.left;
outRect.left = outRect.right;
outRect.right = tmp;
}
}
@SuppressLint({"NewApi", "WrongConstant"})
private static boolean isLayoutRTL(RecyclerView parent) {
return parent.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL;
}
}
它允许任意数量的列并将它们正确对齐。
关于android - RecyclerView Item装饰间距和跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45660968/