0

我正在尝试在 recyclerview 中使用 gridlayoutmanager 制作如下 UI:

+-------+ +-------+
| | | |
| | | |
| | | |
+-------+ +-------+
+-----------------+
| |
| |
+-----------------+

我在这里调整 onBindViewHolder 的宽度:

int mode = (position+1)%4;
        int h_mul = 1, w_mul = 1;
        switch (mode){
            case 0:
                h_mul = w_mul = 2;
                break;
            case 2:
                w_mul = 2;
                break;
            case 3:
            case 1:
            default:
                h_mul = w_mul = 1;
                break;
        }
        mCardView.getLayoutParams().height = h_mul * mContext.getResources().getInteger(R.integer.video_tile_height);
        mCardView.getLayoutParams().width = w_mul * mContext.getResources().getInteger(R.integer.video_tile_width);

我尝试了多种方法: 1. 带有 setSpanSizeLookup 的水平网格布局管理器:

    manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            int mode = (position+1)%4;
            switch (mode){
                case 0:
                case 2:
                    return manager.getSpanCount();
                case 3:
                case 1:
                    return 1;
                default:
                    return -1;
            }
        }
    });

我最终是这样的:

+------------+ +-----------------+ +---- --------+
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+------------+ +-----------------+ +---- --------+

如果我不设置跨度,那么它最终会是这样的:

+------------+ +----------------+
| | | |
| | | |
| | | |
| | | |
| | | |
+------------+ +----------------+

+---------------+
| |
| |
| |
| |
| |
+---------------+

  1. 水平交错网格布局管理器。如果是使用

     StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
            layoutParams.setFullSpan(true);
    

然后它变得与

+------------+ +-----------------+ +---- --------+
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+------------+ +-----------------+ +---- --------+

如果我最初不使用全跨度,它是完美的,但如果我滚动 recyclerview,它会变得与上面相同:

+------------+ +-----------------+ +---- --------+
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+------------+ +-----------------+ +---- --------+

我不明白是什么原因造成的。有人可以指出我可能犯了什么错误吗?

4

1 回答 1

1

您想要实现一个水平回收器视图,您可以在其中重复这三个框。但是您将这 3 个框视为单独的回收站项目的方法不允许您分组滚动它们。您需要做的是定义一个单独的布局,其中包含这 3 个框。然后为它提供 3 个项目的数据并将其绑定在视图持有者中

基本上你必须操纵你的数据来分组喜欢这个然后显示它。

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == GROUP_ITEM) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.group_item, parent, false);
        return new GroupViewHolder(view);
    } else if (viewType == SINGLE_ITEM) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.single_item, parent, false);
        return new SingleViewHolder(view);
    }

    return null;
}

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof GroupViewHolder) {
        ((GroupViewHolder) holder).populateData(position);
    } else if (holder instanceof SingleViewHolder) {
      // populate your normal view.
    }
}

在此处输入图像描述

于 2018-02-21T08:41:56.390 回答