package com.example.photovoltaic.base; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.LinearLayout; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.lifecycle.Lifecycle; import com.example.photovoltaic.listener.BaseView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.Locale; public abstract class BaseFragment extends Fragment implements BaseView { private boolean isFirst = true; protected FragmentActivity _mActivity; protected View mContainerView; // private LoadingDialog loadingDialog; // private LoadingDialog loadingGetDataDialog; // private DeviceLoadingDialog deviceLoadingDialog; @Override public void onAttach(@NonNull Context context) { super.onAttach(context); _mActivity = getActivity(); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view; if (getContentView() instanceof Integer) { view = inflater.inflate((int) getContentView(), container, false); } else if (getContentView() instanceof View) { view = (View) getContentView(); FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); view.setLayoutParams(layoutParams); } else { view = null; } mContainerView = view; return view; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); view.postDelayed(new Runnable() { @Override public void run() { dispatchVisible(); } }, 200L); } @Override public void onResume() { super.onResume(); dispatchVisible(); } protected void dispatchVisible() { if (null != getParentFragment() && !getParentFragment().isResumed() && getParentFragment().isRemoving()) { return; } if (getLifecycle().getCurrentState() == Lifecycle.State.STARTED && isFirst) { if (getView() != null) { onLazyInitView(getArguments()); isFirst = false; } } } protected void onLazyInitView(Bundle savedInstanceState) { onBindView(savedInstanceState); // registerEventBus(); } @Override public void onDestroyView() { super.onDestroyView(); _mActivity = null; isFirst = true; // unregisterEventBus(); } protected void unregisterEventBus() { if (EventBus.getDefault().isRegistered(this)) { EventBus.getDefault().unregister(this); } } protected void registerEventBus() { if (!EventBus.getDefault().isRegistered(this)) { EventBus.getDefault().register(this); } } // @Subscribe(threadMode = ThreadMode.MAIN) // public void onEventMessage(BaseEvent event) { // // } /** * 显示View * * @param view */ public void setViewVisible(View view) { if (view.getVisibility() != View.VISIBLE && _mActivity != null) { view.setVisibility(View.VISIBLE); } } /** * 隐藏View * * @param view */ protected void setViewGone(View view) { if (view.getVisibility() != View.GONE && _mActivity != null) { view.setVisibility(View.GONE); } } /** * 简单的跳转Activity * * @param clazz */ protected void startActivity(Class clazz) { if (_mActivity != null) { Intent intent = new Intent(_mActivity, clazz); startActivity(intent); } } /* *//** * 获取LoadingDialog * * @return *//* protected LoadingDialog getLoadingDialog() { if (loadingDialog == null && _mActivity != null) { loadingDialog = new LoadingDialog(_mActivity, R.style.loading_dialog); } return loadingDialog; } public LoadingDialog getLoadingGetDataDialog() { if (loadingGetDataDialog == null && _mActivity != null) { loadingGetDataDialog = new LoadingDialog(_mActivity, R.style.loading_dialog); } return loadingGetDataDialog; } *//** * 获取DeviceLoadingDialog * * @return *//* protected DeviceLoadingDialog getDeviceLoadingDialog() { if (deviceLoadingDialog == null && _mActivity != null) { deviceLoadingDialog = new DeviceLoadingDialog(_mActivity, R.style.loading_dialog); } return deviceLoadingDialog; } *//** * 开始Loading *//* public void showLoading() { getLoadingDialog().start(); } *//** * 开始Loading *//* public void showDeviceLoading() { getDeviceLoadingDialog().start(); } *//** * 开始Loading *//* protected void showLoading(String mes) { getLoadingDialog().start(); getLoadingDialog().setText(mes); } *//** * 开始Loading *//* protected void showGetDataLoading(String mes) { getLoadingGetDataDialog().startTouchNotGone(); getLoadingGetDataDialog().setText(mes); } *//** * 开始Loading *//* protected void showDeviceLoading(String mes) { getDeviceLoadingDialog().start(); getDeviceLoadingDialog().setText(mes); } *//** * 停止隐藏Loading *//* protected void hideLoading() { if (loadingDialog != null && loadingDialog.isShowing()) { loadingDialog.stop(); } } *//** * 停止隐藏Loading *//* protected void hideGetDataLoading() { if (loadingGetDataDialog != null && loadingGetDataDialog.isShowing()) { loadingGetDataDialog.stop(); } } */ /** * 停止隐藏Loading *//* protected void hideDeviceLoading() { if (deviceLoadingDialog != null && deviceLoadingDialog.isShowing()) { deviceLoadingDialog.stop(); } }*/ //用于popwindow显示隐藏时候背景的颜色更换 protected void backgroundAlpha(float bgAlpha) { if (_mActivity != null) { WindowManager.LayoutParams lp = _mActivity.getWindow().getAttributes(); _mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); lp.alpha = bgAlpha; //0.0-1.0 _mActivity.getWindow().setAttributes(lp); } } public static boolean isZh(Context context) { Locale locale = context.getResources().getConfiguration().locale; String language = locale.getLanguage(); if (language.endsWith("zh")) return true; else return false; } }