我有一个recylerview,其中包含项目(包含调查时间的圆形进度条)
我使用后台线程将CircularProgress bar
更新为剩余时间
当我滚动Recyclerview
时,我得到 java.lang.IndexOutOfBoundsException:检测到不一致。无效的项目位置26(偏移量:26).state:30
MeetingSurveryAdapter.java
public class MeetingSurveryAdapter extends RecyclerView.Adapter<MeetingSurveryAdapter.SurveyViewHolder> {
private static final String TAG = "SurveryAdapter";
private List<MeetingSurveyModel> list;
MeetingSurveyModel model;
private Context mContext;
private int mSize;
private UtilMethods utilMethods;
private AppCompatActivity activity;
private int mPreviousPosition;
// Adapters
private ArrayAdapter<String> adapterCountry;
private final List<SurveyViewHolder> lstHolders;
private Handler mHandler = new Handler();
private Runnable updateRemainingTimeRunnable = new Runnable() {
@Override
public void run() {
synchronized (lstHolders) {
long currentTime = System.currentTimeMillis();
for (SurveyViewHolder holder : lstHolders) {
holder.updateTimeRemaining(currentTime);
}
}
}
};
public MeetingSurveryAdapter(Context mContext, List<MeetingSurveyModel> list) {
this.list = list;
this.mContext = mContext;
this.activity = (AppCompatActivity) mContext;
utilMethods = new UtilMethods(mContext);
lstHolders = new ArrayList<>();
startUpdateTimer();
}
@Override
public SurveyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_meeting_survey, viewGroup, false);
SurveyViewHolder viewHolder = new SurveyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final SurveyViewHolder surveyViewHolder, final int position) {
model = list.get(position);
surveyViewHolder.tvTitle.setText(model.Title);
surveyViewHolder.tvDate.setText(utilMethods.formatDateMeetingSurvey(model.MeetingDate));
surveyViewHolder.tvParticipantsCount.setText("Participants: " + model.Participants);
surveyViewHolder.setData(list.get(position));
synchronized (lstHolders) {
lstHolders.add(surveyViewHolder);
}
surveyViewHolder.updateTimeRemaining(System.currentTimeMillis());
surveyViewHolder.relativeTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openMettingSurveyList(surveyViewHolder, position);
}
});
if (position > mPreviousPosition) {
animate1(surveyViewHolder, true);
} else {
animate1(surveyViewHolder, false);
}
mPreviousPosition = position;
}
private void openMettingSurveyList(SurveyViewHolder surveyViewHolder, int position) {
MeetingSurveyModel model = list.get(position);
Intent intent = new Intent(mContext, NewActivity.class);
intent.putExtra(QrgSensContants.EMP_SURVEY_ID, model.ManualSurvey_Id);
activity.startActivity(intent);
}
@Override
public int getItemCount() {
return list.size();
}
public class SurveyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@Bind(R.id.relative_title)
RelativeLayout relativeTitle;
@Bind(R.id.img_number3)
ImageView imgNumber3;
@Bind(R.id.tv_title)
TextView tvTitle;
@Bind(R.id.tv_date)
TextView tvDate;
@Bind(R.id.tv_participants_count)
TextView tvParticipantsCount;
@Bind(R.id.prg_counter)
DonutProgress prgCounter;
@Bind(R.id.tv_counter)
TextView tvCounter;
public void setData(MeetingSurveyModel item) {
model = item;
prgCounter.setSuffixText("");
updateTimeRemaining(System.currentTimeMillis());
}
public void updateTimeRemaining(long currentTime) {
long timeDiff = utilMethods.getLongDate(model.MeetingDeadLine) - currentTime;
if (timeDiff > 0) {
//int minutes= Integer.parseInt(String.format("%d", TimeUnit.MILLISECONDS.toMinutes(timeDiff) ));
int minutes = (int) ((timeDiff / (1000 * 60)) % 60);
prgCounter.setProgress(minutes);
//prgCounter.setSuffixText(""+minutes);
} else {
list.remove(getLayoutPosition());
//notifyItemRemoved(getLayoutPosition());
tvCounter.setText("Expired!!");
//prgCounter.setSuffixText("0");
}
}
public SurveyViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
prgCounter.setTextSize(18);
prgCounter.setTextColor(mContext.getResources().getColor(R.color.black));
}
@Override
public void onClick(View view) {
switch (view.getId()) {
}
}
}
private void startUpdateTimer() {
Timer tmr = new Timer();
tmr.schedule(new TimerTask() {
@Override
public void run() {
mHandler.post(updateRemainingTimeRunnable);
}
}, 0, 60000);
}
public void animate1(RecyclerView.ViewHolder holder, boolean goesDown) {
int holderHeight = holder.itemView.getHeight();
holder.itemView.setPivotY(goesDown == true ? 0 : holderHeight);
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator animatorTranslateY = ObjectAnimator.ofFloat(holder.itemView, "translationY", goesDown == true ? 200 : 0, 0);
animatorTranslateY.setInterpolator(new AccelerateInterpolator());
animatorTranslateY.setDuration(700);
animatorTranslateY.start();
}
}