Skip to content

Instantly share code, notes, and snippets.

@passiondroid
Last active July 30, 2018 17:07
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 passiondroid/a2d0c4d4d723e2d95912400cd46477ea to your computer and use it in GitHub Desktop.
Save passiondroid/a2d0c4d4d723e2d95912400cd46477ea to your computer and use it in GitHub Desktop.
Sample Activity showing the use of ListAdapter
class MainActivity : AppCompatActivity() {
private val adapter = SampleListAdapter()
private var list: ArrayList<Item>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recyclerview.adapter = adapter
updateAdapter()
addBtn.setOnClickListener {
val newList = ArrayList<Item>()
newList.addAll(list!!)
val random = Random().nextInt(200)
val item = Item(random, "New item added ","")
newList.add(1,item)
adapter.submitList(newList)
}
removeBtn.setOnClickListener {
val newList = ArrayList<Item>()
newList.addAll(list!!)
adapter.submitList(newList)
}
}
private fun updateAdapter() {
list = ArrayList()
for(i in 0..9) {
val item = Item(i, "Item $i",
"")
list?.add(item)
}
progressBar.visibility = View.GONE
adapter.submitList(list)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment