Skip to content

Instantly share code, notes, and snippets.

@har5hit
Last active April 24, 2019 17:53
Show Gist options
  • Save har5hit/a45ab6e8b31261d8cfc7444899a2e3bc to your computer and use it in GitHub Desktop.
Save har5hit/a45ab6e8b31261d8cfc7444899a2e3bc to your computer and use it in GitHub Desktop.
File template for simple listing screen with activity and recycler view. Add them in Settings -> File and Code Templates -> File
-----Activity-----
#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import android.arch.lifecycle.Observer
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
class ${NAME}Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setupView()
initData()
attachListeners()
loadData()
}
private fun loadData() {
}
lateinit var binding: ${BINDING}Binding
val listAdapter by lazy {
${RecyclerViewAdapter}()
}
private fun attachListeners() {
}
private fun handleList(dataResource: Resource<List<${MODEL}>>) {
when (dataResource.status) {
Status.SUCCESS -> {
showLoading(false)
dataResource.data?.let { it1 -> listAdapter.update(it1) }
}
Status.ERROR -> {
showLoading(false)
showError(dataResource.error)
}
Status.LOADING -> {
showLoading(true)
}
}
}
private fun showLoading(loading: Boolean) {
binding.progressbar.visibility=if (loading) View.VISIBLE else View.GONE
}
private fun initData() {
}
private fun setupView() {
binding = DataBindingUtil.setContentView(this, R.layout.${LAYOUT_ID})
binding.recyclerView.apply {
layoutManager = LinearLayoutManager(context)
adapter = listAdapter
}
}
}
------------RecyclerView------------------------
#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
class ${NAME}: RecyclerView.Adapter<${NAME}.MyViewHolder>() {
val list= mutableListOf<${MODEL}>();
fun update(list:List<${MODEL}>){
this.list.clear()
this.list.addAll(list)
notifyDataSetChanged()
}
inner class MyViewHolder(val binding: ${BINDING}Binding): RecyclerView.ViewHolder(binding.root),IViewHolder<$MODEL> {
override fun setData(data:${MODEL}) {
binding.${MODEL}=data
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ${NAME}.MyViewHolder
=
MyViewHolder(${BINDING}Binding.inflate(LayoutInflater.from(parent.context),parent,false))
override fun getItemCount() = list.size
override fun onBindViewHolder(holder: ${NAME}.MyViewHolder, position: Int) {
holder.setData(list[position])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment