Skip to main content

Android: Displaying a List of items using RecyclerView

Since ListView is now considered legacy, and I am learning Android from scratch I thought I might as well start with the preferred method, RecyclerView.

It wasn't as complicated as it first appeared.

I think of it like this

  • Store all items you want to display in an array.
  • Create a new layout file that contains a RecyclerView widget that will will display each item in our 
  • Map the items in the array to to a layout manager so they will display in the view.

 

To use use the RecyclerView widget, I had to first add the dependencey to my build.gradle file for your app module

dependencies {
    implementation 'com.android.support:recyclerview-v7:28.0.0'
}

In reality my build.gradle dependency list looks like this:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
}

Next I need to sync this new dependency with the project.

  • app
    • app/src/main/AndroidManifest.xml
    • com.example.list
      • com/example/list/ListAdapter.kt
      • com.example.list.MainActivity
  •  
  •  
  • layout
  • layout/activity_main.xml
  • layout/recycler_view_item.xml