如何在倒计时结束时调用recyclerview加载Web服务?在这段代码中,getAllResturant("")
函数会连续自动调用。如果计时器在recyclerview适配器中完成,我只想调用一次。下面是我的代码:
TimerListAdapter(ArrayList<DashBoardListModel.Detail> resturantList,
Context context, String latitude, String longitude, long time) {
this.resturantList = resturantList;
this.mContext = context;
this.latitude = latitude;
this.longitude = longitude;
this.response_time = time;
lstHolders = new ArrayList<>();
startUpdateTimer();
}
private void startUpdateTimer() {
tmr.schedule(new TimerTask() {
@Override
public void run() {
mHandler.post(updateRemainingTimeRunnable);
}
}, 1000, 1000);
}
@NonNull
@Override
public ResturantDashboardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_item, parent, false);
return new ResturantDashboardViewHolder(v);
}
@Override
public void onViewRecycled(@NonNull ResturantDashboardViewHolder holder) {
super.onViewRecycled(holder);
}
@Override
public void onBindViewHolder(@NonNull final ResturantDashboardViewHolder holder, final int position) {
final DashBoardListModel.Detail resturantDetails = resturantList.get(position);
synchronized (lstHolders) {
lstHolders.add(holder);
}
holder.setData(resturantDetails);
}
@Override
public int getItemCount() {
if (6 > resturantList.size()) {
return resturantList.size();
} else {
return 6;
}
}
class ResturantDashboardViewHolder extends RecyclerView.ViewHolder {
TextView tv_resturantname, tvCloseAt, book_now, tvSeatLeft, tvSuburb, tvMinutes, tvSeconds, tvOutlets, tv_distance, tv_place, tv_rating, tvClosed, tvCostForTwo, tvOffer;
DashBoardListModel.Detail mProduct;
ResturantDashboardViewHolder(View itemView) {
super(itemView);
tvMinutes = itemView.findViewById(R.id.tvMinutes);
tvSeconds = itemView.findViewById(R.id.tvSeconds);
tvOutlets = itemView.findViewById(R.id.tvOutlets);
}
public void setData(DashBoardListModel.Detail resturantDetails) {
mProduct = resturantDetails;
if (!mProduct.getRemainingTime().equalsIgnoreCase("")) {
updateTimeRemaining(Long.parseLong(mProduct.getRemainingTime()));
}
}
private void updateTimeRemaining(long currentTime) {
if (!mProduct.getRemainingTime().equalsIgnoreCase("")) {
TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
Calendar calendar = Calendar.getInstance(timeZone);
String time = String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY)) + ":" +
String.format("%02d", calendar.get(Calendar.MINUTE)) + ":" +
String.format("%02d", calendar.get(Calendar.SECOND));
List<String> list = Arrays.asList(time.split(":"));
long hour = Integer.parseInt(list.get(0));
long min = Integer.parseInt(list.get(1));
long sec = Integer.parseInt(list.get(2));
long result = (hour * 60L * 60L * 1000L) + (min * 60L * 1000L) + (sec * 1000L);
long timeDiff = result - response_time;
timeDiff = 60000- timeDiff;
if (timeDiff > 0) {
int seconds = (int) (timeDiff / 1000) % 60;
int minutes = (int) ((timeDiff / (1000 * 60)) % 60);
tvMinutes.setText(String.format("%02d", minutes));
tvSeconds.setText(String.format("%02d", seconds));
} else {
tmr.cancel();
getAllResturant("");
}
}
}
}
}
答案 0 :(得分:0)
这可能不是最好的方法,但是您可以尝试
int count = 0;
if (timeDiff > 0) {
.....
} else {
if (count == 0) {
getAllResturant("");
count++
}
tmr.cancel();
}