抽象類方法-倒計時顯示(一)

好比 我在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);
            }

        }
    };


}
相關文章
相關標籤/搜索