我正在尝试在我的RecyclerView Adapter
上实现一个EmptyView
,但是我没有得到任何结果。
我已经按照这个tutorial和这个tip的步骤进行了操作,但是都没有起作用。
我已经实现了:
if (viewType == EMPTY_VIEW) {
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_view, parent, false);
EmptyViewHolder evh = new EmptyViewHolder(v);
return evh;
}
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.data_row, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
但是它不让我编译,因为它们是不同的ViewHolder
,因为我创建了两个ViewHolder
类,但它们都extends Recycler.ViewHolder
,所以我不明白...
我试图这样做是因为我有一个SearchView
,当列表为空时,我希望它显示一个EmptyView
,我已经通过编程实现了它,但我更喜欢添加一个像layout
这样的东西,因为我不太知道如何在程序中放置TextViews
和Buttons
。
另外,如果我加上
return dataList.size() > 0 ? dataList.size() : 1;
由于索引为0,它给我报错。
我已经调试了viewType
并且总是为1,因此它不会进入if
条件...
在Android
中深入研究后,我发现了这个:
/**
* Return the view type of the item at <code>position</code> for the purposes
* of view recycling.
*
* <p>The default implementation of this method returns 0, making the assumption of
* a single view type for the adapter. Unlike ListView adapters, types need not
* be contiguous. Consider using id resources to uniquely identify item view types.
*
* @param position position to query
* @return integer value identifying the type of the view needed to represent the item at
* <code>position</code>. Type codes need not be contiguous.
*/
public int getItemViewType(int position) {
return 0;
}
但问题是没有改变值。
编辑
我几乎完成了,我做了这个:
@Override
public int getItemViewType(int position) {
return list.size() > 0 ? list.size() : 1;
}
但有时当 size() 为 0 时它会返回 0……我不明白,我正在使用 SearchView,有时当我输入一个与列表中任何项都不匹配的字母时,它不显示,有时它会显示……
另外还有一件事发生了,就是当 layout
弹出时,它显示在屏幕的左边,但我把它设置为居中,我想这可能是 RecyclerView
的问题,因为 layout
放在其中。
RecyclerView 的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/rtpew"
android:layout_centerInParent="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearpew">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
这是我的emptylayout
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ImageViewSearchFail"
android:src="@drawable/sadface"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_gravity="center"
android:textSize="@dimen/15dp"
android:layout_marginTop="4dp"
android:text="foo"
android:layout_below="@+id/ImageViewSearchFail"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ButtonAddEntity"
android:text="foo"
android:background="?android:selectableItemBackground"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
我想到的另一种方法是按照以下方式进行编程实现:
@Override
public boolean onQueryTextChange(String query) {
final ArrayList<List> filteredModelList = filter(mModel, query);
mAdapter.animateTo(filteredModelList);
rv.scrollToPosition(0);
if(query.isEmpty()){
//Here
}
return true;
}
并且:
private ArrayList<List> filter(ArrayList<List> models, String query) {
query = query.toLowerCase();
final ArrayList<List> filteredModelList = new ArrayList<List>();
for (List model : models) {
final String text = model.getRedName().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
if (filteredModelList.size()<0) {
//HERE
}
else{
//Delete the views added
}
return filteredModelList;
}
问题
-我只是使用@Jimeux answer添加了视图,但我想在Adapter
中实现此功能,我做到了,但即使list
为空,有时仍然不会显示view
。
-在放置emptyview.xml
时,它会放在RecyclerView
内部,因此自从我把所有这些xml
放在中心位置后,它就会显示在右侧。我尝试通过编程方式添加xml
,但就像混乱一样....