/* * Copyright (C) 2015 Arles. All rights reserved. * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.arles.swissmanager.utils; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.view.View; /** * Simple RecyclerView subclass that supports providing an empty view (which * is displayed when the adapter has no data and hidden otherwise). * Created by Admin on 20.08.2015. */ public class ExtendedRecyclerView extends RecyclerView{ private View mEmptyView; private AdapterDataObserver mDataObserver = new AdapterDataObserver() { @Override public void onChanged() { super.onChanged(); updateEmptyView(); } }; public ExtendedRecyclerView(Context context) { super(context); } public ExtendedRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public ExtendedRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * Designate a view as the empty view. When the backing adapter has no * data this view will be made visible and the recycler view hidden. * */ public void setEmptyView(View emptyView) { mEmptyView = emptyView; } @Override public void setAdapter(RecyclerView.Adapter adapter) { if (getAdapter() != null) { getAdapter().unregisterAdapterDataObserver(mDataObserver); } if (adapter != null) { adapter.registerAdapterDataObserver(mDataObserver); } super.setAdapter(adapter); updateEmptyView(); } private void updateEmptyView() { if (mEmptyView != null && getAdapter() != null) { boolean showEmptyView = getAdapter().getItemCount() == 0; mEmptyView.setVisibility(showEmptyView ? VISIBLE : GONE); setVisibility(showEmptyView ? GONE : VISIBLE); } } }