Skip to content

Instantly share code, notes, and snippets.

@txusballesteros
Last active November 27, 2018 13:13
Show Gist options
  • Save txusballesteros/c0b5a6c6fb51fbb9b70a99ef8f890cce to your computer and use it in GitHub Desktop.
Save txusballesteros/c0b5a6c6fb51fbb9b70a99ef8f890cce to your computer and use it in GitHub Desktop.
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
fun RecyclerView.setEmptyView(view: View) {
adapter?.apply {
val observer = object: RecyclerView.AdapterDataObserver() {
override fun onChanged() {
view.visibility = if (adapter.isEmpty()) VISIBLE else GONE
this@setEmptyView.visibility = if (adapter.isEmpty()) GONE else VISIBLE
}
}
registerAdapterDataObserver(observer)
observer.onChanged()
}
}
fun <A: RecyclerView.ViewHolder> RecyclerView.Adapter<A>.isEmpty() = itemCount == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment