2016-04-12 37 views
1

我有一個recyclerview,它有circleprogreesbar,它顯示剩下的時間來完成調查。Recyclerview中的計數器(剩餘時間)android

會議開始時間=下午1時
會議結束時間= 2時

調查活性從= 2時至下午3時(僅1小時)

我的邏輯
剩餘時間=調查結束時間 - 當前時間

我的櫃檯不是動態更新

MeetingSurv eryAdapter.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(); 
     } 
} 

我的意料:

enter image description here

回答

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); 
     } 

看起來這可運行的運行唯一

或者還有正在一個更解決方案:每1000毫秒的活動或片段 呼叫adapter.notifyDataSetChange,並計算出留在車內onBindViewHolder時間取決於當前時間