我滚动太快时,RecyclerView位置错误

时间:2016-10-08 08:50:43

标签: android

这是一个复杂的RecyclerView Holder,问题是当我快速滚动到顶部时,位置将是deranged.log就像这样: it's log about position

我的适配器是这样的:

class StoryAdapter extends RecyclerView.Adapter {
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        RecyclerView.ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case StoryDetail.FormatType.HEADER:
                itemView = inflater.inflate(R.layout.story_header_holder, parent, false);
                viewHolder = new HeaderViewHolder(itemView);
                break;
            case StoryDetail.FormatType.TEXT:
                itemView = inflater.inflate(R.layout.story_text_holder, parent, false);
                viewHolder = new TextViewHolder(itemView);
                break;
            case StoryDetail.FormatType.IMAGE:
                itemView = inflater.inflate(R.layout.story_image_holder, parent, false);
                viewHolder = new ImageViewHolder(itemView);
                break;
            case StoryDetail.FormatType.FROM_GRANDPA:
                itemView = inflater.inflate(R.layout.story_grandpa_letter_holder, parent, false);
                viewHolder = new GrandpaLetterHolder(itemView);
                break;
            case StoryDetail.FormatType.FROM_USER:
                itemView = inflater.inflate(R.layout.story_user_letter_holder, parent, false);
                viewHolder = new UserLetterHolder(itemView);
                break;
            case StoryDetail.FormatType.COMMENT_TITLE:
                itemView = inflater.inflate(R.layout.story_comment_title_holder, parent, false);
                viewHolder = new CommentTitleHolder(itemView);
                break;
            case StoryDetail.FormatType.COMMENT_PLACE_HOLDER:
                itemView = inflater.inflate(R.layout.story_place_holder, parent, false);
                viewHolder = new PlaceHolder(itemView);
                break;
            case StoryDetail.FormatType.RECOMMEND_COMMENT_HEADER:
                itemView = inflater.inflate(R.layout.story_rcommend_comment_header_holder, parent, false);
                viewHolder = new ReCommentHolder(itemView);
                break;
            case StoryDetail.FormatType.RECOMMEND_COMMENTS:
                itemView = inflater.inflate(R.layout.story_comments_holder, parent, false);
                viewHolder = new CommentsHolder(itemView);
                break;
            case StoryDetail.FormatType.COMMENT_HEADER:
                itemView = inflater.inflate(R.layout.story_comment_header_holder, parent, false);
                viewHolder = new CommentHeaderHolder(itemView);
                break;
            case StoryDetail.FormatType.COMMENTS:
                itemView = inflater.inflate(R.layout.story_comments_holder, parent, false);
                viewHolder = new CommentsHolder(itemView);
                break;
            case StoryDetail.FormatType.TIP:
                itemView = inflater.inflate(R.layout.story_tip_holder, parent, false);
                viewHolder = new TipHolder(itemView);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        switch (viewType) {
            case StoryDetail.FormatType.HEADER:
                HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
                headerViewHolder.setData(storyDetail.formats.get(position));

                boolean hideSubmission =
                        TYPE_NAME_MAIL_FROM_GRANDPA.equals(storyDetail.formats.get(position).title.subtitle) ||
                                TYPE_NAME_BULLETIN.equals(storyDetail.formats.get(position).title.subtitle) ||
                                TYPE_ID_BULLETIN.equals(storyDetail.formats.get(position).title._id) ||
                                TYPE_ID_MAIL_FROM_GRANDPA.equals(storyDetail.formats.get(position).title._id);

                setSubmissionButtonState(!hideSubmission);
                break;
            case StoryDetail.FormatType.TEXT:
                TextViewHolder textViewHolder = (TextViewHolder) holder;
                textViewHolder.setData(storyDetail.formats.get(position));
                break;
            case StoryDetail.FormatType.IMAGE:
                ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
                imageViewHolder.setData(storyDetail.formats.get(position));
                break;
            case StoryDetail.FormatType.FROM_GRANDPA:
                GrandpaLetterHolder grandpaLetterHolder = (GrandpaLetterHolder) holder;
                grandpaLetterHolder.setData(storyDetail.formats.get(position));
                break;
            case StoryDetail.FormatType.FROM_USER:
                UserLetterHolder userLetterHolder = (UserLetterHolder) holder;
                userLetterHolder.setData(storyDetail.formats.get(position));
                break;
            case StoryDetail.FormatType.COMMENT_TITLE:
                break;
            case StoryDetail.FormatType.COMMENT_PLACE_HOLDER:
                break;
            case StoryDetail.FormatType.RECOMMEND_COMMENT_HEADER:
                break;
            case StoryDetail.FormatType.RECOMMEND_COMMENTS:
                **Log.e("position", position + "");**
                CommentsHolder commentsHolder = (CommentsHolder) holder;
                commentsHolder.setData(storyDetail.formats.get(position), R.mipmap.story_comment_icon_brown,
                        position + 1 < getItemCount() && storyDetail.formats.get(position + 1).type != StoryDetail.FormatType.RECOMMEND_COMMENTS);
                break;
            case StoryDetail.FormatType.COMMENT_HEADER:
                break;
            case StoryDetail.FormatType.COMMENTS:
                CommentsHolder commentsHolder1 = (CommentsHolder) holder;
                commentsHolder1.setData(storyDetail.formats.get(position), R.mipmap.story_comment_mine_icon_black, false);
                break;
            case StoryDetail.FormatType.TIP:
                TipHolder tipHolder = (TipHolder) holder;
                tipHolder.setData(storyDetail.formats.get(position));
                break;
        }

    }

    @Override
    public int getItemCount() {
        if (storyDetail == null || storyDetail.formats == null) {
            return 0;
        }
        return storyDetail.formats.size();
    }

    @Override
    public int getItemViewType(int position) {
        super.getItemViewType(position);
        return storyDetail.formats.get(position).type;
    }

所以,如你所见,有很多类型。 当我慢慢滚动时,它工作正常。 顺便说一句,它只发生滚动到顶部。 请帮帮我,非常感谢! :)

0 个答案:

没有答案