Skip to content

5.x | EmptyViewHelper

Davide Steduto edited this page Jun 3, 2018 · 8 revisions

In this page


Introduction

This Helper class is very simple: User provides pre-configured empty views for data and filter results, the helper will fade in/out the views using alpha animator in accordance with the Adapter callbacks. Usage:

Class location 👓

eu.davidea.flexibleadapter.helpers
  |_ EmptyViewHelper

Include your custom layout after RecyclerView widget:

<android.support.v7.widget.RecyclerView
    .../>

<!-- EmptyView layout for RV when empty -->
<include layout="@layout/empty_view"/>

<!-- EmptyView layout for RV when filter has empty result -->
<include layout="@layout/filter_view"/>

ℹ️ Note: Empty and Filter views should start with android:alpha="0.0".

Examples: empty_view.xml, filter_view.xml

Usage (with fragments)

// Minimum usage
EmptyViewHelper.create(mAdapter, getView().findViewById(R.id.empty_view));

// Extended usage
EmptyViewHelper.create(mAdapter,
        getView().findViewById(R.id.empty_view),
        getView().findViewById(R.id.filter_view),
        // Optional in case of further customization, see below!
        (EmptyViewHelper.OnEmptyViewListener) getActivity());

⚠️Note: Empty views handling, must be added after FastScroller settings to better handle it.

Callbacks

Two new callbacks are provided for optional customization, for instance: user wants to display something else.

/**
 * IMPLEMENT OnEmptyViewListener interface ONLY IN CASE OF EXTRA CUSTOMIZATION.
 */
public class MainActivity extends AppCompatActivity
        implements EmptyViewHelper.OnEmptyViewListener {

    @Override
    public void onUpdateEmptyDataView(int size) {
        String message = "Refreshed " + size + " items in " + mAdapter.getTime() + "ms";
        Snackbar.make(findViewById(R.id.main_view), message, Snackbar.LENGTH_SHORT)
                .show();
    }

    @Override
    public void onUpdateEmptyFilterView(int size) {
        String message = "Filtered " + size + " items in " + mAdapter.getTime() + "ms";
        Snackbar.make(findViewById(R.id.main_view), message, Snackbar.LENGTH_SHORT)
                .show();
    }
}
Clone this wiki locally