java - 当 RecyclerView 为空时如何使 SwipeRefreshLayout 工作

标签 java android android-recyclerview swiperefreshlayout

我注意到 SwipeRefreshLayout 在我的 RecyclerView 充满数据时起作用,但在 RecyclerView 为空时不起作用。有没有办法覆盖这种行为?

我的应用从服务器获取一些数据并填充RecyclerView。例如,如果用户忘记打开互联网但启动了我的应用程序,我希望他们能够打开它并向上滑动以刷新,而不是返回并再次开始 Activity 。

这是我的 Activity 的 xml。我删除了一些代码以使其不那么冗长。我在 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>

这是 .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);
        }
    }

java 文件再次缺少一些与问题无关的代码。 GetMenuBase 扩展 AsyncTask 并实现 doInBackground() 并调用服务器并返回 JSON 输出。

最佳答案

问题是,当您的 RecyclerView 为空时,您的高度将为 0dp,因为您已将高度设置为 wrap_content0dp 到您的 SwipeRefreshLayout

SwipeRefreshLayoutRecyclerView 的高度更改为 match_parent

关于java - 当 RecyclerView 为空时如何使 SwipeRefreshLayout 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56619055/

相关文章:

java - 是否可以将项目拖到 RecyclerView 之外?

java - Ant - 在 build.xml 中的不同终端上运行多个服务器

java - 从 ByteBuffer 读取以 NUL 结尾的字符串

java - 无法将 Spring Boot 应用程序启动到 Linux 服务器

android - 如何在 Asmack 中处理临时异常

java - 如何在android上选择线程池的大小

java - Recyclerview 不会对大型数据集进行动画处理

java - 使用 Apache Axis2 和 Rampart 创建 Web 服务客户端

java - X 不活动量后如何锁定用户?

android - CoordinatorLayout 中的 RelativeLayout 不滚动