我试图得到a中最后一个项目的X位置
androidx.leanback.widget.HorizontalGridView
到目前为止,我有这样的东西,有时有效,而其他人则不行
private fun getXOffsetViewBindOfLastView(): Float? {
val childView = viewBinding.rvItems.layoutManager?.findViewByPosition(viewBinding.rvItems.childCount - 1) ?: return null
viewBinding.rvItems.layoutManager
val offsetViewBounds = Rect()
//returns the visible bounds
childView.getDrawingRect(offsetViewBounds)
// calculates the relative coordinates to the parent
viewBinding.rvItems.offsetDescendantRectToMyCoords(childView, offsetViewBounds)
val relativeTop: Int = offsetViewBounds.top
val relativeLeft: Int = offsetViewBounds.left
val relativeBottom: Int = offsetViewBounds.bottom
val relativeRight: Int = offsetViewBounds.right
logger.d(TAG, "getXOffsetViewBindOfLastView", "lastView coordinates: top=$relativeTop, left=$relativeLeft, bottom=$relativeBottom, right=$relativeRight")
return relativeRight.toFloat() - childView.width
}
xml就是这样。
<androidx.leanback.widget.HorizontalGridView
android:id="@+id/rv_items"
android:layout_width="match_parent"
android:layout_height="@dimen/rail_row_items_height"
android:padding="@dimen/my_home_items_padding_top"
android:layout_marginTop="@dimen/rail_row_items_margin_top"
android:clipChildren="false"
android:clipToPadding="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/rrh_header_item" />
有时,childView为null,它只是返回null。
因为这不是一个常规的
RecyclerView