簡體   English   中英

當 RecyclerView 為空時如何使 SwipeRefreshLayout 工作

[英]How to make SwipeRefreshLayout work when the RecyclerView is empty

我注意到SwipeRefreshLayout在我的RecyclerView充滿數據時有效,但在RecyclerView為空時無效。 有沒有辦法覆蓋這種行為?

我的應用程序從服務器獲取一些數據並填充RecyclerView 因此,例如,如果用戶忘記打開他們的互聯網但啟動了我的應用程序,我希望他們能夠打開它並向上滑動以刷新而不是返回並再次啟動活動。

這是我的活動的 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_content並將0dp為您的SwipeRefreshLayout

SwipeRefreshLayoutRecyclerView的高度更改為match_parent

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM