Search in sources :

Example 61 with ViewHolder

use of android.support.v7.widget.RecyclerView.ViewHolder in project Carbon by ZieIony.

the class ItemTouchHelper method moveIfNecessary.

/**
     * Checks if we should swap w/ another view holder.
     */
private 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(android.support.v7.widget.RecyclerView.ViewHolder)

Example 62 with ViewHolder

use of android.support.v7.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(android.support.v7.widget.RecyclerView.ViewHolder) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 63 with ViewHolder

use of android.support.v7.widget.RecyclerView.ViewHolder in project actor-platform by actorapp.

the class CustomItemAnimator 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 : AnimatorViewHolder(im.actor.sdk.controllers.fragment.AnimatorViewHolder) ViewHolder(android.support.v7.widget.RecyclerView.ViewHolder) View(android.view.View)

Example 64 with ViewHolder

use of android.support.v7.widget.RecyclerView.ViewHolder in project actor-platform by actorapp.

the class CustomItemAnimator method endAnimation.

@Override
public void endAnimation(ViewHolder item) {
    final View view = item.itemView;
    // this will trigger end callback which should set properties to their target values.
    ViewCompat.animate(view).cancel();
    // TODO if some other animations are chained to end, how do we cancel them as well?
    for (int i = mPendingMoves.size() - 1; i >= 0; i--) {
        MoveInfo moveInfo = mPendingMoves.get(i);
        if (moveInfo.holder == item) {
            ViewCompat.setTranslationY(view, 0);
            ViewCompat.setTranslationX(view, 0);
            dispatchMoveFinished(item);
            mPendingMoves.remove(i);
        }
    }
    endChangeAnimation(mPendingChanges, item);
    if (mPendingRemovals.remove(item)) {
        ViewCompat.setAlpha(view, 1);
        dispatchRemoveFinished(item);
    }
    if (mPendingAdditions.remove(item)) {
        ViewCompat.setAlpha(view, 1);
        dispatchAddFinished(item);
    }
    for (int i = mChangesList.size() - 1; i >= 0; i--) {
        ArrayList<ChangeInfo> changes = mChangesList.get(i);
        endChangeAnimation(changes, item);
        if (changes.isEmpty()) {
            mChangesList.remove(i);
        }
    }
    for (int i = mMovesList.size() - 1; i >= 0; i--) {
        ArrayList<MoveInfo> moves = mMovesList.get(i);
        for (int j = moves.size() - 1; j >= 0; j--) {
            MoveInfo moveInfo = moves.get(j);
            if (moveInfo.holder == item) {
                ViewCompat.setTranslationY(view, 0);
                ViewCompat.setTranslationX(view, 0);
                dispatchMoveFinished(item);
                moves.remove(j);
                if (moves.isEmpty()) {
                    mMovesList.remove(i);
                }
                break;
            }
        }
    }
    for (int i = mAdditionsList.size() - 1; i >= 0; i--) {
        ArrayList<ViewHolder> additions = mAdditionsList.get(i);
        if (additions.remove(item)) {
            ViewCompat.setAlpha(view, 1);
            dispatchAddFinished(item);
            if (additions.isEmpty()) {
                mAdditionsList.remove(i);
            }
        }
    }
    //noinspection PointlessBooleanExpression,ConstantConditions
    if (mRemoveAnimations.remove(item) && DEBUG) {
        throw new IllegalStateException("after animation is cancelled, item should not be in " + "mRemoveAnimations list");
    }
    //noinspection PointlessBooleanExpression,ConstantConditions
    if (mAddAnimations.remove(item) && DEBUG) {
        throw new IllegalStateException("after animation is cancelled, item should not be in " + "mAddAnimations list");
    }
    //noinspection PointlessBooleanExpression,ConstantConditions
    if (mChangeAnimations.remove(item) && DEBUG) {
        throw new IllegalStateException("after animation is cancelled, item should not be in " + "mChangeAnimations list");
    }
    //noinspection PointlessBooleanExpression,ConstantConditions
    if (mMoveAnimations.remove(item) && DEBUG) {
        throw new IllegalStateException("after animation is cancelled, item should not be in " + "mMoveAnimations list");
    }
    dispatchFinishedWhenDone();
}
Also used : AnimatorViewHolder(im.actor.sdk.controllers.fragment.AnimatorViewHolder) ViewHolder(android.support.v7.widget.RecyclerView.ViewHolder) View(android.view.View)

Example 65 with ViewHolder

use of android.support.v7.widget.RecyclerView.ViewHolder in project storymaker by StoryMaker.

the class InstanceIndexItemAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Context context = holder.card.getContext();
    final BaseIndexItem baseItem = mDataset.get(position);
    String description = baseItem.getDescription();
    if (baseItem instanceof InstanceIndexItem) {
        final InstanceIndexItem instanceItem = (InstanceIndexItem) baseItem;
        holder.title.setText(String.format("%s %s", !TextUtils.isEmpty(instanceItem.getTitle()) ? instanceItem.getTitle() : context.getString(R.string.no_title), instanceItem.getStoryCreationDate() == 0 ? "" : sdf.format(new Date(instanceItem.getStoryCreationDate()))));
        int mediumStringResId;
        String storyType = TextUtils.isEmpty(instanceItem.getStoryType()) ? "?" : instanceItem.getStoryType();
        switch(storyType) {
            case "video":
                mediumStringResId = R.string.lbl_video;
                break;
            case "audio":
                mediumStringResId = R.string.lbl_audio;
                break;
            case "photo":
                mediumStringResId = R.string.lbl_photo;
                break;
            default:
                mediumStringResId = R.string.no_medium;
                break;
        }
        description = context.getString(mediumStringResId) + ". " + context.getString(org.storymaker.app.R.string.last_modified) + ": ";
        if (!TextUtils.isEmpty(instanceItem.getInstanceFilePath())) {
            description += sdf.format(new Date(new File(instanceItem.getInstanceFilePath()).lastModified()));
        }
        /*
            holder.card.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onStorySelected(instanceItem);
                    }
                }
            });
            */
        String thumbnailPath = baseItem.getThumbnailPath();
        if (!TextUtils.isEmpty(thumbnailPath)) {
            if (thumbnailPath.startsWith("http")) {
                Picasso.with(context).load(thumbnailPath).into(holder.thumb);
            } else {
                Picasso.with(context).load(// FIXME leaving for now, but doesnt picasso handle making teh File object iteself?
                new File(thumbnailPath)).into(holder.thumb);
            }
        } else {
            Picasso.with(context).load(R.drawable.no_thumbnail).into(holder.thumb);
        }
    } else {
        ExpansionIndexItem expansionIndexItem = (ExpansionIndexItem) baseItem;
        // check if this is already installed or waiting to be downloaded to change which picture we show
        HashMap<String, ExpansionIndexItem> installedIds = StorymakerIndexManager.loadInstalledIdIndex(context, installedDao);
        holder.title.setText(baseItem.getTitle());
        // show the content pack version info for help debugging upgrade issues
        if (BuildConfig.DEBUG) {
            int patchVer = !TextUtils.isEmpty(expansionIndexItem.getPatchFileVersion()) ? Integer.parseInt(expansionIndexItem.getPatchFileVersion()) : 0;
            String ver = expansionIndexItem.getExpansionFileVersion() + ((patchVer > 0) ? ("." + patchVer) : "");
            description = "v" + ver + ": " + description;
        }
        // need to verify that content pack containing thumbnail actually exists
        File contentCheck = new File(StorymakerIndexManager.buildFilePath(expansionIndexItem, context), StorymakerIndexManager.buildFileName(expansionIndexItem, Constants.MAIN));
        // need to verify that index item has been updated with content pack thumbnail path
        String contentPath = expansionIndexItem.getPackageName() + File.separator + expansionIndexItem.getExpansionId();
        if (installedIds.containsKey(expansionIndexItem.getExpansionId()) && contentCheck.exists() && baseItem.getThumbnailPath().startsWith(contentPath)) {
            //              ZipHelper.getTempFile((baseItem.getThumbnailPath(), "/sdcard/"
            holder.thumb.setImageBitmap(BitmapFactory.decodeStream(ZipHelper.getFileInputStream(baseItem.getThumbnailPath(), context)));
        } else {
            String thumbnailPath = baseItem.getThumbnailPath();
            if (!TextUtils.isEmpty(thumbnailPath)) {
                if (thumbnailPath.startsWith("http")) {
                    Picasso.with(context).load(thumbnailPath).into(holder.thumb);
                } else {
                    File file = StorymakerIndexManager.copyThumbnail(context, expansionIndexItem.getThumbnailPath());
                    Picasso.with(context).load(file).into(holder.thumb);
                }
            } else {
                Picasso.with(context).load(R.drawable.no_thumbnail).into(holder.thumb);
            }
        // FIXME desaturate image and overlay the downarrow
        }
    }
    holder.card.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onStorySelected(baseItem);
            }
        }
    });
    holder.card.setOnLongClickListener(new DeleteListener(context, baseItem));
    holder.description.setText(description);
}
Also used : Context(android.content.Context) ExpansionIndexItem(scal.io.liger.model.sqlbrite.ExpansionIndexItem) BaseIndexItem(scal.io.liger.model.sqlbrite.BaseIndexItem) ImageView(android.widget.ImageView) View(android.view.View) CardView(android.support.v7.widget.CardView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) InstanceIndexItem(scal.io.liger.model.sqlbrite.InstanceIndexItem) Date(java.util.Date) File(java.io.File)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)265 View (android.view.View)248 TextView (android.widget.TextView)129 ImageView (android.widget.ImageView)95 ViewHolder (android.support.v7.widget.RecyclerView.ViewHolder)47 LayoutInflater (android.view.LayoutInflater)34 ViewGroup (android.view.ViewGroup)32 ItemTouchHelper (android.support.v7.widget.helper.ItemTouchHelper)28 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)27 Intent (android.content.Intent)26 ArrayList (java.util.ArrayList)25 Context (android.content.Context)21 ViewPropertyAnimatorCompat (android.support.v4.view.ViewPropertyAnimatorCompat)17 BindView (butterknife.BindView)17 CardView (android.support.v7.widget.CardView)15 Paint (android.graphics.Paint)11 ParallaxRecyclerAdapter (com.poliveira.parallaxrecycleradapter.ParallaxRecyclerAdapter)8 ExampleData (de.madcyph3r.example.data.ExampleData)8 Drawable (android.graphics.drawable.Drawable)7 Test (org.junit.Test)7