Skip to content

Commit

Permalink
Improve alternative for implement start shimmer
Browse files Browse the repository at this point in the history
  • Loading branch information
ramanaptr committed Jun 27, 2021
1 parent 38ca02d commit 07c967c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
12 changes: 10 additions & 2 deletions app/src/main/java/com/ramanaptr/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<SampleData>

// 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()
Expand All @@ -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<SampleData>

// 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()
Expand Down Expand Up @@ -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<List<SampleData>>({
Expand Down Expand Up @@ -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<List<SampleData>>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class EzRecyclerView<Data extends EzBaseData> extends RecyclerView {
private int offset = 0;
private int limit = 0;
private int currentPage = 0;
private Data data;

public EzRecyclerView(@NonNull Context context) {
super(context);
Expand Down Expand Up @@ -76,6 +77,7 @@ public void destroy() {
ezPaginationListener = null;
baseAdapter = null;
listener = null;
data = null;
tempShimmerSize = 0;
startShimmerSize = 0;
endShimmerSize = 0;
Expand All @@ -84,6 +86,10 @@ public void destroy() {
currentPage = 0;
}

public void setData(Data data) {
this.data = data;
}

public void setViewHolderLayout(@NonNull Listener<Data> listener) {
this.listener = listener;
baseAdapter.setListener(listener::setDataOnViewHolder);
Expand Down Expand Up @@ -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<Data> 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<Data> shimmer = new ArrayList<>();
final List<Data> shimmer = new ArrayList<>();
for (int i = 0; i < shimmerSize; i++) {
data.setEzViewType(EzViewType.SHIMMER_EFFECT);
shimmer.add(data);
Expand Down

0 comments on commit 07c967c

Please sign in to comment.