在我正在处理的应用中,我使用Recycler View来显示项目列表。我希望列表在列表为空时显示一些视图(例如,文本视图)。我知道使用ListViews,可以调用setEmptyView
,但RecyclerView没有这样的方法。
我尝试将视图的可见性设置为GONE,并在RecyclerView数据集为空时使其可见,但是将任何视图添加到定义了RecyclerView的布局文件会导致错误:
Attempt to invoke virtual method 'boolean
android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null
object reference
关于我在做什么的最好方法是什么?
有关详细信息,RecyclerView保存在Fragment中,布局在onCreateView函数中膨胀。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_grocerylist, container, false);
rv = view.findViewById(R.id.grocery_list);
dh = new DatabaseHandler(view.getContext());
dataset = dh.getGroceries();
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
recyclerView.setLayoutManager(new LinearLayoutManager(context));
adapter = new GroceryListRecyclerViewAdapter(dataset,mListener,this);
recyclerView.setAdapter(adapter);
}
ItemTouchHelper.Callback callback = new SimpleTouchHelperCallback(adapter);
ith = new ItemTouchHelper(callback);
ith.attachToRecyclerView(rv);
DividerItemDecoration div = new DividerItemDecoration(rv.getContext(),
DividerItemDecoration.VERTICAL);
rv.addItemDecoration(div);
return view;
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 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:id="@+id/grocery_list" android:name="com.github.jlcarveth.grocer.layout.fragment.GroceryListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layoutManager="LinearLayoutManager" tools:context="com.github.jlcarveth.grocer.layout.fragment.GroceryListFragment" tools:listitem="@layout/grocery_item"/>
答案 0 :(得分:1)
在布局中添加textview。与RecyclerView处于同一级别
<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或相应的回调检查中,查看为您的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);
}
答案 1 :(得分:1)
我更新了您的代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:name="com.github.jlcarveth.grocer.layout.fragment.GroceryListFragment"
android:layout_width="match_parent"
tools:context="com.github.jlcarveth.grocer.layout.fragment.GroceryListFragment"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/grocery_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/grocery_item" />
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="NO DATA AVAILABLE" />
</FrameLayout>
你的碎片:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_grocerylist, container, false);
rv = view.findViewById(R.id.grocery_list);
TextView emptyView = (TextView)view.findViewById(R.id.empty_view);
dh = new DatabaseHandler(view.getContext());
dataset = dh.getGroceries();
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) rv;
recyclerView.setLayoutManager(new LinearLayoutManager(context));
adapter = new GroceryListRecyclerViewAdapter(dataset,mListener,this);
recyclerView.setAdapter(adapter);
ItemTouchHelper.Callback callback = new SimpleTouchHelperCallback(adapter);
ith = new ItemTouchHelper(callback);
ith.attachToRecyclerView(recyclerView);
DividerItemDecoration div = new DividerItemDecoration(recyclerView.getContext(),
DividerItemDecoration.VERTICAL);
recyclerView.addItemDecoration(div);
if (dataset.isEmpty()){
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
} else {
recyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}
return view;
}
答案 2 :(得分:0)
为什么不选择相对布局。
<Relative layout>
<Recycler View/>
<Text View/>
</Relative layout
由于array
为空
RecyclerView.GONE
TextView.VISIBLE
如果array
有一些数据
RecyclerView.VISIBLE
TextView.GONE
就我而言,我只关注此事。
如果不起作用,我会发给你我的代码。
答案 3 :(得分:0)
当我遇到此问题时,在网上冲浪时发现有用的要旨https://gist.github.com/sheharyarn/5602930ad84fa64c30a29ab18eb69c6e。
此处显示了如何通过定义数据观察器以优雅的方式实现空视图。
答案 4 :(得分:0)
此答案适用于在其Android项目中使用Kotlin和数据绑定的开发人员。
custom_recyclerview_empty_support.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Recycler view -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Empty view -->
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/empty_text_view"
style="@style/TextAppearance.MaterialComponents.Body1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/white_light"
android:gravity="center"
android:textColor="@color/black_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Empty View" />
<!-- Retry view -->
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/retry_linear_layout_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/white_light"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/retry_text_view"
style="@style/TextAppearance.MaterialComponents.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/views_pad_horizontal"
android:layout_marginTop="@dimen/views_pad_vertical"
android:layout_marginEnd="@dimen/views_pad_horizontal"
android:layout_marginBottom="@dimen/views_pad_vertical"
android:gravity="center"
tools:text="Retry View" />
<com.google.android.material.button.MaterialButton
android:id="@+id/retry_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/retry_button" />
</androidx.appcompat.widget.LinearLayoutCompat>
<!-- Loading view -->
<FrameLayout
android:id="@+id/loading_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/white_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
我正在使用此Custom RecyclerView类的片段类
<app.ui.widgets.RecyclerViewEmptySupport
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
创建自定义RecyclerView类:
class RecyclerViewEmptySupport(context: Context, attrs: AttributeSet) :
ConstraintLayout(context, attrs) {
private val binding: CustomRecyclerviewEmptySupportBinding
val recyclerView: RecyclerView
init {
this.binding =
CustomRecyclerviewEmptySupportBinding.inflate(LayoutInflater.from(context), this, true)
this.recyclerView = binding.recyclerView
}
fun updateViews(
loading: Boolean = false,
retry: Boolean = false,
empty: Boolean = false,
success: Boolean = false
) {
binding.recyclerView.setVisibleOrGone(success)
binding.emptyTextView.setVisibleOrGone(empty)
binding.retryLinearLayoutView.setVisibleOrGone(retry)
binding.loadingView.setVisibleOrGone(loading)
}
// Empty view
fun showEmptyMessage(message: String) {
updateViews(empty = true)
binding.emptyTextView.text = message
}
// Retry view
fun showRetryView(message: String, callback: () -> Unit) {
updateViews(retry = true)
binding.retryTextView.text = message
binding.retryButton.setOnClickListener { callback() }
}
}
Fragment / Activity类可以管理View的可见性并为View设置数据,如下所示:
要调用api时,请调用此代码块
binding.myRecyclerView.updateViews(loading = true)
getDataFromRepository()
当api响应中的数据为空时,请调用
binding.myRecyclerView.showEmptyMessage("No Items Message")
如果api中存在数据,请调用
list.addAll(apiList)
adapter.notifyDataSetChanged()
binding.myRecyclerView.updateViews(success = true)
如果api响应中有错误,请调用
binding.myRecyclerView.showRetryView("Unable to get data at the moment.") { getDataFromRepository() }