所以我能够使用的一个解决方案是使用项目装饰器,但这绝对有点怪异/ hack怪的感觉。
基本上,您将根据列的位置(或类似内容)调整项目的外部矩形。我的理解是,外部矩形或多或少是您要更改的间距。尝试一下下面的代码,显然,您需要进行自己的调整和逻辑以“计算”该项目在哪一列上,但这应该足以弄清楚它,希望:recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int left = outRect.left;
int right = outRect.right;
int top = outRect.top;
int bottom = outRect.bottom;
int idx = parent.getChildPosition(view);
int perRow = gridLayoutManager.getSpanCount();
int adj = blahh... // some adjustment
if (idx < itemsPerRow) {
// on first row, adjust top if needed
}
if(idx % perRow == 0){
// on first column, adjust. Left magically adjusts bottom, so adjust it too...
left += adj;
bottom -= adj;
}
if(idx % itemsPerRow == perRow - 1){
// on last column, adjust. Right magically adjusts bottom, so adjust it too...
right += adjustment;
bottom -= adjustment;
}
outRect.set(left, top, right, bottom);
}
});
同样,这很骇人,需要经过反复试验才能正确。
我尝试成功的另一种解决方案是为不同的列定义不同的视图。在您的情况下,各列的左右两边将具有不同的负边距视图,以获得所需的效果。
作为旁注,我假设您正在卡片视图上使用高程。我注意到的一件事是,如果名片视图没有高程,而是您自己来处理(是的,我知道,这不是您自己不处理高程的要点),这种困难大部分消失了,事情开始发生,可能是由于高程/阴影计算所致。但是无论如何...希望这至少对您有所帮助...