SwipeRefreshLayout can host only one direct child

Yevgeni picture Yevgeni · May 23, 2014 · Viewed 7.6k times · Source

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 view outside of the swipeRefresh and then add it as the emptyView it will work. So how do i so it with an outer xml file .. ?

The xml code:

        <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >


        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/inbox_vertical_margin" ></ListView>


</android.support.v4.widget.SwipeRefreshLayout>

onCreateView:

View rootView = inflater.inflate(R.layout.fragment_inbox, container,
        false);

View empty = inflater.inflate(R.layout.empty_message_list, container,
        false);
ListView mListView = (ListView) rootView
        .findViewById(android.R.id.list);
((ViewGroup) mListView.getParent()).addView(empty);
TextView textViewToChange = (TextView) empty
        .findViewById(R.id.welcomeMessage);

textViewToChange.setText("YO "
        + ParseUser.getCurrentUser().getString(
                ParseConstants.KEY_PRESENTING_USERNAME)
        + "!  bad news.. :(");
mListView.setEmptyView(empty);

mSwipeRefreshLayout = (SwipeRefreshLayout) rootView
        .findViewById(R.id.swipeRefreshLayout);
mSwipeRefreshLayout.setOnRefreshListener(mOnRefreshListener);
mSwipeRefreshLayout.setColorScheme(R.color.swipeRefresh1,
        R.color.swipeRefresh2, R.color.swipeRefresh3,
        R.color.swipeRefresh4);

return rootView;

Answer

LordRaydenMK picture LordRaydenMK · May 23, 2014

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.