Android:如何实现具有不同列/行大小的网格 [英] Android: how to implement grid with different columns/rows size

查看:94
本文介绍了Android:如何实现具有不同列/行大小的网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现以下类似内容:

I need to implement something like this:

好像是StaggeredGridLayoutManager | GridLayoutManager无法做到这一点, 目前如何实现?

Seems like StaggeredGridLayoutManager | GridLayoutManager can not do it, how to achieve it for now?

GridView是否具有良好的性能?

GridView has good performance?

推荐答案

您可以使用我测试了此库的1.0.0-SNAPSHOT版本.

I tested the 1.0.0-SNAPSHOT version of this lib.

这是一个旧项目(不再进行维护),但是它可以帮助您编写自己的RecyclerView/LayoutManager.

It's kind of old project (not maintened anymore), but it can help you to write your own RecyclerView/LayoutManager.

布局示例

在RecyclerViewAdapter中,您需要设置跨度逻辑:

In RecyclerViewAdapter you need to set the span logic:

@Override
 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        final SpannableGridLayoutManager.LayoutParams lp =
                (SpannableGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();

        final int span1;
        final int span2;
        if (position == 0) {
            span1 = 2;
            span2 = 2;
        } else if (position == 3) {
            span1 = 2;
            span2 = 3;
        } else {
            span1 = 1;
            span2 = 1;
        }

        final int colSpan = (span2);
        final int rowSpan = (span1);

        if (lp.rowSpan != rowSpan || lp.colSpan != colSpan) {
            lp.rowSpan = rowSpan;
            lp.colSpan = colSpan;
            holder.itemView.setLayoutParams(lp);
        }
}

我使用了来自库(TwoWayView)的自定义RecyclerView:

I used custom RecyclerView from the library (TwoWayView):

<org.lucasr.twowayview.widget.TwoWayView
        android:id="@+id/fragment_drawer_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="60dp"
        android:clipToPadding="false"
        android:paddingBottom="32dp"
        app:twowayview_layoutManager="SpannableGridLayoutManager"
        app:twowayview_numColumns="3"
        app:twowayview_numRows="3" />

结果截图:

这篇关于Android:如何实现具有不同列/行大小的网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆