抽象类方法-倒计时显示

2017-12-07 12:51:39来源:oschina作者:lsy999人点击

分享

比如 我在BaseFreagment里面


protected abstract void callbackTime(int time);//抽象方法

在BaseFragment


callbackTime(recLen);//赋值

最后我在我MyFragment里面: 你继承的时候 就必须要实现这个抽象类。 这个抽象类就是调用这个时间的方法


@Override
protected void callbackTime(int time) {
cd_time.setText(""+time);
}
}

BaseFragment


package com.hatai.smartlokey.base;import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.hatai.smartlokey.MainFragment;
import com.hatai.smartlokey.R;
import com.hatai.smartlokey.SplashActivity;
import java.util.Timer;
import java.util.TimerTask;
import me.yokeyword.fragmentation.SupportFragment;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;/**
* 所有的Fragment的基类
*/
public abstract class BaseFragment extends SwipeBackFragment
{
private int recLen = 60;
private TextView cd_time;
public static int FRAGMENTALL=0; //1.取件 2.寄件 3.快递员 4.管理员
public static int BOXALL=0; //1为寄件开箱子 2为快递员开箱子
protected abstract void callbackTime(int time);
protected void initToolbarNav(SupportFragment supportFragment)
{
getFragmentManager().popBackStack();
start(supportFragment);
}
@Override
public void startWithPop(SupportFragment toFragment) {
super.startWithPop(toFragment);
startWithPop(toFragment);
}
public void start(SupportFragment toFragment) {
start(toFragment, STANDARD);
}/**
* 启动定时器
*/
public int startTimer() {
//cd_time=view.findViewById(R.id.cd_time);
handler.postDelayed(runnable, 1000);
Log.e("startTimer","startTimer");
return recLen;
}
/**
* 停止定时器
*/
public void stopTimer() {
recLen=-1;
handler.removeCallbacks(runnable);
Log.e("stopTimer","stopTimer");
}
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
recLen--;
// cd_time.setText(recLen+"");
callbackTime(recLen);
Log.e("cd_time",recLen+"=");
if(recLen == 0){
handler.removeCallbacks(runnable);
start(MainFragment.newInstance());
Log.e("BaseFragment","BaseFragment=="+recLen);
}else if (recLen>0){
handler.postDelayed(this, 1000);
Log.e("BaseFragment","BaseFragment=="+recLen);
}else if (recLen<0){
handler.removeCallbacks(runnable);
Log.e("BaseFragment","BaseFragment=="+recLen);
}
}
};}

相关文章

    无相关信息

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台