2016-11-25 2106 views
0

我使用GridLayoutManagerRecyclerViewGridLayoutManager.HORIZONTAL爲方向。 SpanCount對我來說工作不正常,因爲它取決於佈局方向,但我知道我需要多少列和行。我怎麼能做到這一點?GridLayoutManager設置列和行計數

從技術文檔到setSpanCount(int spanCount)

/** 
* Sets the number of spans to be laid out. 
* <p> 
* If {@link #getOrientation()} is {@link #VERTICAL}, this is the number of columns. 
* If {@link #getOrientation()} is {@link #HORIZONTAL}, this is the number of rows. 
* 
* @param spanCount The total number of spans in the grid 
* @see #getSpanCount() 
*/ 

例子:我RecyclerView大小match_parent,如果我有9個項目,我有網格3x3的,如果我有5個項目,我有網格3x2的,但我想的2x3 。

P.S.我不想設置GridLayoutManager.VERTICAL佈局方向。

在截圖不正確,我想格2x3。

it is not correct for me

回答

0

你應該根據你的數據大小設置跨度動態計數。

gridLayoutManager.setSpanCount((int) Math.ceil(data.size()/3f)); 

因此,對於7,8和9項,您有3行。對於4,5,6和2行,依此類推。 使用float(3f)非常重要,因此您的分區不會被截斷。

+0

它在行之間留下空間當它的4-6個項目 – ViVekH