Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android GridLayoutManager spansizelookup不工作_Android - Fatal编程技术网

Android GridLayoutManager spansizelookup不工作

Android GridLayoutManager spansizelookup不工作,android,Android,我正在使用GridLayoutManager,以便动态设置recyclerview的列数(每行)。这是我的密码: GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2); gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override

我正在使用GridLayoutManager,以便动态设置
recyclerview
的列数(每行)。这是我的密码:

GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int num = 0;
                if(position == 0)
                    num = 2;
                else if(position == 1)
                    num = 1;
                else if (position % 4 == 0)
                    num = 1;
                else
                    num = 2;
                Log.i("spansize", "spansize: " + num);
                return num;
            }
        });

        mAdapter = new ImageAdapter(getActivity(), mBlessingPics, options, ImageAdapter.POPULAR);

        mPopularImagesGrid.setLayoutManager(gridLayoutManager);

        mPopularImagesGrid.setAdapter(mAdapter);
        mAdapter.setOnClick(this);
但是,列的数量似乎没有更新。请看下图:

我可以在logcat中验证spansize的数量是否从2或1更改,但实际的recyclerview没有显示它。它只显示每行的一列/span

编辑: 我无法使第一行包含2个项目/列。第二项始终放置在第二行中。我打算在第二行有一列让位于本地广告,它将占据整行。我在spansizelookup中使用了这个:
返回(位置%3)=0?1 : 2;

以下是您的方法的重要部分:

if(position == 0)
    num = 2;
else if(position == 1)
    num = 1;
else if (position % 4 == 0)
    num = 1;
else
    num = 2;
因此,位置
1
4,8,12,16…
的跨度大小将为1,其他所有位置的跨度大小将为2。这意味着永远不会有两个项目彼此相邻且跨度大小为1,并且由于网格只有两个跨度宽,因此所有项目都需要位于自己的行中。我使用了您的
SpanSizeLookup
,但布局很简单,我看到:

因此,如果您希望有时看到两个相邻的图像,有时只看到一个,则需要使用不同的算法来查找跨度大小。例如:

return (position % 3) == 0 ? 2 : 1;

谢谢!我只是想知道为什么您给出的第一个代码在返回(位置%3)=0时不起作用?2 : 1;有效吗?在第一个代码中只有一列。与我的代码没有区别。@JaysonTamayo这是我的观点。我在告诉你为什么第一个代码不起作用(也就是说,您需要两个跨距为1的项目相邻,才能在一行中得到两个项目。1+2大于2,因此所有内容都在自己的行中。再次感谢!请查看编辑后的问题。我不能让第一行有2列。@JaysonTamayo如果您每次都
返回1
,而没有任何逻辑,会怎么样?那就好了至少应该告诉你问题出在
SpanSizeLookup
还是你的布局上。