/** * Copyright 2016 JustWayward Team * <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.justwayward.reader.view; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.view.View; import com.justwayward.reader.utils.LogUtils; /** * 支持emptyView * * @author yuyh. * @date 16/6/10. */ public class SupportRecyclerView extends RecyclerView { private View emptyView; private AdapterDataObserver emptyObserver = new AdapterDataObserver() { @Override public void onChanged() { LogUtils.i("adapter changed"); Adapter adapter = getAdapter(); if (adapter != null && emptyView != null) { if (adapter.getItemCount() == 0) { LogUtils.i("adapter visible"); emptyView.setVisibility(View.VISIBLE); SupportRecyclerView.this.setVisibility(View.GONE); } else { LogUtils.i("adapter gone"); emptyView.setVisibility(View.GONE); SupportRecyclerView.this.setVisibility(View.VISIBLE); } } } }; public SupportRecyclerView(Context context) { super(context); } public SupportRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public SupportRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setAdapter(Adapter adapter) { Adapter oldAdapter = getAdapter(); if (oldAdapter != null && emptyObserver != null) { oldAdapter.unregisterAdapterDataObserver(emptyObserver); } super.setAdapter(adapter); if (adapter != null) { adapter.registerAdapterDataObserver(emptyObserver); } emptyObserver.onChanged(); } /** * set view when no content item * * @param emptyView visiable view when items is empty */ public void setEmptyView(View emptyView) { this.emptyView = emptyView; } }