如果列表大小为奇数,我试图将第一个项目设置为水平居中
GridLayoutManager gridLayoutManager = new GridLayoutManager(recyclerViewHolder.recyclerView.getContext(), 2);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (position == 0&&questionsAndAnswersChatModel.get(position).getAnswers().size()%2!=0) {
return 2;
}
return 1;
}
});
答案 0 :(得分:0)
以上代码只会在第一行插入一个项目,但要将其居中,请将layout_gravity = center
参数添加到RecyclerView
<RecyclerView
...
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
.../>