BrowseSupportFragment
public class BrowseSupportFragment extends BaseSupportFragment
| java.lang.Object | ||||
| ↳ | androidx.fragment.app.Fragment | |||
| ↳ | androidx.leanback.app.BrandedSupportFragment | |||
| ↳ | androidx.leanback.app.BaseSupportFragment | |||
| ↳ | androidx.leanback.app.BrowseSupportFragment |
A fragment for creating Leanback browse screens. It is composed of a RowsSupportFragment and a HeadersSupportFragment.
A BrowseSupportFragment 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 HeadersSupportFragment can be set to be either shown or hidden by default, or may be disabled entirely. See setHeadersState for details.
By default the BrowseSupportFragment 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 with false and use BrowseSupportFragment.BrowseTransitionListener and startHeadersTransition.
The recommended theme to use with a BrowseSupportFragment is Theme_Leanback_Browse.
Summary
Nested types |
|---|
public class BrowseSupportFragment.BrowseTransitionListenerListener for transitions between browse headers and rows. |
public abstract class BrowseSupportFragment.FragmentFactory<T extends Fragment>Factory class responsible for creating fragment given the current item. |
public interface BrowseSupportFragment.FragmentHostPossible set of actions that |
public class BrowseSupportFragment.ListRowFragmentFactory extends BrowseSupportFragment.FragmentFactoryFragmentFactory implementation for |
public class BrowseSupportFragment.MainFragmentAdapter<T extends Fragment>Interface that defines the interaction between |
public interface BrowseSupportFragment.MainFragmentAdapterProviderInterface to be implemented by all fragments for providing an instance of |
public final class BrowseSupportFragment.MainFragmentAdapterRegistryRegistry class maintaining the mapping of |
public class BrowseSupportFragment.MainFragmentRowsAdapter<T extends Fragment>This is used to pass information to |
public interface BrowseSupportFragment.MainFragmentRowsAdapterProviderInterface to be implemented by |
Constants |
|
|---|---|
static final int |
HEADERS_DISABLED = 3The headers fragment is disabled and will never be shown. |
static final int |
HEADERS_ENABLED = 1The headers fragment is enabled and shown by default. |
static final int |
HEADERS_HIDDEN = 2The headers fragment is enabled and hidden by default. |
Public constructors |
|---|
Public methods |
|
|---|---|
static Bundle |
createArgs(Bundle args, String title, int headersState)Creates arguments for a browse fragment. |
void |
enableMainFragmentScaling(boolean enable)Enables scaling of main fragment when headers are present. |
void |
This method is deprecated. use |
ObjectAdapter |
Returns the adapter containing the rows for the fragment. |
@ColorInt int |
Returns the brand color for the browse fragment. |
int |
Returns the state of the headers column in the browse fragment. |
HeadersSupportFragment |
Get currently bound HeadersSupportFragment or null if HeadersSupportFragment has not been created yet. |
Fragment |
|
final BrowseSupportFragment.MainFragmentAdapterRegistry |
|
OnItemViewClickedListener |
Returns the item Clicked listener. |
OnItemViewSelectedListener |
Returns an item selection listener. |
RowsSupportFragment |
Get RowsSupportFragment if it's bound to BrowseSupportFragment or null if either BrowseSupportFragment has not been created yet or a different fragment is bound to it. |
int |
Gets position of currently selected row. |
RowPresenter.ViewHolder |
|
final boolean |
Returns true if headers transition on back key support is enabled. |
boolean |
Returns true if the headers transition is currently running. |
boolean |
Returns true if headers are shown. |
void |
Called to do initial creation of a fragment. |
HeadersSupportFragment |
Creates a new |
@Nullable View |
onCreateView(Called to have the fragment instantiate its user interface view. |
void |
Called when the fragment is no longer in use. |
void |
Called when the view previously created by |
void |
onSaveInstanceState(@NonNull Bundle outState)Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance if its process is restarted. |
void |
onStart()Called when the Fragment is visible to the user. |
void |
onStop()Called when the Fragment is no longer started. |
void |
setAdapter(ObjectAdapter adapter)Sets the adapter containing the rows for the fragment. |
void |
setBrandColor(@ColorInt int color)Sets the brand color for the browse fragment. |
void |
Sets a listener for browse fragment transitions. |
void |
setHeaderPresenterSelector(PresenterSelector headerPresenterSelector)Sets the |
void |
setHeadersState(int headersState)Sets the state for the headers column in the browse fragment. |
final void |
setHeadersTransitionOnBackEnabled(boolean headersBackStackEnabled)Enables/disables headers transition on back key support. |
void |
Sets an item clicked listener on the fragment. |
void |
Sets an item selection listener. |
void |
setSelectedPosition(int position)Sets the selected row position with smooth animation. |
void |
setSelectedPosition(int position, boolean smooth)Sets the selected row position. |
void |
setSelectedPosition(Selects a Row and perform an optional task on the Row. |
void |
startHeadersTransition(boolean withHeaders)Starts a headers transition. |
Protected methods |
|
|---|---|
Object |
Create entrance transition. |
void |
Callback when entrance transition is ended. |
void |
Callback when entrance transition is prepared. |
void |
Callback when entrance transition is started. |
void |
runEntranceTransition(Object entranceTransition)Run entrance transition. |
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Constants
HEADERS_DISABLED
public static final int HEADERS_DISABLED = 3
The headers fragment is disabled and will never be shown.
HEADERS_ENABLED
public static final int HEADERS_ENABLED = 1
The headers fragment is enabled and shown by default.
HEADERS_HIDDEN
public static final int HEADERS_HIDDEN = 2
The headers fragment is enabled and hidden by default.
Public constructors
Public methods
createArgs
public static Bundle createArgs(Bundle args, String title, int headersState)
Creates arguments for a browse fragment.
| Parameters | |
|---|---|
Bundle args |
The Bundle to place arguments into, or null if the method should return a new Bundle. |
String title |
The title of the BrowseSupportFragment. |
int headersState |
The initial state of the headers of the BrowseSupportFragment. Must be one of |
| Returns | |
|---|---|
Bundle |
A Bundle with the given arguments for creating a BrowseSupportFragment. |
enableMainFragmentScaling
public void enableMainFragmentScaling(boolean enable)
Enables scaling of main fragment when headers are present. For the page/row fragment, scaling is enabled only when both this method and isScalingEnabled are enabled.
| Parameters | |
|---|---|
boolean enable |
true to enable row scaling |
getAdapter
public ObjectAdapter getAdapter()
Returns the adapter containing the rows for the fragment.
getBrandColor
public @ColorInt int getBrandColor()
Returns the brand color for the browse fragment. The default is transparent.
getHeadersState
public int getHeadersState()
Returns the state of the headers column in the browse fragment.
getHeadersSupportFragment
public HeadersSupportFragment getHeadersSupportFragment()
Get currently bound HeadersSupportFragment or null if HeadersSupportFragment has not been created yet.
| Returns | |
|---|---|
HeadersSupportFragment |
Currently bound HeadersSupportFragment or null if HeadersSupportFragment has not been created yet. |
getMainFragment
public Fragment getMainFragment()
| Returns | |
|---|---|
Fragment |
Current main fragment or null if not created. |
getMainFragmentRegistry
public final BrowseSupportFragment.MainFragmentAdapterRegistry getMainFragmentRegistry()
getOnItemViewClickedListener
public OnItemViewClickedListener getOnItemViewClickedListener()
Returns the item Clicked listener.
getOnItemViewSelectedListener
public OnItemViewSelectedListener getOnItemViewSelectedListener()
Returns an item selection listener.
getRowsSupportFragment
public RowsSupportFragment getRowsSupportFragment()
Get RowsSupportFragment if it's bound to BrowseSupportFragment or null if either BrowseSupportFragment has not been created yet or a different fragment is bound to it.
| Returns | |
|---|---|
RowsSupportFragment |
RowsSupportFragment if it's bound to BrowseSupportFragment or null otherwise. |
getSelectedPosition
public int getSelectedPosition()
Gets position of currently selected row.
| Returns | |
|---|---|
int |
Position of currently selected row. |
getSelectedRowViewHolder
public RowPresenter.ViewHolder getSelectedRowViewHolder()
| Returns | |
|---|---|
RowPresenter.ViewHolder |
selected row ViewHolder inside fragment created by |
isHeadersTransitionOnBackEnabled
public final boolean isHeadersTransitionOnBackEnabled()
Returns true if headers transition on back key support is enabled.
isInHeadersTransition
public boolean isInHeadersTransition()
Returns true if the headers transition is currently running.
isShowingHeaders
public boolean isShowingHeaders()
Returns true if headers are shown.
onCreate
public void onCreate(Bundle savedInstanceState)
Called to do initial creation of a fragment. This is called after onAttach and before onCreateView.
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, add a androidx.lifecycle.LifecycleObserver on the activity's Lifecycle, removing it when it receives the CREATED callback.
Any restored child fragments will be created before the base Fragment.onCreate method returns.
| Parameters | |
|---|---|
Bundle savedInstanceState |
If the fragment is being re-created from a previous saved state, this is the state. |
onCreateHeadersSupportFragment
public HeadersSupportFragment onCreateHeadersSupportFragment()
Creates a new HeadersSupportFragment instance. Subclass of BrowseSupportFragment may override and return an instance of subclass of HeadersSupportFragment, e.g. when app wants to replace presenter to render HeaderItem.
| Returns | |
|---|---|
HeadersSupportFragment |
A new instance of |
onCreateView
public @Nullable View onCreateView(
@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState
)
Called to have the fragment instantiate its user interface view. This is optional, and non-graphical fragments can return null. This will be called between onCreate and onViewCreated.
A default View can be returned by calling Fragment in your constructor. Otherwise, this method returns null.
It is recommended to only inflate the layout in this method and move logic that operates on the returned View to onViewCreated.
If you return a View from here, you will later be called in onDestroyView when the view is being released.
| Parameters | |
|---|---|
@NonNull LayoutInflater inflater |
The LayoutInflater object that can be used to inflate any views in the fragment, |
@Nullable ViewGroup 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. |
@Nullable Bundle savedInstanceState |
If non-null, this fragment is being re-constructed from a previous saved state as given here. |
onDestroy
public void onDestroy()
Called when the fragment is no longer in use. This is called after onStop and before onDetach.
onDestroyView
public void onDestroyView()
Called when the view previously created by onCreateView has been detached from the fragment. The next time the fragment needs to be displayed, a new view will be created. This is called after onStop and before onDestroy. It is called regardless of whether onCreateView returned a non-null view. Internally it is called after the view's state has been saved but before it has been removed from its parent.
onSaveInstanceState
public void onSaveInstanceState(@NonNull Bundle outState)
Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance if its process is restarted. If a new instance of the fragment later needs to be created, the data you place in the Bundle here will be available in the Bundle given to onCreate, onCreateView, and onViewCreated.
This corresponds to Activity.onSaveInstanceState(Bundle) and most of the discussion there applies here as well. Note however: this method may be called at any time before onDestroy. There are many situations where a fragment may be mostly torn down (such as when placed on the back stack with no UI showing), but its state will not be saved until its owning activity actually needs to save its state.
onStart
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.
onStop
public void onStop()
Called when the Fragment is no longer started. This is generally tied to Activity.onStop of the containing Activity's lifecycle.
setAdapter
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 | |
|---|---|
ObjectAdapter adapter |
An ObjectAdapter for the browse rows. All items must derive from |
setBrandColor
public void setBrandColor(@ColorInt 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 | |
|---|---|
@ColorInt int color |
The color to use as the brand color of the fragment. |
setBrowseTransitionListener
public void setBrowseTransitionListener(
BrowseSupportFragment.BrowseTransitionListener listener
)
Sets a listener for browse fragment transitions.
| Parameters | |
|---|---|
BrowseSupportFragment.BrowseTransitionListener listener |
The listener to call when a browse headers transition begins or ends. |
setHeaderPresenterSelector
public void setHeaderPresenterSelector(PresenterSelector headerPresenterSelector)
Sets the PresenterSelector used to render the row headers.
| Parameters | |
|---|---|
PresenterSelector headerPresenterSelector |
The PresenterSelector that will determine the Presenter for each row header. |
setHeadersState
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 | |
|---|---|
int headersState |
The state of the headers for the browse fragment. |
setHeadersTransitionOnBackEnabled
public final void setHeadersTransitionOnBackEnabled(boolean headersBackStackEnabled)
Enables/disables headers transition on back key support. This is enabled by default. The BrowseSupportFragment 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 and BrowseTransitionListener in your own back stack handling.
setOnItemViewClickedListener
public void setOnItemViewClickedListener(OnItemViewClickedListener listener)
Sets an item clicked listener on the fragment. OnItemViewClickedListener will override View.OnClickListener that item presenter sets during onCreateViewHolder. So in general, developer should choose one of the listeners but not both.
setOnItemViewSelectedListener
public void setOnItemViewSelectedListener(OnItemViewSelectedListener listener)
Sets an item selection listener.
setSelectedPosition
public void setSelectedPosition(int position)
Sets the selected row position with smooth animation.
setSelectedPosition
public void setSelectedPosition(int position, boolean smooth)
Sets the selected row position.
setSelectedPosition
public void setSelectedPosition(
int rowPosition,
boolean smooth,
Presenter.ViewHolderTask rowHolderTask
)
Selects a Row and perform an optional task on the Row. For example setSelectedPosition(10, true, new ListRowPresenterSelectItemViewHolderTask(5)) scrolls to 11th row and selects 6th item on that row. The method will be ignored if RowsSupportFragment has not been created (i.e. before onCreateView).
| Parameters | |
|---|---|
int rowPosition |
Which row to select. |
boolean smooth |
True to scroll to the row, false for no animation. |
Presenter.ViewHolderTask rowHolderTask |
Optional task to perform on the Row. When the task is not null, headers fragment will be collapsed. |
startHeadersTransition
public void startHeadersTransition(boolean withHeaders)
Starts 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 | |
|---|---|
boolean withHeaders |
True if the headers should transition to being shown, false if the transition should result in headers being hidden. |
Protected methods
createEntranceTransition
protected Object createEntranceTransition()
Create entrance transition. Subclass can override to load transition from resource or construct manually. Typically app does not need to override the default transition that browse and details provides.
onEntranceTransitionEnd
protected void onEntranceTransitionEnd()
Callback when entrance transition is ended.
onEntranceTransitionPrepare
protected void onEntranceTransitionPrepare()
Callback when entrance transition is prepared. This is when fragment should stop user input and animations.
onEntranceTransitionStart
protected void onEntranceTransitionStart()
Callback when entrance transition is started. This is when fragment should stop processing layout.
runEntranceTransition
protected void runEntranceTransition(Object entranceTransition)
Run entrance transition. Subclass may use TransitionManager to perform go(Scene) or beginDelayedTransition(). App should not override the default implementation of browse and details fragment.