[英]How to make SwipeRefreshLayout work when the RecyclerView is empty
I noticed the SwipeRefreshLayout
works when my RecyclerView
is filled with data but not when the RecyclerView
is empty.我注意到SwipeRefreshLayout
在我的RecyclerView
充满数据时有效,但在RecyclerView
为空时无效。 Is there a way to override this behavior?有没有办法覆盖这种行为?
My app fetches some data from the server and populates the RecyclerView
.我的应用程序从服务器获取一些数据并填充RecyclerView
。 So for example, if the user has forgotten to turn on their internet but started my app, I want them to be able to turn it on and swipe up to refresh instead of going back and starting the activity again.因此,例如,如果用户忘记打开他们的互联网但启动了我的应用程序,我希望他们能够打开它并向上滑动以刷新而不是返回并再次启动活动。
Here's is my activity's xml.这是我的活动的 xml。 I have removed some code to make this less verbose.我删除了一些代码以减少冗长。 I had one more button outside of the SwipeRefreshLayout
and I have also removed my constraints.我在SwipeRefreshLayout
之外还有一个按钮,而且我还删除了我的约束。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".BreakfastActivity"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/b_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="0dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_breakfast"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>
And this is the .java file:这是 .java 文件:
public class BreakfastActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
// to display the menu
RecyclerView rv_breakfast;
RecyclerView.Adapter my_adapter;
// the actual menu
ArrayList<Menu> menu;
private SwipeRefreshLayout swipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_breakfast);
rv_breakfast = findViewById(R.id.rv_breakfast);
swipeRefreshLayout = findViewById(R.id.b_swipe_refresh_layout);
swipeRefreshLayout.setRefreshing(true);
swipeRefreshLayout.setOnRefreshListener(this);
rv_breakfast.setLayoutManager(new LinearLayoutManager(this));
new GetMenu(this).execute();
}
@Override
public void onRefresh() {
new GetMenu(this).execute();
}
static class GetMenu extends GetMenuBase {
WeakReference<BreakfastActivity> context;
GetMenu(BreakfastActivity b) {
context = new WeakReference<>(b);
meal_type = "breakfast";
b.swipeRefreshLayout.setRefreshing(true);
}
@Override
protected void onPostExecute(JSONObject parent) {
// process the output of the server side script script
b.swipeRefreshLayout.setRefreshing(false);
}
}
The java file is again devoid of some code not concerning the question. java文件再次没有一些与问题无关的代码。 GetMenuBase
extends an AsyncTask
and implements doInBackground()
and makes a call to the server and returns the JSON output. GetMenuBase
扩展AsyncTask
并实现doInBackground()
并调用服务器并返回 JSON 输出。
The problem is that when your RecyclerView
is empty then your height will be 0dp
because you've set the height to wrap_content
and 0dp
to your SwipeRefreshLayout
.问题是,当您的RecyclerView
为空时,您的高度将为0dp
因为您已将高度设置为wrap_content
并将0dp
为您的SwipeRefreshLayout
。
Change the height of your SwipeRefreshLayout
or your RecyclerView
to match_parent
.将SwipeRefreshLayout
或RecyclerView
的高度更改为match_parent
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.