From 89e3d124f5382eeed7a7e593a9bf215a8f68d585 Mon Sep 17 00:00:00 2001 From: YOUNG HO CHA Date: Sat, 5 Aug 2017 22:51:16 +0900 Subject: [PATCH] #51 convert RecyclerViewEmptySupport Signed-off-by: YOUNG HO CHA --- .../minimaltodo/RecyclerViewEmptySupport.java | 72 ------------------- .../minimaltodo/RecyclerViewEmptySupport.kt | 54 ++++++++++++++ 2 files changed, 54 insertions(+), 72 deletions(-) delete mode 100644 app/src/main/java/com/example/avjindersinghsekhon/minimaltodo/RecyclerViewEmptySupport.java create mode 100644 app/src/main/java/com/example/avjindersinghsekhon/minimaltodo/RecyclerViewEmptySupport.kt diff --git a/app/src/main/java/com/example/avjindersinghsekhon/minimaltodo/RecyclerViewEmptySupport.java b/app/src/main/java/com/example/avjindersinghsekhon/minimaltodo/RecyclerViewEmptySupport.java deleted file mode 100644 index 1b760a8..0000000 --- a/app/src/main/java/com/example/avjindersinghsekhon/minimaltodo/RecyclerViewEmptySupport.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.example.avjindersinghsekhon.minimaltodo; - -import android.content.Context; -import android.support.v7.widget.RecyclerView; -import android.util.AttributeSet; -import android.view.View; - -public class RecyclerViewEmptySupport extends RecyclerView { - private View emptyView; - - private AdapterDataObserver observer = new AdapterDataObserver() { - @Override - public void onChanged() { - showEmptyView(); - } - - @Override - public void onItemRangeInserted(int positionStart, int itemCount) { - super.onItemRangeInserted(positionStart, itemCount); - showEmptyView(); - } - - @Override - public void onItemRangeRemoved(int positionStart, int itemCount) { - super.onItemRangeRemoved(positionStart, itemCount); - showEmptyView(); - } - }; - - - - public RecyclerViewEmptySupport(Context context) { - super(context); - } - - public void showEmptyView(){ - - Adapter adapter = getAdapter(); - if(adapter!=null && emptyView!=null){ - if(adapter.getItemCount()==0){ - emptyView.setVisibility(VISIBLE); - RecyclerViewEmptySupport.this.setVisibility(GONE); - } - else{ - emptyView.setVisibility(GONE); - RecyclerViewEmptySupport.this.setVisibility(VISIBLE); - } - } - - } - - public RecyclerViewEmptySupport(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public RecyclerViewEmptySupport(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } - - @Override - public void setAdapter(Adapter adapter) { - super.setAdapter(adapter); - if(adapter!=null){ - adapter.registerAdapterDataObserver(observer); - observer.onChanged(); - } - } - - public void setEmptyView(View v){ - emptyView = v; - } -} diff --git a/app/src/main/java/com/example/avjindersinghsekhon/minimaltodo/RecyclerViewEmptySupport.kt b/app/src/main/java/com/example/avjindersinghsekhon/minimaltodo/RecyclerViewEmptySupport.kt new file mode 100644 index 0000000..92b6c6a --- /dev/null +++ b/app/src/main/java/com/example/avjindersinghsekhon/minimaltodo/RecyclerViewEmptySupport.kt @@ -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() + } +} \ No newline at end of file