Skip to content
Davide Steduto edited this page Apr 29, 2018 · 22 revisions

In this page


FlexibleViewHolder

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()

Settings for itemView

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().

Important settings for sticky headers

  • 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 call getAdapterPosition() (declared final too), therefore, only in case of sticky view holders, you now have to call getFlexibleAdapterPosition(). This overcomes the situation of returning an unknown position (-1) of ViewHolders created out of the LayoutManager.

Scrolling animation

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

ExpandableViewHolder extends FlexibleViewHolder. Here we handle the events to expand/collapse an item through the usual methods onClick, onLongClick and onActionStateChanged (dragging and swiping).

Settings for itemView

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

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.

Create/Bind ViewHolders

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.

No more item binding on selection/deselection

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.

Clone this wiki locally