Search in sources :

Example 6 with ViewHolder

use of androidx.recyclerview.widget.RecyclerView.ViewHolder in project Lightning-Browser by anthonycr.

the class VerticalItemAnimator method endAnimations.

@Override
public void endAnimations() {
    int count = mPendingMoves.size();
    for (int i = count - 1; i >= 0; i--) {
        MoveInfo item = mPendingMoves.get(i);
        View view = item.holder.itemView;
        ViewCompat.setTranslationY(view, 0);
        ViewCompat.setTranslationX(view, 0);
        dispatchMoveFinished(item.holder);
        mPendingMoves.remove(i);
    }
    count = mPendingRemovals.size();
    for (int i = count - 1; i >= 0; i--) {
        ViewHolder item = mPendingRemovals.get(i);
        dispatchRemoveFinished(item);
        mPendingRemovals.remove(i);
    }
    count = mPendingAdditions.size();
    for (int i = count - 1; i >= 0; i--) {
        ViewHolder item = mPendingAdditions.get(i);
        View view = item.itemView;
        ViewCompat.setAlpha(view, 1);
        dispatchAddFinished(item);
        mPendingAdditions.remove(i);
    }
    count = mPendingChanges.size();
    for (int i = count - 1; i >= 0; i--) {
        endChangeAnimationIfNecessary(mPendingChanges.get(i));
    }
    mPendingChanges.clear();
    if (!isRunning()) {
        return;
    }
    int listCount = mMovesList.size();
    for (int i = listCount - 1; i >= 0; i--) {
        ArrayList<MoveInfo> moves = mMovesList.get(i);
        count = moves.size();
        for (int j = count - 1; j >= 0; j--) {
            MoveInfo moveInfo = moves.get(j);
            ViewHolder item = moveInfo.holder;
            View view = item.itemView;
            ViewCompat.setTranslationY(view, 0);
            ViewCompat.setTranslationX(view, 0);
            dispatchMoveFinished(moveInfo.holder);
            moves.remove(j);
            if (moves.isEmpty()) {
                mMovesList.remove(moves);
            }
        }
    }
    listCount = mAdditionsList.size();
    for (int i = listCount - 1; i >= 0; i--) {
        ArrayList<ViewHolder> additions = mAdditionsList.get(i);
        count = additions.size();
        for (int j = count - 1; j >= 0; j--) {
            ViewHolder item = additions.get(j);
            View view = item.itemView;
            ViewCompat.setAlpha(view, 1);
            dispatchAddFinished(item);
            additions.remove(j);
            if (additions.isEmpty()) {
                mAdditionsList.remove(additions);
            }
        }
    }
    listCount = mChangesList.size();
    for (int i = listCount - 1; i >= 0; i--) {
        ArrayList<ChangeInfo> changes = mChangesList.get(i);
        count = changes.size();
        for (int j = count - 1; j >= 0; j--) {
            endChangeAnimationIfNecessary(changes.get(j));
            if (changes.isEmpty()) {
                mChangesList.remove(changes);
            }
        }
    }
    cancelAll(mRemoveAnimations);
    cancelAll(mMoveAnimations);
    cancelAll(mAddAnimations);
    cancelAll(mChangeAnimations);
    dispatchAnimationsFinished();
}
Also used : ViewHolder(androidx.recyclerview.widget.RecyclerView.ViewHolder) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Example 7 with ViewHolder

use of androidx.recyclerview.widget.RecyclerView.ViewHolder in project Carbon by ZieIony.

the class ItemTouchHelper method findSwapTargets.

private List<ViewHolder> findSwapTargets(ViewHolder viewHolder) {
    if (mSwapTargets == null) {
        mSwapTargets = new ArrayList<ViewHolder>();
        mDistances = new ArrayList<Integer>();
    } else {
        mSwapTargets.clear();
        mDistances.clear();
    }
    final int margin = mCallback.getBoundingBoxMargin();
    final int left = Math.round(mSelectedStartX + mDx) - margin;
    final int top = Math.round(mSelectedStartY + mDy) - margin;
    final int right = left + viewHolder.itemView.getWidth() + 2 * margin;
    final int bottom = top + viewHolder.itemView.getHeight() + 2 * margin;
    final int centerX = (left + right) / 2;
    final int centerY = (top + bottom) / 2;
    final RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager();
    final int childCount = lm.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View other = lm.getChildAt(i);
        if (other == viewHolder.itemView) {
            // myself!
            continue;
        }
        if (other.getBottom() < top || other.getTop() > bottom || other.getRight() < left || other.getLeft() > right) {
            continue;
        }
        final ViewHolder otherVh = mRecyclerView.getChildViewHolder(other);
        if (mCallback.canDropOver(mRecyclerView, mSelected, otherVh)) {
            // find the index to add
            final int dx = Math.abs(centerX - (other.getLeft() + other.getRight()) / 2);
            final int dy = Math.abs(centerY - (other.getTop() + other.getBottom()) / 2);
            final int dist = dx * dx + dy * dy;
            int pos = 0;
            final int cnt = mSwapTargets.size();
            for (int j = 0; j < cnt; j++) {
                if (dist > mDistances.get(j)) {
                    pos++;
                } else {
                    break;
                }
            }
            mSwapTargets.add(pos, otherVh);
            mDistances.add(pos, dist);
        }
    }
    return mSwapTargets;
}
Also used : ViewHolder(androidx.recyclerview.widget.RecyclerView.ViewHolder) RecyclerView(androidx.recyclerview.widget.RecyclerView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Example 8 with ViewHolder

use of androidx.recyclerview.widget.RecyclerView.ViewHolder in project Carbon by ZieIony.

the class ItemTouchHelper method moveIfNecessary.

/**
 * Checks if we should swap w/ another view holder.
 */
void moveIfNecessary(ViewHolder viewHolder) {
    if (mRecyclerView.isLayoutRequested()) {
        return;
    }
    if (mActionState != ACTION_STATE_DRAG) {
        return;
    }
    final float threshold = mCallback.getMoveThreshold(viewHolder);
    final int x = (int) (mSelectedStartX + mDx);
    final int y = (int) (mSelectedStartY + mDy);
    if (Math.abs(y - viewHolder.itemView.getTop()) < viewHolder.itemView.getHeight() * threshold && Math.abs(x - viewHolder.itemView.getLeft()) < viewHolder.itemView.getWidth() * threshold) {
        return;
    }
    List<ViewHolder> swapTargets = findSwapTargets(viewHolder);
    if (swapTargets.size() == 0) {
        return;
    }
    // may swap.
    ViewHolder target = mCallback.chooseDropTarget(viewHolder, swapTargets, x, y);
    if (target == null) {
        mSwapTargets.clear();
        mDistances.clear();
        return;
    }
    final int toPosition = target.getAdapterPosition();
    final int fromPosition = viewHolder.getAdapterPosition();
    if (mCallback.onMove(mRecyclerView, viewHolder, target)) {
        // keep target visible
        mCallback.onMoved(mRecyclerView, viewHolder, fromPosition, target, toPosition, x, y);
    }
}
Also used : ViewHolder(androidx.recyclerview.widget.RecyclerView.ViewHolder)

Example 9 with ViewHolder

use of androidx.recyclerview.widget.RecyclerView.ViewHolder in project Carbon by ZieIony.

the class ItemTouchHelper method select.

/**
 * Starts dragging or swiping the given View. Call with null if you want to clear it.
 *
 * @param selected    The ViewHolder to drag or swipe. Can be null if you want to cancel the
 *                    current action
 * @param actionState The type of action
 */
void select(ViewHolder selected, int actionState) {
    if (selected == mSelected && actionState == mActionState) {
        return;
    }
    mDragScrollStartTimeInMs = Long.MIN_VALUE;
    final int prevActionState = mActionState;
    // prevent duplicate animations
    endRecoverAnimation(selected, true);
    mActionState = actionState;
    if (actionState == ACTION_STATE_DRAG) {
        // we remove after animation is complete. this means we only elevate the last drag
        // child but that should perform good enough as it is very hard to start dragging a
        // new child before the previous one settles.
        mOverdrawChild = selected.itemView;
        addChildDrawingOrderCallback();
    }
    int actionStateMask = (1 << (DIRECTION_FLAG_COUNT + DIRECTION_FLAG_COUNT * actionState)) - 1;
    boolean preventLayout = false;
    if (mSelected != null) {
        final ViewHolder prevSelected = mSelected;
        if (prevSelected.itemView.getParent() != null) {
            final int swipeDir = prevActionState == ACTION_STATE_DRAG ? 0 : swipeIfNecessary(prevSelected);
            releaseVelocityTracker();
            // find where we should animate to
            final float targetTranslateX, targetTranslateY;
            int animationType;
            switch(swipeDir) {
                case LEFT:
                case RIGHT:
                case START:
                case END:
                    targetTranslateY = 0;
                    targetTranslateX = Math.signum(mDx) * mRecyclerView.getWidth();
                    break;
                case UP:
                case DOWN:
                    targetTranslateX = 0;
                    targetTranslateY = Math.signum(mDy) * mRecyclerView.getHeight();
                    break;
                default:
                    targetTranslateX = 0;
                    targetTranslateY = 0;
            }
            if (prevActionState == ACTION_STATE_DRAG) {
                animationType = ANIMATION_TYPE_DRAG;
            } else if (swipeDir > 0) {
                animationType = ANIMATION_TYPE_SWIPE_SUCCESS;
            } else {
                animationType = ANIMATION_TYPE_SWIPE_CANCEL;
            }
            getSelectedDxDy(mTmpPosition);
            final float currentTranslateX = mTmpPosition[0];
            final float currentTranslateY = mTmpPosition[1];
            final RecoverAnimation rv = new RecoverAnimation(prevSelected, animationType, prevActionState, currentTranslateX, currentTranslateY, targetTranslateX, targetTranslateY) {

                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    if (this.mOverridden) {
                        return;
                    }
                    if (swipeDir <= 0) {
                        // this is a drag or failed swipe. recover immediately
                        mCallback.clearView(mRecyclerView, prevSelected);
                    // full cleanup will happen on onDrawOver
                    } else {
                        // wait until remove animation is complete.
                        mPendingCleanup.add(prevSelected.itemView);
                        mIsPendingCleanup = true;
                        if (swipeDir > 0) {
                            // Animation might be ended by other animators during a layout.
                            // We defer callback to avoid editing adapter during a layout.
                            postDispatchSwipe(this, swipeDir);
                        }
                    }
                    // removed from the list after it is drawn for the last time
                    if (mOverdrawChild == prevSelected.itemView) {
                        removeChildDrawingOrderCallbackIfNecessary(prevSelected.itemView);
                    }
                }
            };
            final long duration = mCallback.getAnimationDuration(mRecyclerView, animationType, targetTranslateX - currentTranslateX, targetTranslateY - currentTranslateY);
            rv.setDuration(duration);
            mRecoverAnimations.add(rv);
            rv.start();
            preventLayout = true;
        } else {
            removeChildDrawingOrderCallbackIfNecessary(prevSelected.itemView);
            mCallback.clearView(mRecyclerView, prevSelected);
        }
        mSelected = null;
    }
    if (selected != null) {
        mSelectedFlags = (mCallback.getAbsoluteMovementFlags(mRecyclerView, selected) & actionStateMask) >> (mActionState * DIRECTION_FLAG_COUNT);
        mSelectedStartX = selected.itemView.getLeft();
        mSelectedStartY = selected.itemView.getTop();
        mSelected = selected;
        if (actionState == ACTION_STATE_DRAG) {
            mSelected.itemView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
        }
    }
    final ViewParent rvParent = mRecyclerView.getParent();
    if (rvParent != null) {
        rvParent.requestDisallowInterceptTouchEvent(mSelected != null);
    }
    if (!preventLayout) {
        mRecyclerView.getLayoutManager().requestSimpleAnimationsInNextLayout();
    }
    mCallback.onSelectedChanged(mSelected, mActionState);
    mRecyclerView.invalidate();
}
Also used : Animator(android.animation.Animator) ValueAnimator(android.animation.ValueAnimator) ViewHolder(androidx.recyclerview.widget.RecyclerView.ViewHolder) ViewParent(android.view.ViewParent)

Example 10 with ViewHolder

use of androidx.recyclerview.widget.RecyclerView.ViewHolder in project epoxy by airbnb.

the class ListenersUtils method getEpoxyHolderForChildView.

@Nullable
static EpoxyViewHolder getEpoxyHolderForChildView(View v) {
    RecyclerView recyclerView = findParentRecyclerView(v);
    if (recyclerView == null) {
        return null;
    }
    ViewHolder viewHolder = recyclerView.findContainingViewHolder(v);
    if (viewHolder == null) {
        return null;
    }
    if (!(viewHolder instanceof EpoxyViewHolder)) {
        return null;
    }
    return (EpoxyViewHolder) viewHolder;
}
Also used : ViewHolder(androidx.recyclerview.widget.RecyclerView.ViewHolder) RecyclerView(androidx.recyclerview.widget.RecyclerView) Nullable(androidx.annotation.Nullable)

Aggregations

ViewHolder (androidx.recyclerview.widget.RecyclerView.ViewHolder)23 View (android.view.View)16 RecyclerView (androidx.recyclerview.widget.RecyclerView)16 AnimatedViewHolder (eu.davidea.viewholders.AnimatedViewHolder)6 ArrayList (java.util.ArrayList)4 ViewPropertyAnimatorCompat (androidx.core.view.ViewPropertyAnimatorCompat)3 NonNull (androidx.annotation.NonNull)2 Animator (android.animation.Animator)1 ValueAnimator (android.animation.ValueAnimator)1 ViewParent (android.view.ViewParent)1 Nullable (androidx.annotation.Nullable)1