Search in sources :

Example 56 with ViewHolder

use of android.support.v7.widget.RecyclerView.ViewHolder in project MVCHelper by LuckyJayce.

the class ReBooksAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    TextView textView = (TextView) holder.itemView;
    textView.setText(books.get(position).getName());
    textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "p:" + position, Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View)

Example 57 with ViewHolder

use of android.support.v7.widget.RecyclerView.ViewHolder in project CircleDemo by Naoki2015.

the class CircleAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
    if (getItemViewType(position) == TYPE_HEAD) {
    //HeaderViewHolder holder = (HeaderViewHolder) viewHolder;
    } else {
        final int circlePosition = position - HEADVIEW_SIZE;
        final CircleViewHolder holder = (CircleViewHolder) viewHolder;
        final CircleItem circleItem = (CircleItem) datas.get(circlePosition);
        final String circleId = circleItem.getId();
        String name = circleItem.getUser().getName();
        String headImg = circleItem.getUser().getHeadUrl();
        final String content = circleItem.getContent();
        String createTime = circleItem.getCreateTime();
        final List<FavortItem> favortDatas = circleItem.getFavorters();
        final List<CommentItem> commentsDatas = circleItem.getComments();
        boolean hasFavort = circleItem.hasFavort();
        boolean hasComment = circleItem.hasComment();
        Glide.with(context).load(headImg).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.color.bg_no_photo).transform(new GlideCircleTransform(context)).into(holder.headIv);
        holder.nameTv.setText(name);
        holder.timeTv.setText(createTime);
        if (!TextUtils.isEmpty(content)) {
            holder.contentTv.setExpand(circleItem.isExpand());
            holder.contentTv.setExpandStatusListener(new ExpandTextView.ExpandStatusListener() {

                @Override
                public void statusChange(boolean isExpand) {
                    circleItem.setExpand(isExpand);
                }
            });
            holder.contentTv.setText(UrlUtils.formatUrlString(content));
        }
        holder.contentTv.setVisibility(TextUtils.isEmpty(content) ? View.GONE : View.VISIBLE);
        if (DatasUtil.curUser.getId().equals(circleItem.getUser().getId())) {
            holder.deleteBtn.setVisibility(View.VISIBLE);
        } else {
            holder.deleteBtn.setVisibility(View.GONE);
        }
        holder.deleteBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //删除
                if (presenter != null) {
                    presenter.deleteCircle(circleId);
                }
            }
        });
        if (hasFavort || hasComment) {
            if (hasFavort) {
                //处理点赞列表
                holder.praiseListView.setOnItemClickListener(new PraiseListView.OnItemClickListener() {

                    @Override
                    public void onClick(int position) {
                        String userName = favortDatas.get(position).getUser().getName();
                        String userId = favortDatas.get(position).getUser().getId();
                        Toast.makeText(MyApplication.getContext(), userName + " &id = " + userId, Toast.LENGTH_SHORT).show();
                    }
                });
                holder.praiseListView.setDatas(favortDatas);
                holder.praiseListView.setVisibility(View.VISIBLE);
            } else {
                holder.praiseListView.setVisibility(View.GONE);
            }
            if (hasComment) {
                //处理评论列表
                holder.commentList.setOnItemClickListener(new CommentListView.OnItemClickListener() {

                    @Override
                    public void onItemClick(int commentPosition) {
                        CommentItem commentItem = commentsDatas.get(commentPosition);
                        if (DatasUtil.curUser.getId().equals(commentItem.getUser().getId())) {
                            //复制或者删除自己的评论
                            CommentDialog dialog = new CommentDialog(context, presenter, commentItem, circlePosition);
                            dialog.show();
                        } else {
                            //回复别人的评论
                            if (presenter != null) {
                                CommentConfig config = new CommentConfig();
                                config.circlePosition = circlePosition;
                                config.commentPosition = commentPosition;
                                config.commentType = CommentConfig.Type.REPLY;
                                config.replyUser = commentItem.getUser();
                                presenter.showEditTextBody(config);
                            }
                        }
                    }
                });
                holder.commentList.setOnItemLongClickListener(new CommentListView.OnItemLongClickListener() {

                    @Override
                    public void onItemLongClick(int commentPosition) {
                        //长按进行复制或者删除
                        CommentItem commentItem = commentsDatas.get(commentPosition);
                        CommentDialog dialog = new CommentDialog(context, presenter, commentItem, circlePosition);
                        dialog.show();
                    }
                });
                holder.commentList.setDatas(commentsDatas);
                holder.commentList.setVisibility(View.VISIBLE);
            } else {
                holder.commentList.setVisibility(View.GONE);
            }
            holder.digCommentBody.setVisibility(View.VISIBLE);
        } else {
            holder.digCommentBody.setVisibility(View.GONE);
        }
        holder.digLine.setVisibility(hasFavort && hasComment ? View.VISIBLE : View.GONE);
        final SnsPopupWindow snsPopupWindow = holder.snsPopupWindow;
        //判断是否已点赞
        String curUserFavortId = circleItem.getCurUserFavortId(DatasUtil.curUser.getId());
        if (!TextUtils.isEmpty(curUserFavortId)) {
            snsPopupWindow.getmActionItems().get(0).mTitle = "取消";
        } else {
            snsPopupWindow.getmActionItems().get(0).mTitle = "赞";
        }
        snsPopupWindow.update();
        snsPopupWindow.setmItemClickListener(new PopupItemClickListener(circlePosition, circleItem, curUserFavortId));
        holder.snsBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //弹出popupwindow
                snsPopupWindow.showPopupWindow(view);
            }
        });
        holder.urlTipTv.setVisibility(View.GONE);
        switch(holder.viewType) {
            case // 处理链接动态的链接内容和和图片
            CircleViewHolder.TYPE_URL:
                if (holder instanceof URLViewHolder) {
                    String linkImg = circleItem.getLinkImg();
                    String linkTitle = circleItem.getLinkTitle();
                    Glide.with(context).load(linkImg).into(((URLViewHolder) holder).urlImageIv);
                    ((URLViewHolder) holder).urlContentTv.setText(linkTitle);
                    ((URLViewHolder) holder).urlBody.setVisibility(View.VISIBLE);
                    ((URLViewHolder) holder).urlTipTv.setVisibility(View.VISIBLE);
                }
                break;
            case // 处理图片
            CircleViewHolder.TYPE_IMAGE:
                if (holder instanceof ImageViewHolder) {
                    final List<PhotoInfo> photos = circleItem.getPhotos();
                    if (photos != null && photos.size() > 0) {
                        ((ImageViewHolder) holder).multiImageView.setVisibility(View.VISIBLE);
                        ((ImageViewHolder) holder).multiImageView.setList(photos);
                        ((ImageViewHolder) holder).multiImageView.setOnItemClickListener(new MultiImageView.OnItemClickListener() {

                            @Override
                            public void onItemClick(View view, int position) {
                                //imagesize是作为loading时的图片size
                                ImagePagerActivity.ImageSize imageSize = new ImagePagerActivity.ImageSize(view.getMeasuredWidth(), view.getMeasuredHeight());
                                List<String> photoUrls = new ArrayList<String>();
                                for (PhotoInfo photoInfo : photos) {
                                    photoUrls.add(photoInfo.url);
                                }
                                ImagePagerActivity.startImagePagerActivity(((MainActivity) context), photoUrls, position, imageSize);
                            }
                        });
                    } else {
                        ((ImageViewHolder) holder).multiImageView.setVisibility(View.GONE);
                    }
                }
                break;
            case CircleViewHolder.TYPE_VIDEO:
                if (holder instanceof VideoViewHolder) {
                    ((VideoViewHolder) holder).videoView.setVideoUrl(circleItem.getVideoUrl());
                    //视频封面图片
                    ((VideoViewHolder) holder).videoView.setVideoImgUrl(circleItem.getVideoImgUrl());
                    ((VideoViewHolder) holder).videoView.setPostion(position);
                    ((VideoViewHolder) holder).videoView.setOnPlayClickListener(new CircleVideoView.OnPlayClickListener() {

                        @Override
                        public void onPlayClick(int pos) {
                            curPlayIndex = pos;
                        }
                    });
                }
                break;
            default:
                break;
        }
    }
}
Also used : PhotoInfo(com.yiw.circledemo.bean.PhotoInfo) CircleViewHolder(com.yiw.circledemo.adapter.viewholder.CircleViewHolder) PraiseListView(com.yiw.circledemo.widgets.PraiseListView) VideoViewHolder(com.yiw.circledemo.adapter.viewholder.VideoViewHolder) CommentConfig(com.yiw.circledemo.bean.CommentConfig) SnsPopupWindow(com.yiw.circledemo.widgets.SnsPopupWindow) ImageViewHolder(com.yiw.circledemo.adapter.viewholder.ImageViewHolder) ArrayList(java.util.ArrayList) List(java.util.List) GlideCircleTransform(com.yiw.circledemo.utils.GlideCircleTransform) CommentListView(com.yiw.circledemo.widgets.CommentListView) MultiImageView(com.yiw.circledemo.widgets.MultiImageView) FavortItem(com.yiw.circledemo.bean.FavortItem) ExpandTextView(com.yiw.circledemo.widgets.ExpandTextView) CircleVideoView(com.yiw.circledemo.widgets.CircleVideoView) ExpandTextView(com.yiw.circledemo.widgets.ExpandTextView) CommentListView(com.yiw.circledemo.widgets.CommentListView) View(android.view.View) MultiImageView(com.yiw.circledemo.widgets.MultiImageView) PraiseListView(com.yiw.circledemo.widgets.PraiseListView) RecyclerView(android.support.v7.widget.RecyclerView) CommentDialog(com.yiw.circledemo.widgets.dialog.CommentDialog) ImagePagerActivity(com.yiw.circledemo.activity.ImagePagerActivity) URLViewHolder(com.yiw.circledemo.adapter.viewholder.URLViewHolder) CommentItem(com.yiw.circledemo.bean.CommentItem) CircleItem(com.yiw.circledemo.bean.CircleItem) CircleVideoView(com.yiw.circledemo.widgets.CircleVideoView)

Example 58 with ViewHolder

use of android.support.v7.widget.RecyclerView.ViewHolder in project CircleDemo by Naoki2015.

the class CircleAdapter method onCreateViewHolder.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;
    if (viewType == TYPE_HEAD) {
        View headView = LayoutInflater.from(parent.getContext()).inflate(R.layout.head_circle, parent, false);
        viewHolder = new HeaderViewHolder(headView);
    } else {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_circle_item, parent, false);
        if (viewType == CircleViewHolder.TYPE_URL) {
            viewHolder = new URLViewHolder(view);
        } else if (viewType == CircleViewHolder.TYPE_IMAGE) {
            viewHolder = new ImageViewHolder(view);
        } else if (viewType == CircleViewHolder.TYPE_VIDEO) {
            viewHolder = new VideoViewHolder(view);
        }
    }
    return viewHolder;
}
Also used : URLViewHolder(com.yiw.circledemo.adapter.viewholder.URLViewHolder) ImageViewHolder(com.yiw.circledemo.adapter.viewholder.ImageViewHolder) RecyclerView(android.support.v7.widget.RecyclerView) CircleVideoView(com.yiw.circledemo.widgets.CircleVideoView) ExpandTextView(com.yiw.circledemo.widgets.ExpandTextView) CommentListView(com.yiw.circledemo.widgets.CommentListView) View(android.view.View) MultiImageView(com.yiw.circledemo.widgets.MultiImageView) PraiseListView(com.yiw.circledemo.widgets.PraiseListView) RecyclerView(android.support.v7.widget.RecyclerView) VideoViewHolder(com.yiw.circledemo.adapter.viewholder.VideoViewHolder)

Example 59 with ViewHolder

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

the class ItemTouchHelper method findSwipedView.

private ViewHolder findSwipedView(MotionEvent motionEvent) {
    final RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager();
    if (mActivePointerId == ACTIVE_POINTER_ID_NONE) {
        return null;
    }
    final int pointerIndex = MotionEventCompat.findPointerIndex(motionEvent, mActivePointerId);
    final float dx = MotionEventCompat.getX(motionEvent, pointerIndex) - mInitialTouchX;
    final float dy = MotionEventCompat.getY(motionEvent, pointerIndex) - mInitialTouchY;
    final float absDx = Math.abs(dx);
    final float absDy = Math.abs(dy);
    if (absDx < mSlop && absDy < mSlop) {
        return null;
    }
    if (absDx > absDy && lm.canScrollHorizontally()) {
        return null;
    } else if (absDy > absDx && lm.canScrollVertically()) {
        return null;
    }
    View child = findChildView(motionEvent);
    if (child == null) {
        return null;
    }
    return mRecyclerView.getChildViewHolder(child);
}
Also used : RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 60 with ViewHolder

use of android.support.v7.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 RowViewHolder to drag or swipe. Can be null if you want to cancel the
     *                    current action
     * @param actionState The type of action
     */
private 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(ValueAnimatorCompat 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 : ViewHolder(android.support.v7.widget.RecyclerView.ViewHolder) ViewParent(android.view.ViewParent) ValueAnimatorCompat(android.support.v4.animation.ValueAnimatorCompat)

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