首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自配交错GridLayoutManager

自配交错GridLayoutManager
EN

Stack Overflow用户
提问于 2015-11-07 02:53:51
回答 1查看 2K关注 0票数 1

我有一个与这篇文章相同的问题:

RecyclerView GridLayoutManager: how to auto-detect span count?

但对于,错开的 GridLayoutManager。我试着从this very good answer编辑代码

代码语言:javascript
复制
public class StaggeredGridAutofitLayoutManager extends StaggeredGridLayoutManager {
    private int mColumnWidth;
    private boolean mColumnWidthChanged = true;

    public StaggeredGridAutofitLayoutManager(Context context, int columnWidth, int orientation) {
        /* Initially set spanCount to 1, will be changed automatically later. */
        super(1, orientation);
        setColumnWidth(checkedColumnWidth(context, columnWidth));
    }

    private int checkedColumnWidth(Context context, int columnWidth) {
        if (columnWidth <= 0) {
            /* Set default columnWidth value (48dp here). It is better to move this constant
            to static constant on top, but we need context to convert it to dp, so can't really
            do so. */
            columnWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48,
                    context.getResources().getDisplayMetrics());
        }
        return columnWidth;
    }

    public void setColumnWidth(int newColumnWidth) {
        if (newColumnWidth > 0 && newColumnWidth != mColumnWidth) {
            mColumnWidth = newColumnWidth;
            mColumnWidthChanged = true;
        }
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        if (mColumnWidthChanged && mColumnWidth > 0) {
            int totalSpace;
            if (getOrientation() == VERTICAL) {
                totalSpace = getWidth() - getPaddingRight() - getPaddingLeft();
            } else {
                totalSpace = getHeight() - getPaddingTop() - getPaddingBottom();
            }
            int spanCount = Math.max(1, totalSpace / mColumnWidth);
            setSpanCount(spanCount);
            mColumnWidthChanged = false;
        }
        super.onLayoutChildren(recycler, state);
    }
}

但是应用程序崩溃时出现了以下错误:

在RecyclerView计算布局或滚动时无法调用此方法 (链接到setSpanCount(spanCount));行=> )

有谁能帮我做一下修改吗?

EN

回答 1

Stack Overflow用户

发布于 2016-01-17 17:05:23

原因

我和你有同样的问题。

造成这种情况的原因是在“计算布局或滚动”期间,框架块方法setSpanCount(int spanCount)用于StaggeredGridLayoutManager

这是一个解释(在RecyclerView.java中找到):

代码语言:javascript
复制
/**
 * This variable is incremented during a dispatchLayout and/or scroll.
 * Some methods should not be called during these periods (e.g. adapter data change).
 * Doing so will create hard to find bugs so we better check it and throw an exception.
 *
 * @see #assertInLayoutOrScroll(String)
 * @see #assertNotInLayoutOrScroll(String)
 */
private int mLayoutOrScrollCounter = 0;

因此,他们检查了它并抛出了一个异常:D

解决方案

当我接受时,我不能以最好的方式完成它,并且仍然不想使用ViewTreeObserver解决方案,我将setSpanCount(spanCount)更改为:

代码语言:javascript
复制
new Handler(context.getMainLooper()).post(new Runnable() {
       @Override
       public void run() {
            setSpanCount(spanCount);
       }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33578649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档