10

我有一个RecyclerView用于显示 2 列布局,其中一些项目作为部分,使用全宽。这就是我设置我的方式RecyclerView

StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setItemAnimator(null);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new SpacesItemDecoration(Utils.dpToPx(8)));

这是我的SpaceItemDecoration课:

private class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;

        public SpacesItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view);
            int viewType = adapter.getItemViewType(position);
            if (viewType == 1) {
                //this is the regular view type
                if (map.get(position)) {
                    //it is on the left side
                    outRect.right = space / 2;
                    outRect.left = Utils.dpToPx(12);
                    outRect.top = space / 2;
                    outRect.bottom = space / 2;
                } else {
                    //it is on the right side
                    outRect.right = Utils.dpToPx(12);
                    outRect.left = space / 2;
                    outRect.top = space / 2;
                    outRect.bottom = space / 2;
                }
            } else {
                outRect.left = 0;
                outRect.right = 0;
                outRect.top = 0;
                outRect.bottom = 0;
            }
        }
}

基本上,我检查该项目是否属于部分/标题类型,还是必须在 2 列中显示的常规项目。此外,我需要知道常规项目是显示在左侧还是右侧。

这是我计算元素是显示在左侧还是右侧的方法:

private void prepareMapping(int start, int end) {
        LogUtil.i(TAG, "prepareMapping called");

//        LogUtil.i(TAG, "mapping from " + start + " to " + end);
        int lastHeaderPosition = -1;
        FeedItems currentItem;
        for (int i=start; i<end-1; i++) {
            currentItem = list.get(i);
            if (currentItem.getType() == 0) {
                //it is header
                lastHeaderPosition = i;
            } else if ((currentItem.getType() == 1) && ((i - lastHeaderPosition) % 2 == 0)) {
                //it is right side, put false into map
//                LogUtil.i(TAG, i + " is on right");
                map.put(i, false);
            } else if ((currentItem.getType() == 1) && ((i - lastHeaderPosition) % 2 != 0)) {
                //it is left side, put true into map
//                LogUtil.i(TAG, i + " is on right");
                map.put(i, true);
            }
        }
    }

因此,根据我的计算,如果元素中的元素按照RecyclerView它们在列表中的顺序显示为来自后端,则某些元素应该出现在左侧,而其他元素应该出现在右侧。但是,这个GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS策略打乱了我的项目顺序,因此我的左右计算不成立,我得到了奇怪的间距。

为了解决这个问题,我将间隙处理策略设置为GAP_HANDLING_NONE,现在RecyclerView不显示任何项目。为什么会发生这种情况,我该如何解决?

4

0 回答 0