-
Notifications
You must be signed in to change notification settings - Fork 553
5.x | ViewHolders
FlexibleViewHolder
is the predefined ViewHolder for all Adapter items. It manages the click events and manipulates the View for activation due to the selection, dragging and swiping. It drastically reduce the time of developing and extends new functionality at the same time.
Event methods:
onClick()
, onLongClick()
, onTouch()
, onActionStateChanged()
, onItemReleased()
Each of the following methods specifies a single behavior triggered from an event, carefully read the API Javadoc when calling or extending one of them:
toggleActivation()
, getActivationElevation()
, shouldAddSelectionInActionMode()
, shouldActivateViewWhileSwiping()
,
scrollAnimators()
, setDragHandleView()
, isDraggable()
, isSwipeable()
, getFrontView()
, getRearLeftView()
, getRearRightView()
.
- To address correct behaviors when using sticky headers it's important to provide
sticky=true
to the super constructor when extending this ViewHolder. - Not only, due to the original ViewHolder Android(R) implementation (
itemView
is declared final), when retrieving the Adapter position, it's no longer possible to callgetAdapterPosition()
(declared final too), therefore, only in case of sticky view holders, you now have to callgetFlexibleAdapterPosition()
. This overcomes the situation of returning an unknown position (-1) of ViewHolders created out of the LayoutManager.
The new way to scroll animate the itemView
(or any View inside it!) is to implement the method scrollAnimators
of the ViewHolder. Read the Adapter Animations Wiki page to discover the new possibilities to how animate the itemView
.
ExpandableViewHolder
extends FlexibleViewHolder
. Here we handle the events to expand/collapse an item through the usual methods onClick
, onLongClick
and onActionStateChanged
(dragging and swiping).
More settings are available in the expandable version, read carefully the API Javadoc when calling or extending one of them:
isViewExpandableOnClick()
, isViewCollapsibleOnClick()
, isViewCollapsibleOnLongClick()
, shouldNotifyParentOnClick()
, toggleExpansion()
, expandView()
, collapseView()
AnimatedViewHolder
is a new interface to animate the itemView
when a notification is triggered from the Adapter after adding/removing the item. Read the Adapter Animations Wiki page to discover the new possibilities to how animate the itemView
.
If you decide to implement this interface, the itemView
will have independent item animations.
Via Item interfaces. In this case you don't have to implement getItemViewType()
, onCreateViewHolder()
and onBindViewHolder()
adapter methods, but getLayoutRes()
(for the item type we need an int
, the layoutResID is sufficient), createViewHolder()
and bindViewHolder()
from inside the implementation of item interfaces.
Adopting item interfaces, it gives several benefits such as: Expandable items, runtime flags to enable/disable drag and swipe, to enable/disable the item itself and to allow/disallow the selection.
@Override
public int getLayoutRes() {
return R.layout.recycler_expandable_row;
}
@Override
public ViewHolder createViewHolder(View view, FlexibleAdapter<IFlexible> adapter) {
return new ViewHolder(view, adapter);
}
@Override
public void bindViewHolder(FlexibleAdapter<IFlexible> adapter, ViewHolder holder, int position,
List payloads) {
// Bind your VH
}
The ViewHolder
class is an inner [static] class of the same item interface.
From my research, no more notifyItemRangeChanged()
calls in selectAll()
and clearSelection()
methods: This means no more item binding!
Now, bound selectable ViewHolders will have the StateListDrawable
background switching status (activated<->normal) when I internally invoke FlexibleViewHolder.toggleSelection()
, so that, all inner views can complete animations with no interruptions.
ℹ️ Note:
- ViewHolders are cached when first bound and removed when they are recycled by the RecyclerView.
- ViewHolders must extend
FlexibleViewHolder
, otherwise, item binding still occurs.
- Update Data Set
- Selection modes
- Headers and Sections
- Scrollable Headers and Footers
- Expandable items
- Drag&Drop and Swipe
- EndlessScroll / On Load More
- Search Filter
- FastScroller
- Adapter Animations
- Third party Layout Managers
- Payload
- Smooth Layout Managers
- Flexible Item Decoration
- Utils
- ActionModeHelper
- AnimatorHelper
- EmptyViewHelper
- UndoHelper
* = Under revision!