Android RecyclerView Adapter DataBinding - 找不到符号 layoutBindingImpl
Posted
技术标签:
【中文标题】Android RecyclerView Adapter DataBinding - 找不到符号 layoutBindingImpl【英文标题】:Android RecyclerView Adapter DataBinding - cannot find symbol layoutBindingImplAndroid RecyclerView Adapter DataBinding - 找不到符号 layoutBindingImpl 【发布时间】:2019-06-29 10:50:03 【问题描述】:我有一个片段,它显示带有天气信息的城市列表。我正在使用 RecyclerView,我正在尝试在我的 RecyclerView 适配器中实现数据绑定库,但由于某种原因,我得到了这个编译错误:
> error: cannot find symbol import
com.example.zach.weatherapp.databinding.CityListItemBindingImpl;
> ^
> symbol: class CityListItemBindingImpl
> location: package com.example.zach.weatherapp.databinding
这是一个自动生成的类,所以我真的不知道错误在哪里。当 xml 文件中出现问题时,我之前对其他布局也有同样的错误,但在这里看起来很好。
ForecastAdapter.kt
package com.example.zach.weatherapp.Adapter
import ...
class ForecastAdapter(var myDataset: List<City>) :
RecyclerView.Adapter<ForecastAdapter.ForecastViewHolder>()
var context:Context?=null
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder.
class ForecastViewHolder(var binding: CityListItemBinding) : RecyclerView.ViewHolder(binding.root)
fun bind(city: City)
binding.city = city
// Create new views (invoked by the layout manager)
override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): ForecastAdapter.ForecastViewHolder
context = parent.context
val layoutIdForListItem = R.layout.city_list_item
val inflater = LayoutInflater.from(context)
val shouldAttachToParentImmediately = false
val binding = DataBindingUtil.inflate<CityListItemBinding>(inflater,layoutIdForListItem,parent,shouldAttachToParentImmediately)
//val view = inflater.inflate(layoutIdForListItem, parent, shouldAttachToParentImmediately)
return ForecastViewHolder(binding)
// Replace the contents of a view (invoked by the layout manager)
override fun onBindViewHolder(holder: ForecastViewHolder, position: Int)
val city = myDataset[position]
holder.bind(city)
Glide.with(context)
.load("http://openweathermap.org/img/w/$city.weather[0].icon.png")
.into(holder.binding.forceastImageView)
holder.binding.container.setOnClickListener view: View ->
Timber.d("Clicked on city %s",city.name)
Navigation.findNavController(view).navigate(ListFragmentDirections.actionListFragmentToForecastDetailsFragment(city.id))
// Return the size of your dataset (invoked by the layout manager)
override fun getItemCount() = myDataset.size
city_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="city" type="com.example.zach.weatherapp.data.City"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:orientation="horizontal"
android:layout_
android:layout_
android:id="@+id/container">
<TextView
tools:text="Caen"
android:text="@city.name"
android:layout_
android:layout_ android:id="@+id/city_name_textview"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent"
android:fontFamily="@font/roboto_light" android:textSize="22sp" android:textStyle="bold"
android:maxLines="1" android:ellipsize="end"/>
<TextView
tools:text="Sunny"
android:text="@city.weather[0].description"
android:layout_
android:layout_
android:id="@+id/city_forecast_textview" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toBottomOf="@+id/city_name_textview" android:fontFamily="@font/roboto_light"
android:layout_marginBottom="16dp" app:layout_constraintBottom_toBottomOf="parent"/>
<ImageView
android:layout_
android:layout_ app:srcCompat="@drawable/sunny"
android:id="@+id/forceast_imageView" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.562"
android:layout_marginEnd="32dp" app:layout_constraintEnd_toStartOf="@+id/temperatures_layout"/>
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_ android:id="@+id/temperatures_layout"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent">
<TextView
tools:text="15°"
android:text="@city.main.temp_max"
android:layout_
android:layout_ android:id="@+id/max_temperature_textview"
android:fontFamily="@font/roboto_light"
tools:layout_editor_absoluteY="17dp" tools:layout_editor_absoluteX="313dp"/>
<TextView
tools:text="9°"
android:text="@city.main.temp_min"
android:layout_
android:layout_
android:id="@+id/min_temperature_textview"
android:fontFamily="@font/roboto_light" tools:layout_editor_absoluteY="45dp"
tools:layout_editor_absoluteX="321dp" android:layout_gravity="right"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
这可能只是一个 Android Studio 错误,因为 xml 文件看起来不错。
更新:
错误似乎来自 Xml。我在我的 xml 布局中删除了android:text="@city.xxx"
,而是在我的 ViewHolder 绑定方法中手动更新了我的 textViews,如下所示:
fun bind(boundCity: City)
with(binding)
cityNameTextview.text = boundCity.name
cityForecastTextview.text = boundCity.weather[0].description
maxTemperatureTextview.text = "$boundCity.main.temp_max°"
minTemperatureTextview.text = "$boundCity.main.temp_min°"
Glide.with(root.context)
.load("http://openweathermap.org/img/w/$boundCity.weather[0].icon.png")
.into(forceastImageView)
container.setOnClickListener view: View ->
Timber.d("Clicked on city %s",boundCity.name)
Navigation.findNavController(view).navigate(ListFragmentDirections.actionListFragmentToForecastDetailsFragment(boundCity.id))
而且我不再收到错误消息。每当我在我的文本视图中添加android:text="@city.xx"
并在绑定方法中绑定城市变量时,就会出现错误。不过我还是不知道为什么....
【问题讨论】:
清除并重建您的项目或使缓存无效并重新启动android studio。 我确实尝试过,但还是遇到了同样的错误 你应该检查这个解释的答案***.com/a/51579759/6891563 我试过了,但没有解决我的问题 有同样的问题,当使用双向数据绑定时,但我注意到如果我在这里放置一个数据类而不是一个视图模型而不是它构建得很好 【参考方案1】:我相信这对你有用;
class ForecastAdapter(private var myDataset: List<City>) : RecyclerView.Adapter<ForecastAdapter.ForecastViewHolder>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ForecastAdapter.ForecastViewHolder
val itemBinding = CityListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ForecastViewHolder(itemBinding)
override fun onBindViewHolder(holder: ForecastViewHolder, position: Int)
val city = myDataset[position]
holder.bind(city)
override fun getItemCount() = myDataset.size
inner class ForecastViewHolder(var binding: CityListItemBinding) : RecyclerView.ViewHolder(binding.root)
fun bind(boundCity: City)
with(binding)
city = boundCity
Glide.with(root.context)
.load("http://openweathermap.org/img/w/$city.weather[0].icon.png")
.into(forceastImageView)
container.setOnClickListener view ->
Timber.d("Clicked on city %s", city.name)
Navigation.findNavController(view).navigate(ListFragmentDirections.actionListFragmentToForecastDetailsFragment(city.id))
【讨论】:
我试过你的方法,但我仍然得到同样的错误【参考方案2】:您可以尝试在<data>
标签之后添加以下行:
<import type="android.view.View" />
当我遇到该错误时,我发现这对我有用。
【讨论】:
什么行和什么标签? 嘿 Zach,编辑太差了,不知道发生了什么。但我修好了。希望它有所帮助:)以上是关于Android RecyclerView Adapter DataBinding - 找不到符号 layoutBindingImpl的主要内容,如果未能解决你的问题,请参考以下文章
Android中RecyclerView嵌套RecyclerView或嵌套ListView
Android RecyclerView嵌套RecyclerView
Android:RecyclerView里面的RecyclerView,滚动到底部