简体   繁体   中英

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. Is there a way to override this behavior?

My app fetches some data from the server and populates the 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. 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.

<?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:

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. GetMenuBase extends an AsyncTask and implements doInBackground() and makes a call to the server and returns the JSON output.

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 .

Change the height of your SwipeRefreshLayout or your RecyclerView to match_parent .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM