好比 我在BaseFreagment里面java
protected abstract void callbackTime(int time);//抽象方法
在BaseFragmentandroid
callbackTime(recLen);//赋值
最后我在我MyFragment里面:
你继承的时候 就必需要实现这个抽象类。
这个抽象类就是调用这个时间的方法ide
@Override protected void callbackTime(int time) { cd_time.setText(""+time); } }
BaseFragmentpost
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); } } }; }