无法使用 Android 绑定在 RecyclerView 上绑定 RatingBar
Can't bind a RatingBar on the RecyclerView using Android binding
我正在使用 kotlin 绑定放置在 RecyclerView 上的 RatingBar,但出现以下错误:
Cannot find the setter for attribute 'app:ratingValue' with parameter type java.lang.Double on android.widget.RatingBar.
我尝试关注不同的博客,了解如何将值设置为 RatingBar,但我无法在适配器中实现它。
下面是我的模型 class :
@Entity(tableName = "Results")
class Result {
companion object {
@JvmStatic
@BindingAdapter("ratingValue")
fun setRating(ratingBar: RatingBar, mVoteAverage: Float) {
if (mVoteAverage != null) {
ratingBar.rating = mVoteAverage
val stars = ratingBar.progressDrawable as LayerDrawable
stars.getDrawable(2).setColorFilter(ContextCompat.getColor(ratingBar.context, R.color.rating_bar), PorterDuff.Mode.SRC_ATOP)
val roundVal = Math.round(mVoteAverage!!)
ratingBar.numStars = roundVal
}
}
}
constructor(mId: Long?, mOverview: String?, mPosterPath: String?, mTitle: String?, mVoteAverage: Double?) {
this.mId = mId
this.mOverview = mOverview
this.mPosterPath = mPosterPath
this.mTitle = mTitle
this.mVoteAverage = mVoteAverage
}
constructor()
@PrimaryKey
@SerializedName("id")
var mId: Long? = null
@SerializedName("overview")
var mOverview: String? = null
@SerializedName("poster_path")
var mPosterPath: String? = null
@SerializedName("title")
var mTitle: String? = null
@SerializedName("vote_average")
var mVoteAverage: Double? = null
}
然后,这是可绑定的 XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="movie"
type="com.movieapp.huxymovies.model.Result" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@color/bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RatingBar
android:id="@+id/rating_bar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
app:ratingValue="@{movie.mVoteAverage}"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</layout>
那么这是我的适配器 class :
class ResultAdapter(private val context: Context) : PagedListAdapter<Result, ResultAdapter.ResultViewHolder>(DIFF_CALLBACK) {
public lateinit var mBinding: ItemActivitymainBinding
override fun onBindViewHolder(holder: ResultViewHolder, position: Int) {
val result = getItem(position)
if (result != null) {
holder.itemActivitymainBinding.movie = result
holder.itemActivitymainBinding.ratingBar.rating = result.mVoteAverage as (Float)
holder.itemActivitymainBinding.titleTxt.text = result.mTitle
}
}
class ResultViewHolder(itemView: ItemActivitymainBinding) : RecyclerView.ViewHolder(itemView.root) {
var itemActivitymainBinding: ItemActivitymainBinding
var root: View
init {
root = itemView.root
itemActivitymainBinding = itemView
}
}
}
}
我想我在 onBindViewHolder()
中遗漏了一些东西,但找不到,我遗漏了什么?
你的 Result
class 的 mVoteAverage: Double?
属性 是 Double
。
您 @BindingAdapter
方法有一个 Float
参数。这就是绑定不起作用的原因。改变
@JvmStatic
@BindingAdapter("ratingValue")
fun setRating(ratingBar: RatingBar, mVoteAverage: Float)
到
@JvmStatic
@BindingAdapter("ratingValue")
fun setRating(ratingBar: RatingBar, mVoteAverage: Double)
应该可以解决问题。
我正在使用 kotlin 绑定放置在 RecyclerView 上的 RatingBar,但出现以下错误:
Cannot find the setter for attribute 'app:ratingValue' with parameter type java.lang.Double on android.widget.RatingBar.
我尝试关注不同的博客,了解如何将值设置为 RatingBar,但我无法在适配器中实现它。
下面是我的模型 class :
@Entity(tableName = "Results")
class Result {
companion object {
@JvmStatic
@BindingAdapter("ratingValue")
fun setRating(ratingBar: RatingBar, mVoteAverage: Float) {
if (mVoteAverage != null) {
ratingBar.rating = mVoteAverage
val stars = ratingBar.progressDrawable as LayerDrawable
stars.getDrawable(2).setColorFilter(ContextCompat.getColor(ratingBar.context, R.color.rating_bar), PorterDuff.Mode.SRC_ATOP)
val roundVal = Math.round(mVoteAverage!!)
ratingBar.numStars = roundVal
}
}
}
constructor(mId: Long?, mOverview: String?, mPosterPath: String?, mTitle: String?, mVoteAverage: Double?) {
this.mId = mId
this.mOverview = mOverview
this.mPosterPath = mPosterPath
this.mTitle = mTitle
this.mVoteAverage = mVoteAverage
}
constructor()
@PrimaryKey
@SerializedName("id")
var mId: Long? = null
@SerializedName("overview")
var mOverview: String? = null
@SerializedName("poster_path")
var mPosterPath: String? = null
@SerializedName("title")
var mTitle: String? = null
@SerializedName("vote_average")
var mVoteAverage: Double? = null
}
然后,这是可绑定的 XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="movie"
type="com.movieapp.huxymovies.model.Result" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@color/bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RatingBar
android:id="@+id/rating_bar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
app:ratingValue="@{movie.mVoteAverage}"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</layout>
那么这是我的适配器 class :
class ResultAdapter(private val context: Context) : PagedListAdapter<Result, ResultAdapter.ResultViewHolder>(DIFF_CALLBACK) {
public lateinit var mBinding: ItemActivitymainBinding
override fun onBindViewHolder(holder: ResultViewHolder, position: Int) {
val result = getItem(position)
if (result != null) {
holder.itemActivitymainBinding.movie = result
holder.itemActivitymainBinding.ratingBar.rating = result.mVoteAverage as (Float)
holder.itemActivitymainBinding.titleTxt.text = result.mTitle
}
}
class ResultViewHolder(itemView: ItemActivitymainBinding) : RecyclerView.ViewHolder(itemView.root) {
var itemActivitymainBinding: ItemActivitymainBinding
var root: View
init {
root = itemView.root
itemActivitymainBinding = itemView
}
}
}
}
我想我在 onBindViewHolder()
中遗漏了一些东西,但找不到,我遗漏了什么?
你的 Result
class 的 mVoteAverage: Double?
属性 是 Double
。
您 @BindingAdapter
方法有一个 Float
参数。这就是绑定不起作用的原因。改变
@JvmStatic
@BindingAdapter("ratingValue")
fun setRating(ratingBar: RatingBar, mVoteAverage: Float)
到
@JvmStatic
@BindingAdapter("ratingValue")
fun setRating(ratingBar: RatingBar, mVoteAverage: Double)
应该可以解决问题。