Skip to content

Commit

Permalink
Improved EmptyViewHelper.
Browse files Browse the repository at this point in the history
- Static creator methods (constructors are now private).
- On going animations of emptyViews are now cancelled before changing again alpha value (fixes consecutive fast switching).
  • Loading branch information
davideas committed Apr 22, 2018
1 parent d570f68 commit cdc9f78
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,10 @@ private void initializeRecyclerView() {
}

// New empty views handling, to set after FastScroller
mAdapter.addListener(new EmptyViewHelper(mAdapter,
EmptyViewHelper.create(mAdapter,
getView().findViewById(R.id.empty_view),
getView().findViewById(R.id.filter_view),
(EmptyViewHelper.OnEmptyViewListener) getActivity()) // Optional!!
);
(EmptyViewHelper.OnEmptyViewListener) getActivity()); // Optional!!

// Settings for FlipView
FlipView.stopLayoutAnimation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void initializeRecyclerView(Bundle savedInstanceState) {
mAdapter.setFastScroller(fastScroller);

// New empty views handling, to set after FastScroller
new EmptyViewHelper(mAdapter,
EmptyViewHelper.create(mAdapter,
getView().findViewById(R.id.empty_view),
getView().findViewById(R.id.filter_view));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ protected void initializeFab() {
mFab.setImageResource(R.drawable.ic_refresh_white_24dp);
}

@SuppressWarnings({"ConstantConditions", "NullableProblems"})
private void initializeRecyclerView(Bundle savedInstanceState) {
// Initialize Adapter and RecyclerView
// ExampleAdapter makes use of stableIds, I strongly suggest to implement 'item.hashCode()'
Expand Down Expand Up @@ -116,7 +115,7 @@ private void initializeRecyclerView(Bundle savedInstanceState) {
mAdapter.setFastScroller(fastScroller);

// New empty views handling, to set after FastScroller
new EmptyViewHelper(mAdapter,
EmptyViewHelper.create(mAdapter,
getView().findViewById(R.id.empty_view),
getView().findViewById(R.id.filter_view));

Expand Down Expand Up @@ -211,7 +210,7 @@ public void run() {
} else {
DatabaseService.getInstance().addAll(newItems);
}
mAdapter.onLoadMoreComplete(newItems, 3000L);
mAdapter.onLoadMoreComplete(newItems, (newItems.isEmpty() ? -1 : 3000L));
// - Retrieve the new page number after adding new items!
Log.d(TAG, "EndlessCurrentPage=" + mAdapter.getEndlessCurrentPage());
Log.d(TAG, "EndlessPageSize=" + mAdapter.getEndlessPageSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void initializeRecyclerView(Bundle savedInstanceState) {
mAdapter.setFastScroller(fastScroller);

// New empty views handling, to set after FastScroller
new EmptyViewHelper(mAdapter,
EmptyViewHelper.create(mAdapter,
getView().findViewById(R.id.empty_view),
getView().findViewById(R.id.filter_view));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void initializeRecyclerView(Bundle savedInstanceState) {
mAdapter.setFastScroller(fastScroller);

// New empty views handling, to set after FastScroller
new EmptyViewHelper(mAdapter,
EmptyViewHelper.create(mAdapter,
getView().findViewById(R.id.empty_view),
getView().findViewById(R.id.filter_view));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.davidea.samples.flexibleadapter.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
Expand Down Expand Up @@ -79,6 +80,12 @@ public void onActivityCreated(Bundle savedInstanceState) {
FlipView.stopLayoutAnimation();
}

@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
// Restoring selection is done in MainActivity for all samples (occurs after this callback).
}

@SuppressWarnings({"ConstantConditions", "NullableProblems"})
private void initializeRecyclerView(Bundle savedInstanceState) {
// Initialize Adapter and RecyclerView
Expand Down Expand Up @@ -106,7 +113,7 @@ private void initializeRecyclerView(Bundle savedInstanceState) {
mAdapter.setFastScroller(fastScroller);

// New empty views handling, to set after FastScroller
new EmptyViewHelper(mAdapter,
EmptyViewHelper.create(mAdapter,
getView().findViewById(R.id.empty_view),
getView().findViewById(R.id.filter_view),
(EmptyViewHelper.OnEmptyViewListener) getActivity()); // Optional!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private void initializeRecyclerView() {
.withSectionGapOffset(24));

// New empty views handling
new EmptyViewHelper(mAdapter, getView().findViewById(R.id.empty_view));
EmptyViewHelper.create(mAdapter, getView().findViewById(R.id.empty_view));

mAdapter.setDisplayHeadersAtStartUp(true) //Show Headers at startUp!
.setStickyHeaders(true) //Make headers sticky
Expand Down
1 change: 0 additions & 1 deletion flexible-adapter-app/src/main/res/layout/empty_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
android:layout_width="164dp"
android:layout_height="164dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="-16dp"
android:alpha="0.2"
android:scaleType="fitCenter"
android:src="@drawable/ic_xml_grey600_48dp"/>
Expand Down
8 changes: 4 additions & 4 deletions flexible-adapter-app/version.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Sat Oct 21 01:03:41 CEST 2017
#Sun Apr 22 22:37:13 CEST 2018
MAJOR=5
MINOR=0
PATCH=0
PRE_RELEASE=rc3
BUILD=5199
CODE=21
PRE_RELEASE=
BUILD=5530
CODE=23
2 changes: 1 addition & 1 deletion flexible-adapter-ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'eu.davidea.grabver'
versioning {
major 1
minor 0
preRelease "b3"
preRelease "b4"
dependsOn "install"
}
// For maven repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewPropertyAnimator;

import eu.davidea.fastscroller.FastScroller;
import eu.davidea.flexibleadapter.FlexibleAdapter;
Expand All @@ -31,6 +32,7 @@
*
* @author Davide Steduto
* @since 26/12/2017 Created
* <br>22/04/2018 Added static creator methods
*/
@SuppressWarnings("WeakerAccess")
public class EmptyViewHelper implements FlexibleAdapter.OnUpdateListener, FlexibleAdapter.OnFilterListener {
Expand All @@ -40,6 +42,19 @@ public class EmptyViewHelper implements FlexibleAdapter.OnUpdateListener, Flexib
private View mEmptyDataView;
private View mEmptyFilterView;

public static EmptyViewHelper create(FlexibleAdapter adapter, View emptyDataView) {
return new EmptyViewHelper(adapter, emptyDataView);
}

public static EmptyViewHelper create(FlexibleAdapter adapter, View emptyDataView, View emptyFilterView) {
return new EmptyViewHelper(adapter, emptyDataView, emptyFilterView);
}

public static EmptyViewHelper create(FlexibleAdapter adapter, View emptyDataView, View emptyFilterView,
@Nullable OnEmptyViewListener emptyViewListener) {
return new EmptyViewHelper(adapter, emptyDataView, emptyFilterView, emptyViewListener);
}

/**
* Constructor to initialize both empty data view.
* <p><b>Note:</b> To better handle FastScroller, initialize this helper class after setting fast scroller
Expand All @@ -48,7 +63,7 @@ public class EmptyViewHelper implements FlexibleAdapter.OnUpdateListener, Flexib
* @param adapter the Adapter instance
* @param emptyDataView the view for empty updates
*/
public EmptyViewHelper(FlexibleAdapter adapter, View emptyDataView) {
private EmptyViewHelper(FlexibleAdapter adapter, View emptyDataView) {
this(adapter, emptyDataView, null);
}

Expand All @@ -61,7 +76,7 @@ public EmptyViewHelper(FlexibleAdapter adapter, View emptyDataView) {
* @param emptyDataView the view for empty updates
* @param emptyFilterView the view for empty filter result
*/
public EmptyViewHelper(FlexibleAdapter adapter, View emptyDataView, View emptyFilterView) {
private EmptyViewHelper(FlexibleAdapter adapter, View emptyDataView, View emptyFilterView) {
this(adapter, emptyDataView, emptyFilterView, null);
}

Expand All @@ -76,7 +91,7 @@ public EmptyViewHelper(FlexibleAdapter adapter, View emptyDataView, View emptyFi
* @param emptyFilterView the view for empty filter result
* @param emptyViewListener another level of listener callback in case more customization is needed
*/
public EmptyViewHelper(FlexibleAdapter adapter, View emptyDataView, View emptyFilterView,
private EmptyViewHelper(FlexibleAdapter adapter, View emptyDataView, View emptyFilterView,
@Nullable OnEmptyViewListener emptyViewListener) {
this.mEmptyDataView = emptyDataView;
this.mEmptyFilterView = emptyFilterView;
Expand All @@ -97,36 +112,28 @@ public View getEmptyFilterView() {
* Shows EmptyDataView by animating alpha property to {@code 1}.
*/
public final void showEmptyDataView() {
if (mEmptyDataView != null) {
mEmptyDataView.animate().alpha(1);
}
showView(mEmptyDataView);
}

/**
* Hides EmptyDataView by setting Alpha property to {@code 0}.</p>
*/
public final void hideEmptyDataView() {
if (mEmptyDataView != null) {
mEmptyDataView.setAlpha(0);
}
hideView(mEmptyDataView);
}

/**
* Shows EmptyFilterView by animating alpha property to {@code 1}.
*/
public final void showEmptyFilterView() {
if (mEmptyFilterView != null) {
mEmptyFilterView.animate().alpha(1);
}
showView(mEmptyFilterView);
}

/**
* Hides EmptyFilterView by setting Alpha property to {@code 0}.</p>
*/
public final void hideEmptyFilterView() {
if (mEmptyFilterView != null) {
mEmptyFilterView.setAlpha(0);
}
hideView(mEmptyFilterView);
}

@Override
Expand Down Expand Up @@ -161,6 +168,22 @@ public final void onUpdateFilterView(int size) {
}
}

private static void showView(View view) {
if (view != null) {
ViewPropertyAnimator animator = view.animate();
animator.cancel();
animator.alpha(1);
}
}

public static void hideView(View view) {
if (view != null) {
ViewPropertyAnimator animator = view.animate();
animator.cancel();
animator.alpha(0);
}
}

/**
* @since 26/12/2017
*/
Expand Down
8 changes: 4 additions & 4 deletions flexible-adapter-ui/version.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Sat Oct 21 01:03:40 CEST 2017
#Sun Apr 22 22:11:07 CEST 2018
MAJOR=1
MINOR=0
PATCH=0
PRE_RELEASE=b1
BUILD=16
CODE=0
PRE_RELEASE=b4
BUILD=53
CODE=3

0 comments on commit cdc9f78

Please sign in to comment.