SliceView
@RequiresApi(value = 19)
class SliceView : ViewGroup, Observer, View.OnClickListener
A view for displaying Slices.
A slice is a piece of app content and actions that can be surfaced outside of the app it originates from. SliceView is able to interpret the structure and contents of a slice and display it. This structure is defined by the app providing the slice when the slice is constructed with a androidx.slice.builders.TemplateSliceBuilder.
SliceView is able to display slices in a couple of different modes via {@see #setMode}.
- Small: The small format has a restricted height and display top-level information and actions from the slice.
- Large: The large format displays as much of the slice as it can based on the space provided for SliceView, if the slice overflows the space SliceView will scroll the content if scrolling has been enabled on SliceView, {@see #setScrollable}.
- Shortcut: A shortcut shows minimal information and is presented as a tappable icon representing the main content or action associated with the slice.
Slices can contain dynamic content that may update due to user interaction or a change in the data being displayed in the slice. SliceView can be configured to listen for these updates easily using SliceLiveData. Example usage:
SliceView v = new SliceView(getContext()); v.setMode(desiredMode); LiveDataliveData = SliceLiveData.fromUri(sliceUri); liveData.observe(lifecycleOwner, v);
SliceView supports various style options, see SliceView Attributes.
| See also | |
|---|---|
Slice |
|
SliceLiveData |
Summary
Nested types |
|---|
interface SliceView.OnSliceActionListenerImplement this interface to be notified of interactions with the slice displayed in this view. |
Constants |
|
|---|---|
const Int |
MODE_LARGE = 2Mode indicating this slice should be presented in large format, as much or all of the slice contents are shown. |
const Int |
MODE_SHORTCUT = 3Mode indicating this slice should be presented as a tappable icon. |
const Int |
MODE_SMALL = 1Mode indicating this slice should be presented in small format, only top-level information and actions from the slice are shown. |
Public constructors |
|---|
SliceView(context: Context!, attrs: AttributeSet?) |
SliceView(context: Context!, attrs: AttributeSet?, defStyleAttr: Int) |
@RequiresApi(value = 21) |
Public functions |
|
|---|---|
Int |
Returns the number of slice items not displayed in this view. |
Int |
getMode() |
Slice? |
getSlice() |
(Mutable)List<SliceAction!>? |
Returns the slice actions presented in this view. |
Boolean |
Whether this view allow scrollable content when presenting in |
Unit |
Called when the data is changed to |
Unit |
|
Boolean |
|
Boolean |
onTouchEvent(ev: MotionEvent!) |
Unit |
setAccentColor(accentColor: @ColorInt Int)Contents of a slice such as icons, text, and controls (e.g. toggle) can be tinted. |
Unit |
Set the mode this view should present in. |
Unit |
setOnClickListener(listener: View.OnClickListener!) |
Unit |
setOnLongClickListener(listener: View.OnLongClickListener!) |
Unit |
Sets the listener to notify when an interaction event occurs on the view. |
Unit |
setRowStyleFactory(rowStyleFactory: RowStyleFactory?)Sets the |
Unit |
setScrollable(isScrollable: Boolean)Set whether this view should allow scrollable content when presenting in |
Unit |
setShowActionDividers(enabled: Boolean)Whether this view should show action dividers for rows. |
Unit |
setShowHeaderDivider(enabled: Boolean)Whether this view should show the header divider. |
Unit |
setShowTitleItems(enabled: Boolean)Whether this view should show title items on the first row of the slice. |
Unit |
Populates this view to the provided |
Unit |
setSliceActions(newActions: (Mutable)List<SliceAction!>?)Sets the slice actions to display for the slice contained in this view. |
Protected functions |
|
|---|---|
Unit |
configureViewPolicy(maxHeight: Int)Sets the maximum height for a slice through the view policy. |
Unit |
|
Unit |
|
Unit |
|
Unit |
|
Unit |
onVisibilityChanged(changedView: View!, visibility: Int) |
Unit |
onWindowVisibilityChanged(visibility: Int) |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Constants
MODE_LARGE
const val MODE_LARGE = 2: Int
Mode indicating this slice should be presented in large format, as much or all of the slice contents are shown.
MODE_SHORTCUT
const val MODE_SHORTCUT = 3: Int
Mode indicating this slice should be presented as a tappable icon.
MODE_SMALL
const val MODE_SMALL = 1: Int
Mode indicating this slice should be presented in small format, only top-level information and actions from the slice are shown.
Public constructors
SliceView
SliceView(context: Context!, attrs: AttributeSet?, defStyleAttr: Int)
SliceView
@RequiresApi(value = 21)
SliceView(
context: Context!,
attrs: AttributeSet!,
defStyleAttr: Int,
defStyleRes: Int
)
Public functions
getHiddenItemCount
fun getHiddenItemCount(): Int
Returns the number of slice items not displayed in this view.
- In
MODE_LARGE: If the slice is not scrollable, this is the number of slice items that don't fit into the available height. If it is scrollable, this method returns 0. - In
MODE_SMALL: Returns the number of items not presented to the user. - In
MODE_SHORTCUT: Returns 0.
getSlice
fun getSlice(): Slice?
| Returns | |
|---|---|
Slice? |
the slice being used to populate this view. |
getSliceActions
fun getSliceActions(): (Mutable)List<SliceAction!>?
Returns the slice actions presented in this view.
Note that these may be different from getSliceActions if the actions set on the view have been adjusted using setSliceActions.
isScrollable
fun isScrollable(): Boolean
Whether this view allow scrollable content when presenting in MODE_LARGE.
onChanged
fun onChanged(t: Slice?): Unit
Called when the data is changed to value.
setAccentColor
fun setAccentColor(accentColor: @ColorInt Int): Unit
Contents of a slice such as icons, text, and controls (e.g. toggle) can be tinted. Normally a color for tinting will be provided by the slice. Using this method will override the slice-provided color information and instead tint elements with the color set here.
setMode
fun setMode(mode: Int): Unit
Set the mode this view should present in.
setOnSliceActionListener
fun setOnSliceActionListener(observer: SliceView.OnSliceActionListener?): Unit
Sets the listener to notify when an interaction event occurs on the view.
| See also | |
|---|---|
EventInfo |
setRowStyleFactory
fun setRowStyleFactory(rowStyleFactory: RowStyleFactory?): Unit
Sets the RowStyleFactory which allows multiple children to have different styles.
setScrollable
fun setScrollable(isScrollable: Boolean): Unit
Set whether this view should allow scrollable content when presenting in MODE_LARGE.
setShowActionDividers
fun setShowActionDividers(enabled: Boolean): Unit
Whether this view should show action dividers for rows.
setShowHeaderDivider
fun setShowHeaderDivider(enabled: Boolean): Unit
Whether this view should show the header divider.
setShowTitleItems
fun setShowTitleItems(enabled: Boolean): Unit
Whether this view should show title items on the first row of the slice. Title items appear at the start of the row.
setSlice
fun setSlice(slice: Slice?): Unit
Populates this view to the provided Slice. This will not update automatically if the slice content changes, for live content see SliceLiveData.
setSliceActions
fun setSliceActions(newActions: (Mutable)List<SliceAction!>?): Unit
Sets the slice actions to display for the slice contained in this view. Normally SliceView will automatically show actions, however, it is possible to reorder or omit actions on the view using this method. This is generally discouraged.
It is required that the slice be set on this view before actions can be set, otherwise this will throw IllegalStateException. If any of the actions supplied are not available for the slice set on this view (i.e. the action is not returned by getSliceActions this will throw IllegalArgumentException.
Protected functions
configureViewPolicy
protected fun configureViewPolicy(maxHeight: Int): Unit
Sets the maximum height for a slice through the view policy.