我正在使用下面的列表来实现RecyvlerView
。我已经使用了StaggeredGridLayoutManager
注意:此屏幕截图与GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
一切正常,但问题在于物品排列的顺序,它排列的物品如下:
| 0 | 1 |
| 3 | 2 |
| 4 | 5 |
| 7 | 6 |
如果您检查上图,您会注意到第二个项目在右侧绘制,而第四个项目在左侧,但我的要求是从右侧开始每一行,即第四个项目将出现在网格的右侧。我已经尝试过 GapStrategyStaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
和StaggeredGridLayoutManager.GAP_HANDLING_NONE
. 如果我使用GAP_HANDLING_NONE
,则项目不可见,但调用适配器的功能但输出为空白屏幕。是否有任何功能可以强制 StaggeredGridLayoutManager 从右侧开始绘制新行?
我正在使用以下代码计算项目高度:
public static void isSquired(View view, int position) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) view.getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
boolean isSquare = isSquare(position);
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (isSquare) {
lp.width = width / 2;
lp.height = width / 2;
}else{
lp.width = width / 2;
lp.height = (width / 2)+70;
}
view.setLayoutParams(lp);
view.requestLayout();
}
public static boolean isSquare(int position) {
int mod = position % 4;
return mod == 1 || mod == 3;
}
RecyclerView 初始化:
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setLayoutManager(manager);
recyclerView.setNestedScrollingEnabled(false);