Android: how to implement grid with different columns/rows size

和自甴很熟 提交于 2019-11-30 17:51:38

问题


I need to implement something like this:

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

GridView has good performance?


回答1:


You can achieve this kind of RecyclerView behavior with twoway-view

I tested the 1.0.0-SNAPSHOT version of this lib.

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

Example of your layout

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);
        }
}

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" />

Result screenshot:




回答2:


Since the two-way library is deprecated, you can look up GridLayoutManager. There is GridLayoutManager.SpanSizeLookup class which you can use. See this link http://android-er.blogspot.in/2015/07/set-spansizelookup-to-gridlayoutmanager.html



来源:https://stackoverflow.com/questions/38569868/android-how-to-implement-grid-with-different-columns-rows-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!