From 07c967cce7ed5f5d5b0f5ea104afb1bfb0b00e12 Mon Sep 17 00:00:00 2001 From: ramanaptr Date: Mon, 28 Jun 2021 01:55:18 +0800 Subject: [PATCH] Improve alternative for implement start shimmer --- .../java/com/ramanaptr/sample/MainActivity.kt | 12 ++++++-- .../com/ramanaptr/widget/EzRecyclerView.java | 28 ++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/ramanaptr/sample/MainActivity.kt b/app/src/main/java/com/ramanaptr/sample/MainActivity.kt index 281617d..d6cef06 100644 --- a/app/src/main/java/com/ramanaptr/sample/MainActivity.kt +++ b/app/src/main/java/com/ramanaptr/sample/MainActivity.kt @@ -107,6 +107,9 @@ class MainActivity : AppCompatActivity() { // if you want use view binding you should to cast the object like the example below rvSample = binding.rvSample as EzRecyclerView + // set empty object like example "SampleData" when you use for shimmer effect, to avoid exception + rvSample.setData(SampleData()) + // set example function for pagination on Ez-RecyclerView // init the pagination after bind the view and declare it into field initPaginationEzRecyclerView() @@ -133,6 +136,9 @@ class MainActivity : AppCompatActivity() { // if you want use view binding you should to cast the object like the example below rvSample = binding.rvSample as EzRecyclerView + // set empty object like example "SampleData" when you use for shimmer effect, to avoid exception + rvSample.setData(SampleData()) + // example function for pagination on Ez-RecyclerView // init the pagination after bind the view and declare it into field initPaginationEzRecyclerView() @@ -178,7 +184,8 @@ class MainActivity : AppCompatActivity() { private fun exampleDataForSingleLayout(size: Int) { // start shimmer when load the data // please to use your empty object like SampleData() - rvSample.startShimmer(size, SampleData()) + // rvSample.startShimmer(size, SampleData()) // Alternative + rvSample.startShimmer(size) // start load the data subscribe = Flowable.create>({ @@ -215,7 +222,8 @@ class MainActivity : AppCompatActivity() { private fun exampleDataForMultipleLayout(size: Int) { // start shimmer when load the data // please to use your empty object like SampleData() - rvSample.startShimmer(size, SampleData()) + // rvSample.startShimmer(size, SampleData()) // Alternative + rvSample.startShimmer(size) // start load the data subscribe = Flowable.create>({ diff --git a/ez-recyleview/src/main/java/com/ramanaptr/widget/EzRecyclerView.java b/ez-recyleview/src/main/java/com/ramanaptr/widget/EzRecyclerView.java index ec5cdda..f9cc10b 100644 --- a/ez-recyleview/src/main/java/com/ramanaptr/widget/EzRecyclerView.java +++ b/ez-recyleview/src/main/java/com/ramanaptr/widget/EzRecyclerView.java @@ -44,6 +44,7 @@ public class EzRecyclerView extends RecyclerView { private int offset = 0; private int limit = 0; private int currentPage = 0; + private Data data; public EzRecyclerView(@NonNull Context context) { super(context); @@ -76,6 +77,7 @@ public void destroy() { ezPaginationListener = null; baseAdapter = null; listener = null; + data = null; tempShimmerSize = 0; startShimmerSize = 0; endShimmerSize = 0; @@ -84,6 +86,10 @@ public void destroy() { currentPage = 0; } + public void setData(Data data) { + this.data = data; + } + public void setViewHolderLayout(@NonNull Listener listener) { this.listener = listener; baseAdapter.setListener(listener::setDataOnViewHolder); @@ -140,12 +146,32 @@ public void setFlexBoxLayoutManager(@FlexDirection int flexDirection) { setLayoutManager(flexboxLayoutManager); } + public void startShimmer(int shimmerSize) { + if (data == null) { + throw new IllegalArgumentException("Please construct your empty object extended from EzBaseData into the function #setData(data) or you can use #startShimmer(size, data)"); + } + if (isFirstLoadEzRecyclerView) { + this.startShimmerSize = baseAdapter.getItemCount(); + this.endShimmerSize = shimmerSize; + this.tempShimmerSize = shimmerSize; + final List shimmer = new ArrayList<>(); + for (int i = 0; i < shimmerSize; i++) { + data.setEzViewType(EzViewType.SHIMMER_EFFECT); + shimmer.add(data); + } + addAll(shimmer); + flagEzRecyclerViewFirstLoadDone(); + flagOnLoading(); + } + } + public void startShimmer(int shimmerSize, Data data) { if (isFirstLoadEzRecyclerView) { + this.data = data; this.startShimmerSize = baseAdapter.getItemCount(); this.endShimmerSize = shimmerSize; this.tempShimmerSize = shimmerSize; - List shimmer = new ArrayList<>(); + final List shimmer = new ArrayList<>(); for (int i = 0; i < shimmerSize; i++) { data.setEzViewType(EzViewType.SHIMMER_EFFECT); shimmer.add(data);