Android APIs
public class

BrowseFragment

extends Fragment
java.lang.Object
   ↳ android.app.Fragment
     ↳ android.support.v17.leanback.app.BrowseFragment

Class Overview

A fragment for creating Leanback browse screens. It is composed of a RowsFragment and a HeadersFragment.

A BrowseFragment renders the elements of its ObjectAdapter as a set of rows in a vertical list. The elements in this adapter must be subclasses of Row.

The HeadersFragment can be set to be either shown or hidden by default, or may be disabled entirely. See setHeadersState(int) for details.

By default the BrowseFragment includes support for returning to the headers when the user presses Back. For Activities that customize onBackPressed(), you must disable this default Back key support by calling setHeadersTransitionOnBackEnabled(boolean) with false and use BrowseFragment.BrowseTransitionListener and startHeadersTransition(boolean).

Summary

Nested Classes
class BrowseFragment.BrowseTransitionListener Listener for transitions between browse headers and rows. 
Constants
int HEADERS_DISABLED The headers fragment is disabled and will never be shown.
int HEADERS_ENABLED The headers fragment is enabled and shown by default.
int HEADERS_HIDDEN The headers fragment is enabled and hidden by default.
[Expand]
Inherited Constants
From interface android.content.ComponentCallbacks2
Public Constructors
BrowseFragment()
Public Methods
static Bundle createArgs(Bundle args, String title, int headersState)
Create arguments for a browse fragment.
ObjectAdapter getAdapter()
Returns the adapter containing the rows for the fragment.
Drawable getBadgeDrawable()
Returns the badge drawable used in the fragment title.
int getBrandColor()
Returns the brand color for the browse fragment.
int getHeadersState()
Returns the state of the headers column in the browse fragment.
OnItemClickedListener getOnItemClickedListener()
Returns the item clicked listener.
int getSearchAffordanceColor()
Returns the color used to draw the search affordance.
String getTitle()
Returns the title for the browse fragment.
final boolean isHeadersTransitionOnBackEnabled()
Returns true if headers transition on back key support is enabled.
boolean isInHeadersTransition()
Returns true if the headers transition is currently running.
boolean isShowingHeaders()
Returns true if headers are shown.
void onCreate(Bundle savedInstanceState)
Called to do initial creation of a fragment.
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
Called to have the fragment instantiate its user interface view.
void onDestroy()
Called when the fragment is no longer in use.
void onStart()
Called when the Fragment is visible to the user.
void setAdapter(ObjectAdapter adapter)
Sets the adapter containing the rows for the fragment.
void setBadgeDrawable(Drawable drawable)
Sets the drawable displayed in the browse fragment title.
void setBrandColor(int color)
Sets the brand color for the browse fragment.
void setBrowseTransitionListener(BrowseFragment.BrowseTransitionListener listener)
Set a listener for browse fragment transitions.
void setHeaderPresenterSelector(PresenterSelector headerPresenterSelector)
Sets the PresenterSelector used to render the row headers.
void setHeadersState(int headersState)
Sets the state for the headers column in the browse fragment.
final void setHeadersTransitionOnBackEnabled(boolean headersBackStackEnabled)
Enable/disable headers transition on back key support.
void setOnItemClickedListener(OnItemClickedListener listener)
Sets an item clicked listener on the fragment.
void setOnItemSelectedListener(OnItemSelectedListener listener)
Sets an item selection listener.
void setOnSearchClickedListener(View.OnClickListener listener)
Sets a click listener for the search affordance.
void setSearchAffordanceColor(int color)
Sets the color used to draw the search affordance.
void setTitle(String title)
Sets a title for the browse fragment.
void startHeadersTransition(boolean withHeaders)
Start a headers transition.
[Expand]
Inherited Methods
From class android.app.Fragment
From class java.lang.Object
From interface android.content.ComponentCallbacks
From interface android.content.ComponentCallbacks2
From interface android.view.View.OnCreateContextMenuListener

Constants

public static final int HEADERS_DISABLED

The headers fragment is disabled and will never be shown.

Constant Value: 3 (0x00000003)

public static final int HEADERS_ENABLED

The headers fragment is enabled and shown by default.

Constant Value: 1 (0x00000001)

public static final int HEADERS_HIDDEN

The headers fragment is enabled and hidden by default.

Constant Value: 2 (0x00000002)

Public Constructors

public BrowseFragment ()

Public Methods

public static Bundle createArgs (Bundle args, String title, int headersState)

Create arguments for a browse fragment.

Parameters
args The Bundle to place arguments into, or null if the method should return a new Bundle.
title The title of the BrowseFragment.
headersState The initial state of the headers of the BrowseFragment. Must be one of HEADERS_ENABLED, HEADERS_HIDDEN, or HEADERS_DISABLED.
Returns
  • A Bundle with the given arguments for creating a BrowseFragment.

public ObjectAdapter getAdapter ()

Returns the adapter containing the rows for the fragment.

public Drawable getBadgeDrawable ()

Returns the badge drawable used in the fragment title.

public int getBrandColor ()

Returns the brand color for the browse fragment. The default is transparent.

public int getHeadersState ()

Returns the state of the headers column in the browse fragment.

public OnItemClickedListener getOnItemClickedListener ()

Returns the item clicked listener.

public int getSearchAffordanceColor ()

Returns the color used to draw the search affordance. Can be called only after an activity has been attached.

public String getTitle ()

Returns the title for the browse fragment.

public final boolean isHeadersTransitionOnBackEnabled ()

Returns true if headers transition on back key support is enabled.

public boolean isInHeadersTransition ()

Returns true if the headers transition is currently running.

public boolean isShowingHeaders ()

Returns true if headers are shown.

public void onCreate (Bundle savedInstanceState)

Called to do initial creation of a fragment. This is called after onAttach(Activity) and before onCreateView(LayoutInflater, ViewGroup, Bundle).

Note that this can be called while the fragment's activity is still in the process of being created. As such, you can not rely on things like the activity's content view hierarchy being initialized at this point. If you want to do work once the activity itself is created, see onActivityCreated(Bundle).

Parameters
savedInstanceState If the fragment is being re-created from a previous saved state, this is the state.

public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

Called to have the fragment instantiate its user interface view. This is optional, and non-graphical fragments can return null (which is the default implementation). This will be called between onCreate(Bundle) and onActivityCreated(Bundle).

If you return a View from here, you will later be called in onDestroyView() when the view is being released.

Parameters
inflater The LayoutInflater object that can be used to inflate any views in the fragment,
container If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view.
savedInstanceState If non-null, this fragment is being re-constructed from a previous saved state as given here.
Returns
  • Return the View for the fragment's UI, or null.

public void onDestroy ()

Called when the fragment is no longer in use. This is called after onStop() and before onDetach().

public void onStart ()

Called when the Fragment is visible to the user. This is generally tied to Activity.onStart of the containing Activity's lifecycle.

public void setAdapter (ObjectAdapter adapter)

Sets the adapter containing the rows for the fragment.

The items referenced by the adapter must be be derived from Row. These rows will be used by the rows fragment and the headers fragment (if not disabled) to render the browse rows.

Parameters
adapter An ObjectAdapter for the browse rows. All items must derive from Row.

public void setBadgeDrawable (Drawable drawable)

Sets the drawable displayed in the browse fragment title.

Parameters
drawable The Drawable to display in the browse fragment title.

public void setBrandColor (int color)

Sets the brand color for the browse fragment. The brand color is used as the primary color for UI elements in the browse fragment. For example, the background color of the headers fragment uses the brand color.

Parameters
color The color to use as the brand color of the fragment.

public void setBrowseTransitionListener (BrowseFragment.BrowseTransitionListener listener)

Set a listener for browse fragment transitions.

Parameters
listener The listener to call when a browse headers transition begins or ends.

public void setHeaderPresenterSelector (PresenterSelector headerPresenterSelector)

Sets the PresenterSelector used to render the row headers.

Parameters
headerPresenterSelector The PresenterSelector that will determine the Presenter for each row header.

public void setHeadersState (int headersState)

Sets the state for the headers column in the browse fragment. Must be one of HEADERS_ENABLED, HEADERS_HIDDEN, or HEADERS_DISABLED.

Parameters
headersState The state of the headers for the browse fragment.

public final void setHeadersTransitionOnBackEnabled (boolean headersBackStackEnabled)

Enable/disable headers transition on back key support. This is enabled by default. The BrowseFragment will add a back stack entry when headers are showing. Running a headers transition when the back key is pressed only works when the headers state is HEADERS_ENABLED or HEADERS_HIDDEN.

NOTE: If an Activity has its own onBackPressed() handling, you must disable this feature. You may use startHeadersTransition(boolean) and BrowseFragment.BrowseTransitionListener in your own back stack handling.

public void setOnItemClickedListener (OnItemClickedListener listener)

Sets an item clicked listener on the fragment.

OnItemClickedListener will override View.OnClickListener that an item presenter may set during onCreateViewHolder(ViewGroup). So in general, you should choose to use an OnItemClickedListener or a View.OnClickListener on your item views, but not both.

Parameters
listener The listener to call when an item is clicked.

public void setOnItemSelectedListener (OnItemSelectedListener listener)

Sets an item selection listener. This listener will be called when an item or row is selected by a user.

Parameters
listener The listener to call when an item or row is selected.

public void setOnSearchClickedListener (View.OnClickListener listener)

Sets a click listener for the search affordance.

The presence of a listener will change the visibility of the search affordance in the fragment title. When set to non-null, the title will contain an element that a user may click to begin a search.

The listener's onClick method will be invoked when the user clicks on the search element.

Parameters
listener The listener to call when the search element is clicked.

public void setSearchAffordanceColor (int color)

Sets the color used to draw the search affordance.

Parameters
color The color to use for the search affordance.

public void setTitle (String title)

Sets a title for the browse fragment.

Parameters
title The title of the browse fragment.

public void startHeadersTransition (boolean withHeaders)

Start a headers transition.

This method will begin a transition to either show or hide the headers, depending on the value of withHeaders. If headers are disabled for this browse fragment, this method will throw an exception.

Parameters
withHeaders True if the headers should transition to being shown, false if the transition should result in headers being hidden.