Last active
November 27, 2018 13:13
-
-
Save txusballesteros/c0b5a6c6fb51fbb9b70a99ef8f890cce to your computer and use it in GitHub Desktop.
This file contains 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
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 | |
[email protected] = 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