I know that this question has been asked several times, but the problem is still present. So the thing is that when i scroll my vertical RecyclerView
list items inside ViewHolder
are getting shuffled. I have list of some objects with boolean inside it deciding the ViewType
, so i can inflate various views. So here is the code:
@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;
}
}
So when i had:
@Override
public int getItemViewType(int position) {
return position;
}
the shuffling was gone, however i had to do binding stuff in onBindViewHolder
all manually, so i decided to switch to the viewTypes
. I really can't figure it out what is going on here, and it's pretty annoying... Hope i provided enough informations. Thanks in advance!
Here is the bindActivePolls
method from 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);
}
}
And bindPastPolls()
from 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);
}
}
Update: when i set holder.setIsRecyclable(false)
it works properly, but i guess it is losing its main purpose of RecyclerView?