我习惯于在布局文件中放置一个特殊的视图,如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使用LiveData
ViewModel.getInstance().getCustomers?.observe(viewLifecycleOwner, {customerList ->
//assign your adapter with your list then
listAdapter?.notifyDataSetChanged()
isListItemEmpty(customerList?.isEmpty())
}
创建一个接口来在适配器和片段之间通信,并在适配器中注册它
interface EmptyListener {
fun isListEmpty(isEmpty: Boolean)
}
然后在publishResults中调用它:
dataList?.isEmpty()?.let { mListener?.isListEmpty(it) }
最后在Fragment中实现你的接口:
override fun isListEmpty(isEmpty: Boolean) {
if (isEmpty) {
your_list?.visibility = View.GONE
empty_view?.visibility = View.VISIBLE
} else {
empty_view?.visibility = View.GONE
your_list?.visibility = View.VISIBLE
}
}
这是如何显示空视图,同时过滤和更新RecyclerView使用LiveData
ViewModel.getInstance().getCustomers?.observe(viewLifecycleOwner, {customerList ->
//assign your adapter with your list then
listAdapter?.notifyDataSetChanged()
isListItemEmpty(customerList?.isEmpty())
}
创建一个接口来在适配器和片段之间通信,并在适配器中注册它
interface EmptyListener {
fun isListEmpty(isEmpty: Boolean)
}
然后在publishResults中调用它:
dataList?.isEmpty()?.let { mListener?.isListEmpty(it) }
最后在Fragment中实现你的接口:
override fun isListEmpty(isEmpty: Boolean) {
if (isEmpty) {
your_list?.visibility = View.GONE
empty_view?.visibility = View.VISIBLE
} else {
empty_view?.visibility = View.GONE
your_list?.visibility = View.VISIBLE
}
}
下面是一种解决方案,它仅使用一个自定义适配器,为空情况使用不同的视图类型。
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();
}
}
如果你愿意,你也可以用几行代码设置动画
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;