Reputation: 618
I am trying to use Recycler and swipeRefreshLayout in my Main Activity but it just cannot find RecyclerView.
This is my 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>
And this is my 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
}
}
And when i try to compile it says
cannot find symbol public final RecyclerView canadaList;
In
databinding\ActivityMainBinding.java:27: error: cannot find symbol RecyclerView canadaList, ConstraintLayout constraintLay, SwipeRefreshLayout swipeContainer) {
Is it because it is under multiple layers in my activity_main ? How do I reference RecyclerView ?
Thank you for any help
Upvotes: 2
Views: 2174
Reputation: 1294
For folks already using androidx version of recycler view, adding the dependency explicitly helped.
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
Upvotes: 1
Reputation: 455
Change android.support.v7.widget.RecyclerView
to 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>
Upvotes: 1