Skip to content

Instantly share code, notes, and snippets.

@manoj-mili
Last active August 16, 2020 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-mili/5082cf26fc36ea538b48f8561504295c to your computer and use it in GitHub Desktop.
Save manoj-mili/5082cf26fc36ea538b48f8561504295c to your computer and use it in GitHub Desktop.
Initializing And Calling Our Adapter Class
package com.carnot.recyclerviewlistadapter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
lateinit var adapter: PostAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//We initialise the postList just and abstract source
//which can be anything(Network, DB call, File etc)
val postList = getListOfPost(5)
adapter = PostAdapter()
rvTask.adapter = adapter
updatePostAdapter(postList)
fbRefresh.setOnClickListener {
// Just and example of data refresh which gets a new list and passes it to adapter.
val updatedPostList = getListOfPost(10)
updatePostAdapter(updatedPostList)
}
}
fun updatePostAdapter(postList: List<Post>) {
//important method which does the work of updating the items in Adapter
adapter.submitList(postList)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment