我知道这个问题已经问过几次了,但是问题仍然存在。因此,问题是,当我滚动RecyclerView
内的垂直ViewHolder
列表项时,它们的位置会变得混乱。我有一些带有布尔值的对象列表,其中包含布尔值来决定ViewType
,因此我可以充实各种视图。所以这是代码:
@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
。我真的不知道这是怎么回事,这很烦人……希望我提供了足够的信息。预先感谢!
这是bindActivePolls
中的CardViewHolderActive
方法:
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()
中的CardViewHolderPast
:
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)
时,它可以正常工作,但是我想它正在失去RecyclerView的主要用途吗?
答案 0 :(得分:0)
几年前我也有这个问题,这是因为如果API中有图像,而我没有设置图像,那么我正在使用glide。我添加了else语句以指示图像视图的src。如果您希望保留空白,请设置与背景匹配的src。