【发布时间】:2022-11-06 03:33:17
【问题描述】:
几天来,我一直在尝试制作一个列表,其效果与某些 Wear OS 应用程序的效果相同,例如 Google Play Store,其中列表正在缩小正在消失的项目并扩展正在出现的新项目:
我遵循了 Google Developers 文档,问题是当我将自定义布局管理器应用于它时,这些项目不会保持居中,它们看起来像这样:
这是我使用的代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.wear.widget.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/box_inset_layout_padding"
tools:context=".ui.activity.HomeActivity"
tools:deviceIds="wear">
<androidx.wear.widget.WearableRecyclerView
android:id="@+id/wearable_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.wear.widget.BoxInsetLayout>
private fun setupAdapter() {
val homeOptionsAdapter = HomeOptionsAdapter(petList)
val customScrollingLayoutCallback = CustomScrollingLayoutCallback()
recyclerWearable.layoutManager = WearableLinearLayoutManager(
this,
customScrollingLayoutCallback
)
recyclerWearable.isEdgeItemsCenteringEnabled = true
recyclerWearable.adapter = homeOptionsAdapter
}
private const val MAX_ICON_PROGRESS = 2f
class CustomScrollingLayoutCallback : WearableLinearLayoutManager.LayoutCallback() {
private var progressToCenter: Float = 0f
override fun onLayoutFinished(child: View, parent: RecyclerView) {
child.apply {
// Figure out % progress from top to bottom
val centerOffset = height.toFloat() / 2.0f / parent.height.toFloat()
val yRelativeToCenterOffset = y / parent.height + centerOffset
// Normalize for center
progressToCenter = abs(0.5f - yRelativeToCenterOffset)
// Adjust to the maximum scale
progressToCenter = progressToCenter.coerceAtMost(MAX_ICON_PROGRESS)
scaleX = 1 - progressToCenter.pow(2f)
scaleY = 1 - progressToCenter.pow(2f)
}
}
}
我用选项 setEdgeItemsCenteringEnabled 读到了列表居中,但我没有看到它居中,我该怎么办?
【问题讨论】:
标签: android kotlin android-recyclerview watch