Difference between revisions of "Kotlin Recycler Views"
From zen2
(Created page with "For recycler we will NOt have setOnItemClickListener as below: <pre> override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) s...") |
|||
| Line 12: | Line 12: | ||
} | } | ||
</pre> | </pre> | ||
| + | |||
| + | Instead: | ||
| + | |||
| + | We need to add the recycler support library: | ||
| + | *Open build.gradle (Module: app) | ||
| + | *In dependencies create a space after implementations, press alt-enter, add library dependency, then choose the one with recyclerview. This may ass it at the bottom, you can move it up. It should be something like this: | ||
| + | <pre> | ||
| + | dependencies { | ||
| + | implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
| + | implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" | ||
| + | implementation 'com.android.support:appcompat-v7:26.1.0' | ||
| + | implementation 'com.android.support.constraint:constraint-layout:1.0.2' | ||
| + | compile 'com.android.support:recyclerview-v7:27.0.1' | ||
| + | testImplementation 'junit:junit:4.12' | ||
| + | androidTestImplementation 'com.android.support.test:runner:1.0.1' | ||
| + | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' | ||
| + | } | ||
| + | </pre> | ||
| + | You will then need to Sync Now (top right) to resync gradle. | ||
Revision as of 02:18, 30 November 2017
For recycler we will NOt have setOnItemClickListener as below:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
adapter = CategoryAdapter(this, DataService.categories)
categoryListView.adapter = adapter
categoryListView.setOnItemClickListener { parent, view, position, id ->
val category = DataService.categories[position]
Toast.makeText(this, "You clicked on the ${category.title}", Toast.LENGTH_SHORT).show()
}
}
Instead:
We need to add the recycler support library:
- Open build.gradle (Module: app)
- In dependencies create a space after implementations, press alt-enter, add library dependency, then choose the one with recyclerview. This may ass it at the bottom, you can move it up. It should be something like this:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:recyclerview-v7:27.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
You will then need to Sync Now (top right) to resync gradle.
