BaseFragment.java 7 KB
package com.lijinji.part.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/**
 * @类名:BaseFragment
 * @类描述:碎片基类,所有fragment子类需继承此类
 * @修改人:
 * @修改时间:
 * @修改备注:
 * @版本:
 */
public abstract class BaseFragment extends Fragment {
    private Context context;// 上下文

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 注册EventBus
//		EventBus.getDefault().register(this);
        return layout(inflater);// 布局;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState == null) {// 数据初始化
            dataInit();
        } else {// 数据恢复
            dataRestore(savedInstanceState);
        }
        eventDispose();// 事件
    }

    /**
     * @param inflater
     * @return
     * @方法说明:布局
     * @方法名称:layout
     * @返回值:View
     */
    public abstract View layout(LayoutInflater inflater);

    /**
     * @方法说明:数据初始化
     * @方法名称:dataInit
     * @返回值:void
     */
    public void dataInit() {
    }

    @Override
    public void onResume() {
        super.onResume();
        resume();
    }

    public void resume() {
    }

    ;

    /**
     * @param savedInstanceState
     * @方法说明:数据恢复
     * @方法名称:dataRestore
     * @返回值:void
     */
    public void dataRestore(Bundle savedInstanceState) {
    }

    /**
     * @方法说明:处理
     * @方法名称:eventDispose
     * @返回值:void
     */
    public void eventDispose() {
    }

    /**
     * @return
     * @方法说明:获得上下文环境
     * @方法名称:getContext
     * @返回值:Context
     */
    public Context getContext() {
        return context;
    }

    /**
     * @param param
     * @方法说明:接收主activity传递的消息
     * @方法名称:receiverAttach
     * @返回值:void
     */
    public void receiverAttach(Object[] param) {
    }

    /**
     * @类名:OnHeadlineSelectedListener
     * @类描述:管理类事件触发
     */
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(Object[] param);
    }

    /**
     * @方法说明:界面刷新
     * @方法名称:uiRefrsh
     * @返回值:void
     */
    public void uiRefrsh() {
    }

    /**
     * @param keyCode
     * @param event
     * @return
     * @方法说明:按键处理
     * @方法名称:onKeyDown
     * @返回值:boolean
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//		EventBus.getDefault().unregister(this);
        releaseMemory();
    }

    /**
     * @方法说明:手动释放内存
     * @方法名称:releaseMemory
     * @返回值:void
     */
    public abstract void releaseMemory();

    /**
     * @param pClass
     * @方法说明:启动指定activity
     * @方法名称:openActivity
     * @返回值:void
     */
    public void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    /**
     * @param pClass
     * @param pBundle
     * @方法说明:启动到指定activity,Bundle传递对象(作用于2个界面之间传递数据)
     * @方法名称:openActivity
     * @返回值:void
     */
    public void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(this.getContext(), pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    /**
     * @param pAction
     * @方法说明:根据界面name启动到指定界面
     * @方法名称:openActivity
     * @返回值:void
     */
    public void openActivity(String pAction) {
        openActivity(pAction, null);
    }

    /**
     * @param pAction
     * @param pBundle
     * @方法说明:根据界面name启动到指定界面,Bundle传递对象(作用于2个界面之间传递数据)
     * @方法名称:openActivity
     * @返回值:void
     */
    public void openActivity(String pAction, Bundle pBundle) {
        Intent intent = new Intent(pAction);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    /**
     * @param pClass
     * @方法说明:A-Activity需要在B-Activtiy中执行一些数据操作,而B-Activity又要将,执行操作数据的结果返回给A-Activity
     * @方法名称:openActivityResult
     * @返回值:void
     */
    public void openActivityResult(Class<?> pClass) {
        openActivityResult(pClass, null);
    }

    /**
     * @param pClass
     * @param pBundle
     * @方法说明:A-Activity需要在B-Activtiy中执行一些数据操作, 而B-Activity又要将,执行操作数据的结果返回给A-Activity
     * , Bundle传递对象(作用于2个界面之间传递数据)
     * @方法名称:openActivityResult
     * @返回值:void
     */
    public void openActivityResult(Class<?> pClass, Bundle pBundle) {
        openActivityResult(pClass, pBundle, 0);
    }

    /**
     * @param pClass
     * @param pBundle
     * @param requestCode
     * @方法说明:A-Activity需要在B-Activtiy中执行一些数据操作, 而B-Activity又要将,执行操作数据的结果返回给A-Activity
     * , Bundle传递对象(作用于2个界面之间传递数据)
     * @方法名称:openActivityResult
     * @返回值:void
     */
    public void openActivityResult(Class<?> pClass, Bundle pBundle,
                                   int requestCode) {
        Intent intent = new Intent(this.getContext(), pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * @param pResId
     * @方法说明:获取string资源字段土司
     * @方法名称:showShortToast
     * @返回值:void
     */
    public void showShortToast(int pResId) {
        showShortToast(getString(pResId));
    }

    /**
     * @param pMsg
     * @方法说明:长时间土司
     * @方法名称:showLongToast
     * @返回值:void
     */
    public void showLongToast(String pMsg) {
        Toast.makeText(this.getContext(), pMsg, Toast.LENGTH_LONG).show();
    }

    /**
     * @param pMsg
     * @方法说明:短时间土司
     * @方法名称:showShortToast
     * @返回值:void
     */
    public void showShortToast(String pMsg) {
        Toast.makeText(this.getContext(), pMsg, Toast.LENGTH_SHORT).show();

    }
}