1 /*
2  * Copyright 2015 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.xyztouristattractions.ui;
18 
19 import android.content.Context;
20 import android.support.v7.widget.RecyclerView;
21 import android.util.AttributeSet;
22 import android.view.View;
23 
24 /**
25  * Simple RecyclerView subclass that supports providing an empty view (which
26  * is displayed when the adapter has no data and hidden otherwise).
27  */
28 public class AttractionsRecyclerView extends RecyclerView {
29     private View mEmptyView;
30 
31     private AdapterDataObserver mDataObserver = new AdapterDataObserver() {
32         @Override
33         public void onChanged() {
34             super.onChanged();
35             updateEmptyView();
36         }
37     };
38 
AttractionsRecyclerView(Context context)39     public AttractionsRecyclerView(Context context) {
40         super(context);
41     }
42 
AttractionsRecyclerView(Context context, AttributeSet attrs)43     public AttractionsRecyclerView(Context context, AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
AttractionsRecyclerView(Context context, AttributeSet attrs, int defStyle)47     public AttractionsRecyclerView(Context context, AttributeSet attrs, int defStyle) {
48         super(context, attrs, defStyle);
49     }
50 
51     /**
52      * Designate a view as the empty view. When the backing adapter has no
53      * data this view will be made visible and the recycler view hidden.
54      *
55      */
setEmptyView(View emptyView)56     public void setEmptyView(View emptyView) {
57         mEmptyView = emptyView;
58     }
59 
60     @Override
setAdapter(RecyclerView.Adapter adapter)61     public void setAdapter(RecyclerView.Adapter adapter) {
62         if (getAdapter() != null) {
63             getAdapter().unregisterAdapterDataObserver(mDataObserver);
64         }
65         if (adapter != null) {
66             adapter.registerAdapterDataObserver(mDataObserver);
67         }
68         super.setAdapter(adapter);
69         updateEmptyView();
70     }
71 
updateEmptyView()72     private void updateEmptyView() {
73         if (mEmptyView != null && getAdapter() != null) {
74             boolean showEmptyView = getAdapter().getItemCount() == 0;
75             mEmptyView.setVisibility(showEmptyView ? VISIBLE : GONE);
76             setVisibility(showEmptyView ? GONE : VISIBLE);
77         }
78     }
79 }
80