[英]Delete Extra Item Decoration within last row empty columns In a Recycler View
Im trying to delete the empty column Item decoration as per the below Image.我试图根据下图删除空列项目装饰。 I Initially thought to restrict the spancount for the last row as per the content using below code in comments but however Im not successful.
我最初想根据评论中使用以下代码的内容来限制最后一行的跨度计数,但我没有成功。
below is the code and Image as a result.下面是代码和图像结果。
val taskDatesView: RecyclerView = binding.taskDatesRecyclerView
val manager = GridLayoutManager(context,10)
taskDatesView.layoutManager = manager
taskDatesView.adapter = taskDatesAdapter
createTasksViewModel.taskDates.observe(viewLifecycleOwner) {
it?.let(taskDatesAdapter::setTaskDates)
val decoration = DividerItemDecoration(context, HORIZONTAL)
taskDatesView.addItemDecoration(decoration)
/*manager.spanSizeLookup = object :SpanSizeLookup(){
override fun getSpanSize(position: Int): Int {
val lastRowCount = (taskDatesAdapter.itemCount%10)
if(position>taskDatesAdapter.itemCount -lastRowCount){
return lastRowCount
}
return 10
}
}*/
when I uncommented the code and run the app below is the result当我取消注释代码并运行下面的应用程序时结果
Any code references or links will be helpful.任何代码参考或链接都会有所帮助。 Thanks in Advance.
提前致谢。
Within Custom Divider, modifying the bottom padding based on the lastRowCount it is working as expected.在自定义分隔符中,根据 lastRowCount 修改底部填充,它按预期工作。
Below is the working code and result also added comments within code for better understanding下面是工作代码和结果,还在代码中添加了注释以便更好地理解
fun drawHorizontal(c: Canvas?, parent: RecyclerView) {
val noOfColumns = 10
val top = parent.paddingTop
var bottom = parent.height - parent.paddingBottom
val childCount = parent.childCount
// values required not to draw the bottom for last empty columns
val lastRowCount = childCount % noOfColumns
val numOfRows = childCount / noOfColumns
for (i in 0 until childCount) {
val child = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
// this can be optimized to calculate only once for every row
val rowNumber = ((i + 1) / noOfColumns) + 1
// Within every row after crossing the lastRowCount the bottom should not be extended
if ((i * rowNumber) - (lastRowCount - 1) > 0) {
bottom = (child.height * (numOfRows))
}
val left = child.right + params.rightMargin + mDivider.intrinsicHeight
val right = left + mDivider.intrinsicWidth
mDivider.setBounds(left, top, right, bottom)
mDivider.draw(c!!)
}
}
If we need both vertical and horizontal orientations, use above and the below code如果我们需要垂直和水平方向,请使用上面和下面的代码
fun drawVertical(c: Canvas?, parent: RecyclerView){
val dividerLeft = parent.paddingLeft
val dividerRight = parent.width - parent.paddingRight
val childCount = parent.childCount
for (i in 0..childCount - 2) {
val child: View = parent.getChildAt(i)
val params = child.layoutParams as RecyclerView.LayoutParams
val dividerTop: Int = child.getBottom() + params.bottomMargin
val dividerBottom = dividerTop + mDivider.intrinsicHeight
mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom)
mDivider.draw(c)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.