Android RecyclerView Adapter DataBinding-找不到符号{layout} BindingImpl [英] Android RecyclerView Adapter DataBinding - cannot find symbol {layout}BindingImpl

查看:152
本文介绍了Android RecyclerView Adapter DataBinding-找不到符号{layout} BindingImpl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段,显示带有天气信息的城市列表.我正在使用RecyclerView,并且尝试在RecyclerView适配器中实现数据绑定库,但是由于某些原因,我遇到了此编译错误:

I have a Fragment that displays a list of cities with weather informations. I am using a RecyclerView and I am trying to implement the Data Binding Library in my RecyclerView Adapter but for some reason I get this compile error :

> error: cannot find symbol import 
         com.example.zach.weatherapp.databinding.CityListItemBindingImpl;
>                                               ^   
> symbol:   class CityListItemBindingImpl   
> location: package com.example.zach.weatherapp.databinding

这是一个自动生成的类,所以我真的不知道错误在哪里.当xml文件中出现某些错误时,我之前在其他布局中也遇到了相同的错误,但是在这里看起来还不错.

It's an auto generated class so i really don't know where the error is. I had the same error previously for other layouts when there was someting wrong in the xml file but here it seems fine.

ForecastAdapter.kt

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

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_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/container">

        <TextView
                tools:text="Caen"
                android:text="@{city.name}"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 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_width="wrap_content"
                android:layout_height="wrap_content"
                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_width="48dp"
                android:layout_height="48dp" 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_width="wrap_content"
                android:layout_height="wrap_content" 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_width="wrap_content"
                    android:layout_height="wrap_content" 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_width="wrap_content"
                    android:layout_height="wrap_content"
                    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文件看起来不错.

It might just be an Android Studio error because the xml file seems fine.

更新: 错误似乎来自Xml.我在xml布局中删除了android:text="@{city.xxx}",而是在ViewHolder绑定方法中手动更新了textViews,如下所示:

UPDATE : Error seems to come from Xml. I removed the android:text="@{city.xxx}" in my xml layout and instead updated my textViews manually in my ViewHolder bind method like so :

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))}
        }
    }

而且我不再收到该错误.每当我在Textviews中添加android:text="@{city.xx}"并在bind方法中绑定city变量时,都会出现错误.我还是不知道为什么...

And I no longer get the error. The error comes whenever I add android:text="@{city.xx}" in my textviews and bind the city variable in the bind method. I still don't know why though....

推荐答案

嘿,您可以尝试在<data>标签之后添加以下行:

Hey you could try adding the following line after the <data> tag :

<import type="android.view.View" /> 

当我遇到该错误时,我发现这对我有用.

I found that worked for me when I had that error.

这篇关于Android RecyclerView Adapter DataBinding-找不到符号{layout} BindingImpl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆