wjc
2024-12-23 f753d8366041354da60b8096060f3ab5159e3880
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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().setText("");
        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;
    }
}