Recyclerview Changing Items During Scroll
Please try this
-
If you are using
ListView
- override the following methods.@Override public int getViewTypeCount() { return getCount(); } @Override public int getItemViewType(int position) { return position; }
-
If you are using
RecyclerView
- override onlygetItemViewType()
method.@Override public int getItemViewType(int position) { return position; }
Add setHasStableIds(true);
in your adapter constructor and Override these two methods in adapter. It also worked if anyone using a RecyclerView
inside a ViewPager
which is also inside a NestedScrollView
.
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
As the name implies, the views in a RecyclerView
are recycled as you scroll down. This means that you need to keep the state of each item in your backing model, which in this case would be a Historyitem
, and restore it in your onBindViewHolder
.
1) Create position, max, and whatever other variables you need to save the state of the ProgressBar
in your model.
2) Set the state of your ProgressBar
based on the data in your backing model; on click, pass the position of the item to your FileDownload
/StartMediaPlayer
methods.
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {
final Historyitem dataItem = stethitems.get(position);
final MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
myViewHolder.progressplay.setMax(dataItem.getMax());
myViewHolder.progressplay.setProgress(dataItem.getPosition());
...
myViewHolder.stethstreamplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FileDownload(dataItem.getmsg(), position);
}
});
3) Update the progress bar by updating the backing model and notifying that it was changed.
stethitems.get(position).setPosition(mPlayer.getCurrentPosition());
notifyItemChanged(position);