android data binding error: cannot find symbol

android data binding error: cannot find symbol

我正在尝试在我的 Main Activity 中使用 Recycler 和 swipeRefreshLayout,但它就是找不到 RecyclerView。

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            tools:showIn="@layout/activity_main">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/swipeContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            <android.support.v7.widget.RecyclerView
                    android:id="@+id/canadaList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:padding="4dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
            />
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

这是我的主要内容Activity

class MainActivity : AppCompatActivity() {
        lateinit var swipeContainer: SwipeRefreshLayout 
        lateinit var activityMainBinder : ActivityMainBinding

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            activityMainBinder = DataBindingUtil.setContentView(this, R.layout.activity_main)
             // Error here says cannot access RecyclerView 
            val recyclerView = activityMainBinder.canadaList  

            val swipeContainer = activityMainBinder.swipeContainer 
        } 
    }

当我尝试编译它时说

cannot find symbol public final RecyclerView canadaList;

databinding\ActivityMainBinding.java:27: error: cannot find symbol RecyclerView canadaList, ConstraintLayout constraintLay, SwipeRefreshLayout swipeContainer) {

是不是因为在我的activity_main里面是多层的?我如何引用 RecyclerView ?

感谢您的帮助

android.support.v7.widget.RecyclerView更改为androidx.recyclerview.widget.RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            tools:showIn="@layout/activity_main">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/swipeContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/canadaList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:padding="4dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
            />
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

对于已经在使用 androidx 版本的 recycler view 的人来说,添加依赖项会有所帮助。

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'