StaggeredGridLayoutManager项的排列

时间:2018-07-06 13:09:31

标签: android android-recyclerview staggeredgridlayoutmanager

我正在使用RecyvlerView实现以下列表。我已经使用StaggeredGridLayoutManager

完成了

enter image description here

注意::此屏幕截图与GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS

一切正常,但问题在于项目的排列顺序,它在排列项目,例如:

 | 0 | 1 |
 | 3 | 2 |
 | 4 | 5 |
 | 7 | 6 |

如果您查看上面的图像,您会注意到第二项在右侧绘制,而第四项在左侧绘制,但是我的要求是从右侧开始每一行,即第四项将在网格的右侧出现。我已经尝试了GapStrategy StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANSStaggeredGridLayoutManager.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);

0 个答案:

没有答案