Android RecyclerView洗牌项目

Android RecyclerView洗牌项目,android,android-recyclerview,android-viewholder,sectionedrecyclerviewadapter,Android,Android Recyclerview,Android Viewholder,Sectionedrecyclerviewadapter,我知道这个问题已经被问了好几次,但问题仍然存在。所以问题是,当我滚动我的垂直RecyclerView列表时,ViewHolder中的项目会被洗牌。我有一些对象的列表,其中布尔值决定了视图类型,因此我可以放大各种视图。下面是代码: @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if(viewType == TYP

我知道这个问题已经被问了好几次,但问题仍然存在。所以问题是,当我滚动我的垂直
RecyclerView
列表时,
ViewHolder
中的项目会被洗牌。我有一些对象的列表,其中布尔值决定了
视图类型
,因此我可以放大各种视图。下面是代码:

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    if(viewType == TYPE_ACTIVE) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_poll_item_active, parent, false);
        return new CardViewHolderActive(view);

    } else if(viewType == TYPE_INACTIVE) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_poll_item_past, parent, false);
        return new CardViewHolderPast(view);
    }

    throw new RuntimeException("There is no type that matches the type " + viewType + " + make sure your using types correctly");

}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    Poll poll = mListPollObjects.get(position);
    Log.d("ADAPTER", "position - " + position);
    if(holder instanceof CardViewHolderActive) {
        ((CardViewHolderActive) holder).bindActivePolls(poll);

    } else if(holder instanceof CardViewHolderPast) {

        if(position == 0) {
            ((CardViewHolderPast) holder).bindPastPolls(poll, true);

        } else if(getItemViewType(position - 1) == TYPE_INACTIVE) {
            ((CardViewHolderPast) holder).bindPastPolls(poll, false);

        } else if(getItemViewType(position - 1) == TYPE_ACTIVE)
            ((CardViewHolderPast) holder).bindPastPolls(poll, true);
    }

}

@Override
public int getItemCount() {
    return mListPollObjects.size();
}


/**
 * Added to solve the RecyclerView shuffling problem.
 *
 * @param position
 * @return
 */
@Override
public long getItemId(int position) {
    return position;
}

/**
 * Added to solve the RecyclerView shuffling problem.
 *
 * @param position
 * @return
 */

@Override
public int getItemViewType(int position) {
    Poll poll = mListPollObjects.get(position);

    if(poll.getStatus().equals(Poll.STATUS_ACTIVE)) {
        return TYPE_ACTIVE;
    } else {
        return TYPE_INACTIVE;
    }
}
所以当我有:

@Override
public int getItemViewType(int position) {
    return position;
}
洗牌已经消失,但是我必须手动在
onBindViewHolder
中进行绑定,所以我决定切换到
viewTypes
。我真的搞不懂这里到底发生了什么,这很烦人。。。希望我提供了足够的信息。提前谢谢

以下是来自
CardViewHolderActive
bindActivePolls
方法:

public void bindActivePolls(final Poll polls) {

        int listImageSize = polls.getListImageObjects().size();

        //tvPollTitle.setText(polls.getStatus() + " - " + polls.getId());

        if(polls.getTitle() != null || !polls.getTitle().equals("")) {
            tvPollTitle.setText(polls.getTitle());
        } else {
            tvPollTitle.setText("");
        }

        tvCurrentVotes.setText(String.valueOf(polls.getVotesCount()) + "/");
        tvUpToVotes.setText(String.valueOf(polls.getMaxVotes()));

        // Setting the percentage bars.

        pbProgressOne.setProgress(votePercentageCalculator(polls.getVotesCount(), polls.getListImageObjects().get(0).getVotes()));
        tvProgressOne.setText(String.valueOf(pbProgressOne.getProgress()) + mContext.getString(R.string.percentage));

        pbProgressTwo.setProgress(votePercentageCalculator(polls.getVotesCount(), polls.getListImageObjects().get(1).getVotes()));
        tvProgressTwo.setText(String.valueOf(pbProgressTwo.getProgress()) + mContext.getString(R.string.percentage));

        GlideApp.with(mContext)
                .asDrawable()
                .load(polls.getListImageObjects().get(0).getUrlImageMainThumbnail())
                .transforms(new CenterCrop(), new RoundedCorners(20))
                .into(ivImageOne);

        GlideApp.with(mContext)
                .asDrawable()
                .load(polls.getListImageObjects().get(1).getUrlImageMainThumbnail())
                .transforms(new CenterCrop(), new RoundedCorners(20))
                .into(ivImageTwo);

        if(listImageSize >= 3) {

            pbProgressThree.setProgress(votePercentageCalculator(polls.getVotesCount(), polls.getListImageObjects().get(2).getVotes()));
            tvProgressThree.setText(String.valueOf(pbProgressThree.getProgress()) + mContext.getString(R.string.percentage));

            GlideApp.with(mContext)
                    .asDrawable()
                    .load(polls.getListImageObjects().get(2).getUrlImageMainThumbnail())
                    .transforms(new CenterCrop(), new RoundedCorners(20))
                    .into(ivImageThree);
        }
        if(listImageSize >= 4) {

            pbProgressFour.setProgress(votePercentageCalculator(polls.getVotesCount(), polls.getListImageObjects().get(3).getVotes()));
            tvProgressFour.setText(String.valueOf(pbProgressFour.getProgress()) + mContext.getString(R.string.percentage));

            GlideApp.with(mContext)
                    .asDrawable()
                    .load(polls.getListImageObjects().get(3).getUrlImageMainThumbnail())
                    .transforms(new CenterCrop(), new RoundedCorners(20))
                    .into(ivImageFour);
        }


    }
bindPastPolls()
来自
cardwiewholderpast

public void bindPastPolls(Poll polls, boolean isFirstPast) {


        if(isFirstPast) {
            vSeparator.setVisibility(View.VISIBLE);
            tvPastContestSeparator.setVisibility(View.VISIBLE);
        }

        int listImageSize = polls.getListImageObjects().size();
        //tvPollTitle.setText(polls.getStatus() + " - " + polls.getId());
        if(polls.getTitle() != null || !polls.getTitle().equals("")) {
            tvPollTitle.setText(polls.getTitle());
        } else {
            tvPollTitle.setText("");
        }
        tvCurrentVotes.setText(String.valueOf(polls.getVotesCount()) + "/");
        tvUpToVotes.setText(String.valueOf(polls.getMaxVotes()));

        pbProgressOne.setProgress(votePercentageCalculator(polls.getVotesCount(), polls.getListImageObjects().get(0).getVotes()));
        tvProgressOne.setText(String.valueOf(pbProgressOne.getProgress()) + mContext.getString(R.string.percentage));

        pbProgressTwo.setProgress(votePercentageCalculator(polls.getVotesCount(), polls.getListImageObjects().get(1).getVotes()));
        tvProgressTwo.setText(String.valueOf(pbProgressTwo.getProgress()) + mContext.getString(R.string.percentage));



        GlideApp.with(mContext)
                .asDrawable()
                .load(polls.getListImageObjects().get(0).getUrlImageMainThumbnail())
                .transforms(new CenterCrop(), new RoundedCorners(20))
                .into(ivImageOne);

        GlideApp.with(mContext)
                .asDrawable()
                .load(polls.getListImageObjects().get(1).getUrlImageMainThumbnail())
                .transforms(new CenterCrop(), new RoundedCorners(20))
                .into(ivImageTwo);

        if(listImageSize >= 3) {

            pbProgressThree.setProgress(votePercentageCalculator(polls.getVotesCount(), polls.getListImageObjects().get(2).getVotes()));
            tvProgressThree.setText(String.valueOf(pbProgressThree.getProgress()) + mContext.getString(R.string.percentage));

            GlideApp.with(mContext)
                    .asDrawable()
                    .load(polls.getListImageObjects().get(2).getUrlImageMainThumbnail())
                    .transforms(new CenterCrop(), new RoundedCorners(20))
                    .into(ivImageThree);
        }
        if(listImageSize >= 4) {

            pbProgressFour.setProgress(votePercentageCalculator(polls.getVotesCount(), polls.getListImageObjects().get(3).getVotes()));
            tvProgressFour.setText(String.valueOf(pbProgressFour.getProgress()) + mContext.getString(R.string.percentage));

            GlideApp.with(mContext)
                    .asDrawable()
                    .load(polls.getListImageObjects().get(3).getUrlImageMainThumbnail())
                    .transforms(new CenterCrop(), new RoundedCorners(20))
                    .into(ivImageFour);
        }

    }

更新:当我设置holder.setIsRecyclable(false)时,它工作正常,但我猜它正在失去RecycleView的主要用途?

几年前我也遇到了这个问题,因为如果API中有图像,我就使用glide,如果没有,我就没有设置图像。我添加了else语句来指示图像视图的src。如果希望为空,请设置一个与背景匹配的src。

如果仔细阅读,可能重复的src不会重复,但看起来类似。发布
bindActivePolls(poll)
bindPastPolls(poll,true)的代码bindVIewHolder看起来很奇怪。。。为什么要检查上一个项目的viewtype?您应该只依赖那里的
holder.getItemViewType()
。问题已更新。