package com.hdl.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.widget.FrameLayout; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.lifecycle.Lifecycle; import com.hdl.photovoltaic.R; import com.hdl.photovoltaic.listener.BaseView; import com.hdl.photovoltaic.other.HdlThreadLogic; import com.hdl.photovoltaic.widget.LoadingDialog; import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus; 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; @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(); ViewGroup.LayoutParams layoutParams = new ViewGroup.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.setClickable(true); 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); //注册EventBus registerEventBus(); } @Override public void onDestroyView() { super.onDestroyView(); _mActivity = null; isFirst = true; //隐藏Loading hideLoading(); //注销EventBus 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); } } /** * 收到EventBUs通知 * * @param eventBus 数据 */ @Subscribe(threadMode = ThreadMode.MAIN) public void onEventMessage(BaseEventBus eventBus) { } /** * 显示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 LoadingDialog */ protected LoadingDialog getLoadingDialog() { if (loadingDialog == null && _mActivity != null) { loadingDialog = new LoadingDialog(_mActivity, R.style.Custom_Dialog); } return loadingDialog; } /** * 开始Loading */ protected void showLoading() { getLoadingDialog().start(); } /** * 开始Loading * * @param mes 自定义文本 */ protected void showLoading(String mes) { getLoadingDialog().start(); getLoadingDialog().setText(mes); } /** * 停止隐藏Loading */ protected void hideLoading() { HdlThreadLogic.runMainThread(new Runnable() { @Override public void run() { if (loadingDialog != null && loadingDialog.isShowing()) { loadingDialog.stop(); } } }, null, null); } 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; } }