SwipeRefreshLayout can host only one direct child

后端 未结 2 885
遇见更好的自我
遇见更好的自我 2021-01-17 11:09

I added a \"pull to refresh\" to my listView, i also wanted to add an empty view when the list is empty - Now i got this error. How can i make this work? if im positioning a

相关标签:
2条回答
  • 2021-01-17 11:25

    I needed to implement the same "pull to refresh" feature in my Apps. An alternative (and quite simple) way to implement the 'empty view' is to use the ListView's header or footer views.

    First you create an "empty_view.xml" containing the empty view layout. Then within the onCreate() method of your hosting activity, do something like:

    View headerView = LayoutInflater.from(this).inflate(R.layout.empty_view, myListView, false);
    myListView.addHeaderView(headerView);
    myListView.setAdapter(adapter);
    
    // populate your ListView here
    // ......
    
    // now check the number of items and remove the empty view if needed
    if (number_of_items > 1) {
      myListView.removeHeaderView(headerView);
    }
    adapter.notifyDataSetChanged();
    

    In the above code myListView is the ListView object and adapter is its adapter.

    This is useful in other situations too. For example, assume the list data is read from a web service and is slow to load, then one can put a ProgressBar is a view and set it as the ListView's header view so that user has some visual indication.

    0 讨论(0)
  • 2021-01-17 11:47

    SwipeRefreshLayout should be the parent in the XML file.

    You can use a FrameLayout as the child of SwipeRefreshLayout. The ListView and TextView (or any other empty state view) can be child views of the FrameLayout.

    0 讨论(0)
提交回复
热议问题