如何用RecyclerView显示一个空视图?

我用来在布局文件中放置一个特殊的视图, 如ListActivity文档中所描述的ListActivity当没有数据时将被显示 。 这个视图有id "android:id/empty"

 <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_data" /> 

我想知道新的RecyclerView如何做到这一点?

RecyclerView所在的同一个布局上,添加TextView

 <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" /> <TextView android:id="@+id/empty_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:visibility="gone" android:text="@string/no_data_available" /> 

onCreate或相应的callback函数中,检查提供RecyclerView的数据集是否为空。 如果数据集是空的, RecyclerView也是空的。 在这种情况下,消息出现在屏幕上。 如果不是,请更改其可见性:

 private RecyclerView recyclerView; private TextView emptyView; // ... recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view); emptyView = (TextView) rootView.findViewById(R.id.empty_view); // ... if (dataset.isEmpty()) { recyclerView.setVisibility(View.GONE); emptyView.setVisibility(View.VISIBLE); } else { recyclerView.setVisibility(View.VISIBLE); emptyView.setVisibility(View.GONE); } 

对于我的项目,我做了这个解决scheme( RecyclerViewsetEmptyView方法):

 public class RecyclerViewEmptySupport extends RecyclerView { private View emptyView; private AdapterDataObserver emptyObserver = new AdapterDataObserver() { @Override public void onChanged() { Adapter<?> adapter = getAdapter(); if(adapter != null && emptyView != null) { if(adapter.getItemCount() == 0) { emptyView.setVisibility(View.VISIBLE); RecyclerViewEmptySupport.this.setVisibility(View.GONE); } else { emptyView.setVisibility(View.GONE); RecyclerViewEmptySupport.this.setVisibility(View.VISIBLE); } } } }; public RecyclerViewEmptySupport(Context context) { super(context); } public RecyclerViewEmptySupport(Context context, AttributeSet attrs) { super(context, attrs); } public RecyclerViewEmptySupport(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setAdapter(Adapter adapter) { super.setAdapter(adapter); if(adapter != null) { adapter.registerAdapterDataObserver(emptyObserver); } emptyObserver.onChanged(); } public void setEmptyView(View emptyView) { this.emptyView = emptyView; } } 

你应该用它来代替RecyclerView类:

 <com.maff.utils.RecyclerViewEmptySupport android:id="@+id/list1" android:layout_height="match_parent" android:layout_width="match_parent" /> <TextView android:id="@+id/list_empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Empty" /> 

 RecyclerViewEmptySupport list = (RecyclerViewEmptySupport)rootView.findViewById(R.id.list1); list.setLayoutManager(new LinearLayoutManager(context)); list.setEmptyView(rootView.findViewById(R.id.list_empty)); 

这是一个解决scheme,只使用不同视图types的自定义适配器用于空闲情况。

 public class EventAdapter extends RecyclerView.Adapter<EventAdapter.ViewHolder> { private static final int VIEW_TYPE_EVENT = 0; private static final int VIEW_TYPE_DATE = 1; private static final int VIEW_TYPE_EMPTY = 2; private ArrayList items; public EventAdapter(ArrayList items) { this.items = items; } @Override public int getItemCount() { if(items.size() == 0){ return 1; }else { return items.size(); } } @Override public int getItemViewType(int position) { if (items.size() == 0) { return VIEW_TYPE_EMPTY; }else{ Object item = items.get(position); if (item instanceof Event) { return VIEW_TYPE_EVENT; } else { return VIEW_TYPE_DATE; } } } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v; ViewHolder vh; if (viewType == VIEW_TYPE_EVENT) { v = LayoutInflater.from(parent.getContext()).inflate( R.layout.item_event, parent, false); vh = new ViewHolderEvent(v); } else if (viewType == VIEW_TYPE_DATE) { v = LayoutInflater.from(parent.getContext()).inflate( R.layout.item_event_date, parent, false); vh = new ViewHolderDate(v); } else { v = LayoutInflater.from(parent.getContext()).inflate( R.layout.item_event_empty, parent, false); vh = new ViewHolder(v); } return vh; } @Override public void onBindViewHolder(EventAdapter.ViewHolder viewHolder, final int position) { int viewType = getItemViewType(position); if (viewType == VIEW_TYPE_EVENT) { //... } else if (viewType == VIEW_TYPE_DATE) { //... } else if (viewType == VIEW_TYPE_EMPTY) { //... } } public static class ViewHolder extends ParentViewHolder { public ViewHolder(View v) { super(v); } } public static class ViewHolderDate extends ViewHolder { public ViewHolderDate(View v) { super(v); } } public static class ViewHolderEvent extends ViewHolder { public ViewHolderEvent(View v) { super(v); } } } 

我使用ViewSwitcher

 <ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/switcher" > <android.support.v7.widget.RecyclerView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/text_empty" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/list_empty" android:gravity="center" /> </ViewSwitcher> 

在代码中,您将检查游标/数据集并切换视图。

 void showItems(Cursor items) { if (items.size() > 0) { mAdapter.switchCursor(items); if (R.id.list == mListSwitcher.getNextView().getId()) { mListSwitcher.showNext(); } } else if (R.id.text_empty == mListSwitcher.getNextView().getId()) { mListSwitcher.showNext(); } } 

你也可以设置animation,如果你想用几行代码

 mListSwitcher.setInAnimation(slide_in_left); mListSwitcher.setOutAnimation(slide_out_right); 

在您的适配器的getItemViewType检查适配器是否有0个元素,如果是,返回一个不同的viewType。

然后在你的onCreateViewHolder检查viewType是否是你之前返回的,并且膨胀了一个不同的视图。 在这种情况下,使用该TextView的布局文件

编辑

如果这仍然不起作用,那么你可能想像这样以编程方式设置视图的大小:

 Point size = new Point(); ((WindowManager)itemView.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getSize(size); 

然后当你膨胀你的视图呼叫:

 inflatedView.getLayoutParams().height = size.y; inflatedView.getLayoutParams().width = size.x; 

RVEmptyObserver

扩展AdapterDataObserver不是使用自定义的RecyclerViewAdapterDataObserver一个更简单的解决scheme,它允许设置列表中没有项目时显示的自定义View

用法示例:

 RVEmptyObserver observer = new RVEmptyObserver(recyclerView, emptyView) rvAdapter.registerAdapterDataObserver(observer); 

类:

 public class RVEmptyObserver extends RecyclerView.AdapterDataObserver { private View emptyView; private RecyclerView recyclerView; public RVEmptyObserver(RecyclerView rv, View ev) { this.recyclerView = rv; this.emptyView = ev; checkIfEmpty(); } private void checkIfEmpty() { if (emptyView != null && recyclerView.getAdapter() != null) { boolean emptyViewVisible = recyclerView.getAdapter().getItemCount() == 0; emptyView.setVisibility(emptyViewVisible ? View.VISIBLE : View.GONE); recyclerView.setVisibility(emptyViewVisible ? View.GONE : View.VISIBLE); } } public void onChanged() { checkIfEmpty(); } public void onItemRangeInserted(int positionStart, int itemCount) { checkIfEmpty(); } public void onItemRangeRemoved(int positionStart, int itemCount) { checkIfEmpty(); } } 

这里是我的类显示空视图,重试视图(加载API失败时)和加载进度为RecyclerView

 public class RecyclerViewEmptyRetryGroup extends RelativeLayout { private RecyclerView mRecyclerView; private LinearLayout mEmptyView; private LinearLayout mRetryView; private ProgressBar mProgressBar; private OnRetryClick mOnRetryClick; public RecyclerViewEmptyRetryGroup(Context context) { this(context, null); } public RecyclerViewEmptyRetryGroup(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RecyclerViewEmptyRetryGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void onViewAdded(View child) { super.onViewAdded(child); if (child.getId() == R.id.recyclerView) { mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); return; } if (child.getId() == R.id.layout_empty) { mEmptyView = (LinearLayout) findViewById(R.id.layout_empty); return; } if (child.getId() == R.id.layout_retry) { mRetryView = (LinearLayout) findViewById(R.id.layout_retry); mRetryView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mRetryView.setVisibility(View.GONE); mOnRetryClick.onRetry(); } }); return; } if (child.getId() == R.id.progress_bar) { mProgressBar = (ProgressBar) findViewById(R.id.progress_bar); } } public void loading() { mRetryView.setVisibility(View.GONE); mEmptyView.setVisibility(View.GONE); mProgressBar.setVisibility(View.VISIBLE); } public void empty() { mEmptyView.setVisibility(View.VISIBLE); mRetryView.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE); } public void retry() { mRetryView.setVisibility(View.VISIBLE); mEmptyView.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE); } public void success() { mRetryView.setVisibility(View.GONE); mEmptyView.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE); } public RecyclerView getRecyclerView() { return mRecyclerView; } public void setOnRetryClick(OnRetryClick onRetryClick) { mOnRetryClick = onRetryClick; } public interface OnRetryClick { void onRetry(); } } 

activity_xml

 <...RecyclerViewEmptyRetryGroup android:id="@+id/recyclerViewEmptyRetryGroup"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView"/> <LinearLayout android:id="@+id/layout_empty"> ... </LinearLayout> <LinearLayout android:id="@+id/layout_retry"> ... </LinearLayout> <ProgressBar android:id="@+id/progress_bar"/> </...RecyclerViewEmptyRetryGroup> 

在这里输入图像说明

源代码在这里https://github.com/PhanVanLinh/AndroidRecyclerViewWithLoadingEmptyAndRetry

我添加了RecyclerView和替代ImageViewRelativeLayout

 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/no_active_jobs" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_active_jobs" /> <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> 

然后在Adapter

 @Override public int getItemCount() { if (mOrders.size() == 0) { mRecyclerView.setVisibility(View.INVISIBLE); } else { mRecyclerView.setVisibility(View.VISIBLE); } return mOrders.size(); } 

只要incase你正在与FirebaseRecyclerAdapter这篇文章作为一个魅力工程https://stackoverflow.com/a/39058636/6507009