-
Notifications
You must be signed in to change notification settings - Fork 33
Displaying Custom Fragments
Sometimes, an array of RowsFragment is still not custom enough - what if you want to create a truly custom fragment and yet allow it to be an item in a BrowseFragment?
Sofa allows this behaviour by relaxing the strict hierarchical requirements of BrowseFragment and by providing a simple interface to implement on your Fragments to get this functionality.
1. Start with your BrowseFragment
You can either create a new instance (i.e., new BrowseFragment()) or make your Fragment extend from BrowseFragment. Just make sure you're using the com.sgottard.sofa.BrowseFragment class, and not Leanback's!
2. Create an ArrayObjectAdapter
This is the adapter that will be set to the BrowseFragment.
ArrayObjectAdapter adapter = new ArrayObjectAdapter();
3. Make your CustomFragment class implement Sofa's ContentFragment
For a good example of this, see DemoFocusFragment. Here's the quick way if you just want to get up and running:
@Override
public boolean isScrolling() {
return false;
}
@Override
public View getFocusRootView() {
return getView();
}
@Override
public void setExtraMargin(int marginTop, int marginLeft) {
}
4. Create another ArrayObjectAdapter which will contain your CustomFragment
ArrayObjectAdapter customFragmentAdapter = new ArrayObjectAdapter();
customFragmentAdapter.add(new CustomFragment());
ListRow customFragmentRow = new ListRow(new HeaderItem("Custom Fragment Title"), customFragmentAdapter);
adapter.add(customFragmentRow);
This way, your BrowseFragment's adapter will contain a ListRow with an adapter that has one child: your CustomFragment.
5. Set the adapter to BrowseFragment
Finally, set the ArrayObjectAdapter as the adapter for BrowseFragment.
browseFragment.setAdapter(adapter);
That's it! If you want to add more custom Fragments, just repeat all the steps and add them to the ArrayObjectAdapter container.
TODO...
TODO...