在RecyclerView顶部设置进度条,并在加载数据后删除 [英] Set progress bar on top of RecyclerView and remove after data is loaded
问题描述
我希望有一个加载图标显示在RecyclerView的顶部,并在数据加载完成后消失
I want to have a loading icon that is displayed on top of where a RecyclerView would be, and disappear once the data is finished loading
它看起来像:
有人可以帮我吗?
我有代码,在RecyclerView上方显示了一个TextView,上面显示正在加载...",并且在加载数据后消失,但是RecyclerView仍然可见.
I have the code which shows a TextView above the RecyclerView that says "Loading..." and disappears after the data is loaded, but the RecyclerView is still visible.
我的XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loaderTV"
android:text="Loading..."
android:layout_above="@+id/eventListRV"/>
<android.support.v7.widget.RecyclerView
android:scrollbars="vertical"
android:id="@+id/eventListRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/spinner">
</android.support.v7.widget.RecyclerView>
然后在我的代码中
client.listCreators(request, new Callback<ServiceResponse<Event>>() {
@Override
public void success(ServiceResponse<Event> eventServiceResponse, Response response) {
eventList.addAll(eventServiceResponse.data.results);
Log.i("tag", String.valueOf(eventServiceResponse.data.total));
if (eventList.size() > 60) {
adapter.notifyDataSetChanged();
loadingText.setVisibility(View.GONE);
}
}
但是我希望在加载数据时看不到RecyclerView,并且我希望RecyclerView所在位置的进度条,并且我不希望它成为textview
But I want the RecyclerView to be invisible while the data is loading and I want the progress bar ontop of where the RecyclerView is, and I don't want it to be a textview
推荐答案
- 将RecyclerView和加载布局(或TextView)都包装在FrameLayout中.然后将两者的宽度和高度都设置为
match_parent
. - 将RecyclerView隐藏在活动的onCreate()或片段的onCreateView()中:
recyclerView.setVisibility(View.GONE);
- 在回调方法中,使用
setVisibility(View.GONE)
隐藏加载布局,使用setVisibility(View.VISIBLE)
显示RecyclerView并使用adapter.notifyDataSetChanged()
触发适配器的重新加载.
- Wrap both your RecyclerView and your loading layout (or TextView) inside a FrameLayout. Set both of then to
match_parent
for width and height. - Hide the RecyclerView in the onCreate() of your activity or onCreateView() of your fragment:
recyclerView.setVisibility(View.GONE);
- In your callback method hide the loading layout with
setVisibility(View.GONE)
, show your RecyclerView withsetVisibility(View.VISIBLE)
and trigger a reload of the adapter withadapter.notifyDataSetChanged()
这篇关于在RecyclerView顶部设置进度条,并在加载数据后删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!