我有recyclerview
circleprogreesbar
,显示完成调查所需的时间。
会议开始时间=下午1点 会议结束时间=下午2点
调查活动从下午2点到下午3点(仅1小时)
我的逻辑
剩余时间=调查结束时间 - 当前时间
我的计数器没有动态更新
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, MeetingSurveyDetailsActivity.class);
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(item.Status);
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) ));
prgCounter.setProgress(prgCounter.getProgress() + minutes);
//prgCounter.setSuffixText(""+minutes);
} else {
list.remove(getLayoutPosition());
notifyDataSetChanged();
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);
}
}, 1000, 1000);
}
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();
}
}
我的期望:
答案 0 :(得分:0)
尝试使用而不是
private void startUpdateTimer() {
Timer tmr = new Timer();
tmr.schedule(new TimerTask() {
@Override
public void run() {
mHandler.post(updateRemainingTimeRunnable);
}
}, 1000, 1000);
}
此
private Runnable updateRemainingTimeRunnable = new Runnable() {
@Override
public void run() {
synchronized (lstHolders) {
long currentTime = System.currentTimeMillis();
for (SurveyViewHolder holder : lstHolders) {
holder.updateTimeRemaining(currentTime);
}
}
mHandler.postDelayed(this, 1000);
}
};
......
private void startUpdateTimer() {
mHandler.postDelayed(updateRemainingTimeRunnable,1000);
}
看起来这个runnable只运行
或者还有一个解决方案: 在活动或片段中每1000毫秒调用adapter.notifyDataSetChange,并根据当前时间计算onBindViewHolder中剩余的时间