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.util.AttributeSet;
21 import android.view.View;
22 
23 import androidx.recyclerview.widget.RecyclerView;
24 
25 /**
26  * Simple RecyclerView subclass that supports providing an empty view (which
27  * is displayed when the adapter has no data and hidden otherwise).
28  */
29 public class AttractionsRecyclerView extends RecyclerView {
30     private View mEmptyView;
31 
32     private AdapterDataObserver mDataObserver = new AdapterDataObserver() {
33         @Override
34         public void onChanged() {
35             super.onChanged();
36             updateEmptyView();
37         }
38     };
39 
AttractionsRecyclerView(Context context)40     public AttractionsRecyclerView(Context context) {
41         super(context);
42     }
43 
AttractionsRecyclerView(Context context, AttributeSet attrs)44     public AttractionsRecyclerView(Context context, AttributeSet attrs) {
45         super(context, attrs);
46     }
47 
AttractionsRecyclerView(Context context, AttributeSet attrs, int defStyle)48     public AttractionsRecyclerView(Context context, AttributeSet attrs, int defStyle) {
49         super(context, attrs, defStyle);
50     }
51 
52     /**
53      * Designate a view as the empty view. When the backing adapter has no
54      * data this view will be made visible and the recycler view hidden.
55      *
56      */
setEmptyView(View emptyView)57     public void setEmptyView(View emptyView) {
58         mEmptyView = emptyView;
59     }
60 
61     @Override
setAdapter(RecyclerView.Adapter adapter)62     public void setAdapter(RecyclerView.Adapter adapter) {
63         if (getAdapter() != null) {
64             getAdapter().unregisterAdapterDataObserver(mDataObserver);
65         }
66         if (adapter != null) {
67             adapter.registerAdapterDataObserver(mDataObserver);
68         }
69         super.setAdapter(adapter);
70         updateEmptyView();
71     }
72 
updateEmptyView()73     private void updateEmptyView() {
74         if (mEmptyView != null && getAdapter() != null) {
75             boolean showEmptyView = getAdapter().getItemCount() == 0;
76             mEmptyView.setVisibility(showEmptyView ? VISIBLE : GONE);
77             setVisibility(showEmptyView ? GONE : VISIBLE);
78         }
79     }
80 }
81