Skip to content

Commit

Permalink
#51 convert RecyclerViewEmptySupport
Browse files Browse the repository at this point in the history
Signed-off-by: YOUNG HO CHA <[email protected]>
  • Loading branch information
ganadist committed Aug 6, 2017
1 parent 9004686 commit 89e3d12
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 72 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.avjindersinghsekhon.minimaltodo

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.View

class RecyclerViewEmptySupport @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyle: Int = 0):
RecyclerView(context, attrs, defStyle) {
private var emptyView: View? = null

private val observer = object : RecyclerView.AdapterDataObserver() {
override fun onChanged() {
showEmptyView()
}

override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
super.onItemRangeInserted(positionStart, itemCount)
onChanged()
}

override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
super.onItemRangeRemoved(positionStart, itemCount)
onChanged()
}
}

fun showEmptyView() {
val adapter = adapter
if (adapter != null && emptyView != null) {
if (adapter.itemCount == 0) {
emptyView!!.visibility = View.VISIBLE
visibility = View.GONE
} else {
emptyView!!.visibility = View.GONE
visibility = View.VISIBLE
}
}
}

override fun setAdapter(adapter: RecyclerView.Adapter<*>?) {
super.setAdapter(adapter)
adapter?.run {
registerAdapterDataObserver(observer)
observer.onChanged()
}
}

fun setEmptyView(v: View?) {
emptyView = v
showEmptyView()
}
}

0 comments on commit 89e3d12

Please sign in to comment.