-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#51 convert RecyclerViewEmptySupport
Signed-off-by: YOUNG HO CHA <[email protected]>
- Loading branch information
Showing
2 changed files
with
54 additions
and
72 deletions.
There are no files selected for viewing
72 changes: 0 additions & 72 deletions
72
app/src/main/java/com/example/avjindersinghsekhon/minimaltodo/RecyclerViewEmptySupport.java
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/example/avjindersinghsekhon/minimaltodo/RecyclerViewEmptySupport.kt
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
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() | ||
} | ||
} |