Last active
July 30, 2018 17:07
-
-
Save passiondroid/a2d0c4d4d723e2d95912400cd46477ea to your computer and use it in GitHub Desktop.
Sample Activity showing the use of ListAdapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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