I'm targeting an android app to API 15 and minimum 8. So I use support library to manage fragments. I have a set of fragments that I use in several parts of the app.
Now, in an activity I have a ListView in the layout:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/listOfEvents"
android:layout_width="match_parent" android:layout_height="match_parent">
</ListView>
I would like to add a fragment of mine in the ListView header. I tried this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_open);
listOfEvents = (ListView) findViewById(R.id.listOfEvents);
Fragment fragment = new SortingStandardFragment();
getSupportFragmentManager()
.beginTransaction()
.add(fragment, null)
.commit();
View fragmentView = fragment.getView(); // problem: fragment is null!
listOfEvents.addHeaderView(fragmentView);
}
but i get an error since fragment.getView() returns null (api reference docs say that I have to put a GroupView Id in the add call, but where should I put the GroupView in the layout? Is there another way to hit the mark?