Search in sources :

Example 1 with FlexibleViewHolder

use of eu.davidea.viewholders.FlexibleViewHolder in project FlexibleAdapter by davideas.

the class SelectableAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List payloads) {
    // Bind the correct view elevation
    if (holder instanceof FlexibleViewHolder) {
        FlexibleViewHolder flexHolder = (FlexibleViewHolder) holder;
        flexHolder.getContentView().setActivated(isSelected(position));
        if (flexHolder.getContentView().isActivated() && flexHolder.getActivationElevation() > 0) {
            ViewCompat.setElevation(flexHolder.getContentView(), flexHolder.getActivationElevation());
        } else if (flexHolder.getActivationElevation() > 0) {
            // Leave unaltered the default elevation
            ViewCompat.setElevation(flexHolder.getContentView(), 0);
        }
        if (flexHolder.isRecyclable()) {
            mBoundViewHolders.add(flexHolder);
            log.v("onViewBound    viewSize=%s %s %s", mBoundViewHolders.size(), getClassName(holder), holder);
        } else {
            log.v("onViewBound    recyclable=%s %s %s", holder.isRecyclable(), getClassName(holder), holder);
        }
    } else {
        // When user scrolls, this line binds the correct selection status
        holder.itemView.setActivated(isSelected(position));
    }
}
Also used : FlexibleViewHolder(eu.davidea.viewholders.FlexibleViewHolder)

Example 2 with FlexibleViewHolder

use of eu.davidea.viewholders.FlexibleViewHolder in project FlexibleAdapter by davideas.

the class AnimatorAdapter method animateView.

/**
 * Performs checks to scroll animate the itemView and in case, it animates the view.
 * <p><b>Note:</b> If you have to change at runtime the LayoutManager <i>and</i> add
 * Scrollable Headers too, consider to add them in post, using a {@code delay >= 0},
 * otherwise scroll animations on all items will not start correctly.</p>
 *
 * @param holder   the ViewHolder just bound
 * @param position the current item position
 * @since 5.0.0-b1
 */
protected final void animateView(final RecyclerView.ViewHolder holder, final int position) {
    if (mRecyclerView == null) {
        return;
    }
    // Use always the max child count reached
    if (mMaxChildViews < mRecyclerView.getChildCount()) {
        mMaxChildViews = mRecyclerView.getChildCount();
    }
    // Animate only during initial loading?
    if (onlyEntryAnimation && mLastAnimatedPosition >= mMaxChildViews) {
        isForwardEnabled = false;
    }
    int lastVisiblePosition = getFlexibleLayoutManager().findLastVisibleItemPosition();
    // );
    if ((isForwardEnabled || isReverseEnabled) && !isFastScroll && holder instanceof FlexibleViewHolder && (!mAnimatorNotifierObserver.isPositionNotified() || isScrollableHeaderOrFooter(position)) && (isScrollableHeaderOrFooter(position) || (isForwardEnabled && position > lastVisiblePosition) || (isReverseEnabled && position < lastVisiblePosition) || (position == 0 && mMaxChildViews == 0))) {
        // Cancel animation is necessary when fling
        int hashCode = holder.itemView.hashCode();
        cancelExistingAnimation(hashCode);
        // User animators
        List<Animator> animators = new ArrayList<>();
        FlexibleViewHolder flexibleViewHolder = (FlexibleViewHolder) holder;
        flexibleViewHolder.scrollAnimators(animators, position, position >= lastVisiblePosition);
        // Execute the animations together
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animators);
        set.setInterpolator(mInterpolator);
        // Single view duration
        long duration = mDuration;
        for (Animator animator : animators) {
            if (animator.getDuration() != DEFAULT_DURATION) {
                duration = animator.getDuration();
            }
        }
        set.setDuration(duration);
        set.addListener(new HelperAnimatorListener(hashCode));
        if (mEntryStep) {
            // Stop stepDelay when screen is filled
            set.setStartDelay(calculateAnimationDelay(holder, position));
        }
        set.start();
        mAnimators.put(hashCode, set);
    // log.v("animateView    Scroll animation on position %s", position);
    }
    mAnimatorNotifierObserver.clearNotified();
    // Update last animated position
    mLastAnimatedPosition = position;
}
Also used : Animator(android.animation.Animator) ArrayList(java.util.ArrayList) FlexibleViewHolder(eu.davidea.viewholders.FlexibleViewHolder) AnimatorSet(android.animation.AnimatorSet)

Example 3 with FlexibleViewHolder

use of eu.davidea.viewholders.FlexibleViewHolder in project FlexibleAdapter by davideas.

the class FlexibleAdapter method removeListener.

/**
 * Removes one listener from this Adapter.
 * <p><b>Warning:</b>
 * <ul><li>In case of <i>Click</i> and <i>LongClick</i> events, it will remove also the callback
 * from all bound ViewHolders too. To restore these 2 events on the current bound ViewHolders,
 * call {@link #addListener(Object)} providing the instance of the desired listener.</li>
 * <li>To remove a specific listener you have to provide the either the instance or the Class
 * type of the listener, example:
 * <pre>
 *     removeListener(mUpdateListener);
 *     removeListener(FlexibleAdapter.OnItemLongClickListener.class);</pre></li></ul></p>
 *
 * @param listener the listener instance or Class type to remove from this Adapter and/or from all bound ViewHolders
 * @return this Adapter, so the call can be chained
 * @see #addListener(Object)
 * @since 5.0.0-rc3
 */
@CallSuper
public FlexibleAdapter<T> removeListener(Object listener) {
    if (listener == null) {
        log.e("No listener class to remove!");
        return this;
    }
    String className = getClassName(listener);
    if (listener instanceof OnItemClickListener || listener == OnItemClickListener.class) {
        mItemClickListener = null;
        log.i("Removed %s as OnItemClickListener", className);
        for (FlexibleViewHolder holder : getAllBoundViewHolders()) {
            holder.getContentView().setOnClickListener(null);
        }
    }
    if (listener instanceof OnItemLongClickListener || listener == OnItemLongClickListener.class) {
        mItemLongClickListener = null;
        log.i("Removed %s as OnItemLongClickListener", className);
        for (FlexibleViewHolder holder : getAllBoundViewHolders()) {
            holder.getContentView().setOnLongClickListener(null);
        }
    }
    if (listener instanceof OnItemMoveListener || listener == OnItemMoveListener.class) {
        mItemMoveListener = null;
        log.i("Removed %s as OnItemMoveListener", className);
    }
    if (listener instanceof OnItemSwipeListener || listener == OnItemSwipeListener.class) {
        mItemSwipeListener = null;
        log.i("Removed %s as OnItemSwipeListener", className);
    }
    if (listener instanceof OnDeleteCompleteListener || listener == OnDeleteCompleteListener.class) {
        mDeleteCompleteListener = null;
        log.i("Removed %s as OnDeleteCompleteListener", className);
    }
    if (listener instanceof OnStickyHeaderChangeListener || listener == OnStickyHeaderChangeListener.class) {
        mStickyHeaderChangeListener = null;
        log.i("Removed %s as OnStickyHeaderChangeListener", className);
    }
    if (listener instanceof OnUpdateListener || listener == OnUpdateListener.class) {
        mUpdateListener = null;
        log.i("Removed %s as OnUpdateListener", className);
    }
    if (listener instanceof OnFilterListener || listener == OnFilterListener.class) {
        mFilterListener = null;
        log.i("Removed %s as OnFilterListener", className);
    }
    return this;
}
Also used : FlexibleViewHolder(eu.davidea.viewholders.FlexibleViewHolder) CallSuper(androidx.annotation.CallSuper)

Example 4 with FlexibleViewHolder

use of eu.davidea.viewholders.FlexibleViewHolder in project FlexibleAdapter by davideas.

the class FlexibleAdapter method addListener.

/**
 * Initializes the listener(s) of this Adapter.
 * <p>This method is automatically called from the Constructor.</p>
 *
 * @param listener the object(s) instance(s) of any listener
 * @return this Adapter, so the call can be chained
 * @see #removeListener(Object)
 * @since 5.0.0-b6
 */
@CallSuper
public FlexibleAdapter<T> addListener(Object listener) {
    if (listener == null) {
        log.e("Invalid listener class: null");
        return this;
    }
    log.i("Adding listener class %s as:", getClassName(listener));
    if (listener instanceof OnItemClickListener) {
        log.i("- OnItemClickListener");
        mItemClickListener = (OnItemClickListener) listener;
        for (FlexibleViewHolder holder : getAllBoundViewHolders()) {
            holder.getContentView().setOnClickListener(holder);
        }
    }
    if (listener instanceof OnItemLongClickListener) {
        log.i("- OnItemLongClickListener");
        mItemLongClickListener = (OnItemLongClickListener) listener;
        // Restore the event
        for (FlexibleViewHolder holder : getAllBoundViewHolders()) {
            holder.getContentView().setOnLongClickListener(holder);
        }
    }
    if (listener instanceof OnItemMoveListener) {
        log.i("- OnItemMoveListener");
        mItemMoveListener = (OnItemMoveListener) listener;
    }
    if (listener instanceof OnItemSwipeListener) {
        log.i("- OnItemSwipeListener");
        mItemSwipeListener = (OnItemSwipeListener) listener;
    }
    if (listener instanceof OnDeleteCompleteListener) {
        log.i("- OnDeleteCompleteListener");
        mDeleteCompleteListener = (OnDeleteCompleteListener) listener;
    }
    if (listener instanceof OnStickyHeaderChangeListener) {
        log.i("- OnStickyHeaderChangeListener");
        mStickyHeaderChangeListener = (OnStickyHeaderChangeListener) listener;
    }
    if (listener instanceof OnUpdateListener) {
        log.i("- OnUpdateListener");
        mUpdateListener = (OnUpdateListener) listener;
        mUpdateListener.onUpdateEmptyView(getMainItemCount());
    }
    if (listener instanceof OnFilterListener) {
        log.i("- OnFilterListener");
        mFilterListener = (OnFilterListener) listener;
    }
    return this;
}
Also used : FlexibleViewHolder(eu.davidea.viewholders.FlexibleViewHolder) CallSuper(androidx.annotation.CallSuper)

Example 5 with FlexibleViewHolder

use of eu.davidea.viewholders.FlexibleViewHolder in project FlexibleAdapter by davideas.

the class StickyHeaderHelper method updateHeader.

private void updateHeader(int headerPosition, boolean updateHeaderContent) {
    // Check if there is a new header to be sticky
    if (mHeaderPosition != headerPosition && mStickyHolderLayout != null) {
        // #244 - Don't animate if header is already visible at the first layout position
        int firstVisibleItemPosition = mAdapter.getFlexibleLayoutManager().findFirstVisibleItemPosition();
        // Animate if headers were hidden, but don't if configuration changed (rotation)
        if (displayWithAnimation && mHeaderPosition == RecyclerView.NO_POSITION && headerPosition != firstVisibleItemPosition) {
            displayWithAnimation = false;
            mStickyHolderLayout.setAlpha(0);
            mStickyHolderLayout.animate().alpha(1).start();
        } else {
            mStickyHolderLayout.setAlpha(1);
        }
        int oldHeaderPosition = mHeaderPosition;
        mHeaderPosition = headerPosition;
        FlexibleViewHolder holder = getHeaderViewHolder(headerPosition);
        // Swapping header
        swapHeader(holder, oldHeaderPosition);
    } else if (updateHeaderContent) {
        // #594 - Checking same item view type and increased delay to 100ms.
        if (mStickyHeaderViewHolder.getItemViewType() == mAdapter.getItemViewType(headerPosition)) {
            mAdapter.onBindViewHolder(mStickyHeaderViewHolder, headerPosition);
        } else {
            Log.e("updateHeader Wrong itemViewType for StickyViewHolder=%s, PositionViewHolder=%s", getClassName(mStickyHeaderViewHolder), getClassName(getHeaderViewHolder(headerPosition)));
        }
        ensureHeaderParent();
    }
    translateHeader();
}
Also used : FlexibleViewHolder(eu.davidea.viewholders.FlexibleViewHolder)

Aggregations

FlexibleViewHolder (eu.davidea.viewholders.FlexibleViewHolder)6 CallSuper (androidx.annotation.CallSuper)2 Animator (android.animation.Animator)1 AnimatorSet (android.animation.AnimatorSet)1 View (android.view.View)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 ArrayList (java.util.ArrayList)1