Я пытаюсь использовать 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>

И это мой MainActivity

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

И когда я пытаюсь скомпилировать это говорит

не может найти символ public final RecyclerView canadaList;

В

привязка данных \ ActivityMainBinding.java: 27: ошибка: не удается найти символ RecyclerView canadaList, ConstraintLayout constraintLay, SwipeRefreshLayout swipeContainer) {

Это потому, что он находится под несколькими слоями в моей деятельности. Как я могу ссылаться на RecyclerView?

Спасибо за любую помощь

2
Mysterious_android 17 Авг 2019 в 12:15

2 ответа

Лучший ответ

Замените 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>
1
Abu Noman 21 Авг 2019 в 07:20

Для людей, уже использующих версию Android для Android, добавление зависимости явно помогло.

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
1
Vairavan 29 Сен 2019 в 15:48