BindingAdapter
BindingAdapter copied to clipboard
滚轮选择器如果动态设置字体大小会导致recyclerview直接显示全部选项(似乎是高度计算不正确)
如下图所示,当itemView设置了padding或marg,或者是修改了文字大小,会导致recyclerview显示的高度出现错误,直接显示出所有的item项了
主要是想实现选中的item的文字大小和未选中的item文字大小是不一样的效果
itemView布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#252935"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvItem"
android:textColor="#50535E"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Android"/>
</LinearLayout>
recyclerview的高度设置为
wrap_content
Activity中的代码:
val list = (30..100).map { it.toString() }
val adapter =
BindingAdapter<String, RvItemPickerBinding>(RvItemPickerBinding::inflate) { position, item ->
itemBinding.tvItem.text = item
if (isWheelItemSelected) {
itemBinding.tvItem.setTextColor(Color.WHITE)
//修改字体大小也会出现计算高度错误
//itemBinding.tvItem.textSize = SizeUtils.sp2px(22f).toFloat()
} else {
itemBinding.tvItem.setTextColor("#50535E".toColorInt())
//itemBinding.tvItem.textSize = SizeUtils.sp2px(14f).toFloat()
}
}
binding.apply {
rvPicker.adapter = adapter
adapter.data.addAll(list)
adapter.notifyDataSetChanged()
val module = rvPicker.setupWheelModule()
module.apply {
offset = 1 //上下偏移多少
orientation = RecyclerWheelViewModule.VERTICAL//方向
}
module.selectedPosition = list.indexOf("70")
}