Nig*_*ury 13 android gridlayoutmanager android-recyclerview
我试图在回收者视图中使最后一行中心的项目水平.我正在使用GridLayoutManager以实现功能.使用setSpanSizeLookup,我想根据SO上的帖子调整最后一行的跨度大小,但遗憾的是我无法跟踪当前正在填充哪一行,因此我的逻辑无效.
我想要实现的是:
static private int NUM_OF_COLUMNS = 3;
final GridLayoutManager manager = new GridLayoutManager(getBaseActivity(), NUM_OF_COLUMNS);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
int rowNum = 0;
@Override
public int getSpanSize(int position) {
int spanSize = 1;
if(position == 0)
rowNum = 0; //reset it
//check if it is last row
int numOfRows = (int) Math.ceil((double) list.size() / NUM_OF_COLUMNS);
if(rowNum == numOfRows - 1) {
//get num of items in last row
int items = list.size() - (NUM_OF_COLUMNS * rowNum);
if(items == 1)
spanSize = 3;
}
if(rowNum % NUM_OF_COLUMNS == 0) //if last element
rowNum++;
return spanSize;
}
});
recyclerView.setLayoutManager(manager);
Run Code Online (Sandbox Code Playgroud)
上面的代码是管理行号,因为我不知道如何获取网格的当前行和列位置.有人能指向正确的方向吗?谢谢.
看看这个。它是谷歌库,它将 CSS 弹性框布局模块的类似功能带到了 Android。
在活动中
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();
layoutManager.setFlexWrap(FlexWrap.WRAP);
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setAlignItems(AlignItems.CENTER);
viewDecks.setLayoutManager(layoutManager);
Run Code Online (Sandbox Code Playgroud)
在 Adapter.ViewHolder 中
ViewGroup.LayoutParams lp = itemView.getLayoutParams();
if (lp instanceof FlexboxLayoutManager.LayoutParams) {
FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
flexboxLp.setFlexGrow(1.0f);
flexboxLp.setAlignSelf(AlignSelf.CENTER);
}
Run Code Online (Sandbox Code Playgroud)
并确保你设置 android:layout_gravity="center"
| 归档时间: |
|
| 查看次数: |
1552 次 |
| 最近记录: |