Android 如果recyclerview中的计时器启动,则仅从适配器调用函数一次?
如何在倒计时结束时调用recyclerview加载web服务?在此代码中,Android 如果recyclerview中的计时器启动,则仅从适配器调用函数一次?,android,time,countdowntimer,Android,Time,Countdowntimer,如何在倒计时结束时调用recyclerview加载web服务?在此代码中,getAllResturant(“”)函数连续自动调用。如果recyclerview适配器中的计时器完成,我只想调用它一次。下面是我的代码: TimerListAdapter(ArrayList<DashBoardListModel.Detail> resturantList, Context context, String latitude, Str
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("");
}
}
}
}
}
TimerListAdapter(ArrayList resturantList,
上下文、字符串纬度、字符串经度、长时间){
this.resturantList=resturantList;
this.mContext=上下文;
这个。纬度=纬度;
这个经度=经度;
这个.response_time=时间;
lstHolders=新的ArrayList();
startUpdateTimer();
}
私有void startUpdateTimer(){
tmr.时间表(新时间任务(){
@凌驾
公开募捐{
mHandler.post(updateMainingTimeRunnable);
}
}, 1000, 1000);
}
@非空
@凌驾
public ResturantDashboardViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.dashboard_项,parent,false);
归还新的ResturantDashboardViewHolder(v);
}
@凌驾
回收视图上的公共无效(@NonNull ResturantDashboardViewHolder){
super.onViewRecycled(支架);
}
@凌驾
BindViewHolder上的公共无效(@NonNull final ResturantDashboardViewHolder,final int位置){
最终仪表板列表model.Detail resturantDetails=resturantList.get(位置);
已同步(LST){
增加(持有人);
}
holder.setData(resturantDetails);
}
@凌驾
public int getItemCount(){
如果(6>resturantList.size()){
return resturantList.size();
}否则{
返回6;
}
}
类ResturantDashboardViewHolder扩展了RecyclerView.ViewHolder{
TextView tv_resturantname、tvCloseAt、book_now、tvSeatLeft、tv郊区、tvMinutes、tvSeconds、tvOutlets、TVuDistance、TVuPlace、TVuRating、tvClosed、tvCostForTwo、tvOffer;
仪表板列表模型。详细信息MPProduct;
ResturantDashboardViewHolder(查看项目视图){
超级(项目视图);
tvMinutes=itemView.findViewById(R.id.tvMinutes);
tvSeconds=itemView.findViewById(R.id.tvSeconds);
tvouts=itemView.findviewbyd(R.id.tvouts);
}
public void setData(仪表板ListModel.Detail resturantDetails){
MPProduct=餐厅详情;
如果(!MPProduct.getRemainingTime().equalsIgnoreCase(“”){
UpdateTimeRaining(Long.parseLong(mpproduct.getRemainingTime());
}
}
私有void updateTimeRaining(长currentTime){
如果(!MPProduct.getRemainingTime().equalsIgnoreCase(“”){
时区TimeZone=TimeZone.getTimeZone(“GMT+8”);
Calendar Calendar=Calendar.getInstance(时区);
String time=String.format(“%02d”,calendar.get(calendar.HOUR\u OF_DAY))+“:”+
String.format(“%02d”,calendar.get(calendar.MINUTE))+“:”+
格式(“%02d”,calendar.get(calendar.SECOND));
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));
长结果=(小时*60L*60L*1000L)+(分钟*60L*1000L)+(秒*1000L);
长时间差=结果-响应时间;
timeDiff=60000-timeDiff;
如果(时间差>0){
整数秒=(整数)(时间差/1000)%60;
int分钟=(int)((timeDiff/(1000*60))%60);
setText(String.format(“%02d”,分钟));
setText(String.format(“%02d”,秒));
}否则{
tmr.cancel();
getAllResturant(“”);
}
}
}
}
}
这可能不是最好的方法,但您可以尝试一下
int count = 0;
if (timeDiff > 0) {
.....
} else {
if (count == 0) {
getAllResturant("");
count++
}
tmr.cancel();
}
if(timeDiff==0)
?getAllResturant(“”);此函数正在连续调用,这是在else Condition中定义的。如何仅调用一次?您可以添加布尔值
。如果调用了getAllResturant
,请将boolean
设置为true,并使用If-else
条件进行检查。你能给我你的解决方案作为答案,这样我就可以投票@johnjoe为什么你在两个分支中都调用时间的cancel方法,而不是将其移出If?@Zoe说得通。Thanks第一次工作,之后在剩余4秒时调用函数@johnjoe将计数设置为全局