Android - SwipeRefreshLayout with empty textview

后端 未结 15 1432
悲哀的现实
悲哀的现实 2020-12-05 02:06

I\'ve implemented SwipeRefreshLayout into my app but it can only hold one direct child which should be the listview. I\'m trying to figure out how to add an emp

15条回答
  •  星月不相逢
    2020-12-05 02:49

    This was a very frustrating issue for me but after a few hours with try and fail I came up with this solution.

    With this I can refresh even with empty view visible (and RecyclerView too of course)

    In my layout file I have this structure:

    SwipeRefreshLayout
        FrameLayout
            RecyclerView
            my_empty_layout // Doesnt have to be ScrollView it can be whatever ViewGroup you want, I used LinearLayout with a single TextView child
    

    In code:

    ...
    adapter.notifyDataSetChanged()
    
    if (adapter.getItemCount() == 0) {
        recyclerView.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
    }
    else {
        recyclerView.setVisibility(View.VISIBLE);
        emptyView.setVisibility(View.GONE);
    }
    

提交回复
热议问题