我想为(n)个项目列表实现这个布局:

到目前为止,我可以在两列中显示项目,但是如何将两列合并到一个特定位置(即作为项目7)?
我已经尝试了许多在SO和其他地方提供的解决方案,但没有一个对我有效。那里提供的解决方案要么是使用LinearLayout膨胀静态布局,要么是为列中的分区提供解决方案。
我也尝试过使用setSpanLookup方法,但没有成功。
发布于 2019-07-01 18:49:04
最后,在我竭尽全力之后,我终于找到了解决方案:
@Override
public void onViewAttachedToWindow(MyViewHolder holder) {
super.onViewAttachedToWindow(holder);
ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
if(lp != null
&& lp instanceof StaggeredGridLayoutManager.LayoutParams
&& (holder.getLayoutPosition() == 6 || holder.getLayoutPosition() == 13)) {
StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
p.setFullSpan(true);
}
holder.setIsRecyclable(false);
}希望这也能对你有所帮助。
发布于 2019-07-01 17:54:18
正如你所说,我认为setSpanLookup是最好的方法。
尝试以下代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val layoutManager = GridLayoutManager(this, 2)
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
if (position == 6 || position == 13) return 2
else return 1
}
}
rv.layoutManager = layoutManager
val items = mutableListOf<String>()
for (i in 1..14) {
items.add("item$i")
}
rv.adapter = MyAdapter(items)
}
}您需要知道getSpanSize返回的值表示权重,而不是列计数。
在项目布局方面,您可以在特殊位置使用不同的ViewHolder。
例如:
companion object{
const val NORMAL = 0
const val WIDE = 1
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
...
if (viewType == WIDE) return WideViewHolder(wideView)
else return NormalViewHolder(normalView)
}
override fun getItemViewType(position: Int): Int {
if (position == 6 || position == 13) {
return WIDE
}
return NORMAL
}发布于 2019-07-01 17:56:04
只需创建一个表格:在您的示例中是8行3列,设置边框和边框对齐,第7项和第14项将采用3个单元格。下面是一个示例:
private void insertCell(PdfPTable table, String text, int align, int colspan, Font font) {
//create a new cell with the specified Text and Font
PdfPCell cell = new PdfPCell(new Phrase(text.trim(), font));
//set the cell alignment
cell.setHorizontalAlignment(align);
//set the cell column span in case you want to merge two or more cells
cell.setColspan(colspan);
//in case there is no text and you wan to create an empty row
if (text.trim().equalsIgnoreCase("")) {
cell.setMinimumHeight(10f);
}
//add the call to the table
table.addCell(cell);
}https://stackoverflow.com/questions/56833225
复制相似问题