package com.sheep.jiuyan.samllsheep.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.umeng.analytics.MobclickAgent;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  BaseFragment</p>
 * @ <p>Description:方便统计数据</p>
 * @ date:  2017/11/6 18:20
 * @ QQ:    315096953
 */

public abstract class BaseFragment extends Fragment {

    protected View mView;
    /**
     * 是否是第一次初始化，防止重复加载些东西用
     */
    protected boolean mIsFirstInit = true;

    protected ProgressDialog mPd;



    protected void  showProgress(){
        if(mPd!=null){
            hideProgress();
        }
        mPd = new ProgressDialog(getActivity());
//        View view= LayoutInflater.from(this).inflate(R.layout.dialog_layout,null);
//        mPd.setContentView(view);//自己定义布局
        mPd.setMessage("数据加载中...");
        mPd.setCancelable(true);//能否够被取消
//        mPd.setProgressStyle(ProgressDialog.STYLE_SPINNER);//圆环风格
        mPd.show();
    }

    protected void  showProgress(boolean cancel){
        if(mPd!=null){
            hideProgress();
        }
        mPd = new ProgressDialog(getActivity());
//        View view= LayoutInflater.from(this).inflate(R.layout.dialog_layout,null);
//        mPd.setContentView(view);//自己定义布局
        mPd.setMessage("数据加载中...");
        mPd.setCancelable(cancel);//能否够被取消
//        mPd.setProgressStyle(ProgressDialog.STYLE_SPINNER);//圆环风格
        mPd.show();
    }


    protected void  hideProgress(){
        if(mPd!=null){
            mPd.dismiss();
        }

    }




    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }





    /**
     * 注意用 mIsFirstInit 判断，防止重复加载
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mIsFirstInit = true;
            mView = inflater.inflate(getLayoutId(), null);
        } else {
            mIsFirstInit = false;
        }
        return mView;
    }

    public abstract int getLayoutId();

    public abstract void onViewCreated();

    /**
     * 一般要继承这个方法
     *
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onViewCreated();
    }


    /**
     * 万能的找View类
     *
     * @param id
     * @param <T>
     * @return
     */
    public <T extends View> T findViewById(@IdRes int id) {
        return (T)mView.findViewById(id);
    }

    /**
     * 万能的找View类
     *
     * @param id
     * @param tClass
     * @param <T>
     * @return
     */
    public <T extends View> T findViewById(@IdRes int id, Class<T> tClass) {
        return (T)mView.findViewById(id);
    }

}
