Commit 9d201a4dbb651c8edee7a166a776537cba45fb03
1 parent
9be4582669
Exists in
master
避免编译错误
Showing
1 changed file
with
3 additions
and
3 deletions
Show diff stats
libs/common/src/main/java/com/prws/common/base/BaseActivity.java
1 | package com.prws.common.base; | 1 | package com.prws.common.base; |
2 | 2 | ||
3 | 3 | ||
4 | import android.app.Activity; | 4 | import android.app.Activity; |
5 | import android.app.AlertDialog; | 5 | import android.app.AlertDialog; |
6 | import android.content.Context; | 6 | import android.content.Context; |
7 | import android.content.DialogInterface; | 7 | import android.content.DialogInterface; |
8 | import android.content.Intent; | 8 | import android.content.Intent; |
9 | import android.graphics.Color; | 9 | import android.graphics.Color; |
10 | import android.os.Bundle; | 10 | import android.os.Bundle; |
11 | import android.os.IBinder; | 11 | import android.os.IBinder; |
12 | import android.text.TextUtils; | 12 | import android.text.TextUtils; |
13 | import android.util.Log; | 13 | import android.util.Log; |
14 | import android.view.MotionEvent; | 14 | import android.view.MotionEvent; |
15 | import android.view.View; | 15 | import android.view.View; |
16 | import android.view.Window; | 16 | import android.view.Window; |
17 | import android.view.WindowManager; | 17 | import android.view.WindowManager; |
18 | import android.view.inputmethod.InputMethodManager; | 18 | import android.view.inputmethod.InputMethodManager; |
19 | import android.widget.EditText; | 19 | import android.widget.EditText; |
20 | import android.widget.Toast; | 20 | import android.widget.Toast; |
21 | 21 | ||
22 | import androidx.annotation.IdRes; | 22 | import androidx.annotation.IdRes; |
23 | import androidx.annotation.Nullable; | 23 | import androidx.annotation.Nullable; |
24 | import androidx.appcompat.app.AppCompatActivity; | 24 | import androidx.appcompat.app.AppCompatActivity; |
25 | import androidx.fragment.app.Fragment; | 25 | import androidx.fragment.app.Fragment; |
26 | import androidx.fragment.app.FragmentTransaction; | 26 | import androidx.fragment.app.FragmentTransaction; |
27 | import androidx.lifecycle.Lifecycle; | 27 | import androidx.lifecycle.Lifecycle; |
28 | 28 | ||
29 | import com.prws.common.R; | 29 | import com.prws.common.R; |
30 | import com.prws.common.net.INetEvent; | 30 | import com.prws.common.net.INetEvent; |
31 | import com.prws.common.utils.LogUtil; | 31 | import com.prws.common.utils.LogUtil; |
32 | import com.prws.common.utils.ScreenUtils; | 32 | import com.prws.common.utils.ScreenUtils; |
33 | import com.prws.common.view.LoadingView; | 33 | import com.prws.common.view.LoadingView; |
34 | 34 | ||
35 | import org.simple.eventbus.EventBus; | 35 | import org.simple.eventbus.EventBus; |
36 | 36 | ||
37 | import java.util.ArrayList; | 37 | import java.util.ArrayList; |
38 | import java.util.List; | 38 | import java.util.List; |
39 | 39 | ||
40 | import butterknife.ButterKnife; | 40 | import butterknife.ButterKnife; |
41 | import io.reactivex.disposables.CompositeDisposable; | 41 | import io.reactivex.disposables.CompositeDisposable; |
42 | import io.reactivex.disposables.Disposable; | 42 | import io.reactivex.disposables.Disposable; |
43 | 43 | ||
44 | 44 | ||
45 | /** | 45 | /** |
46 | * @author 描述:BaseActivity | 46 | * @author 描述:BaseActivity |
47 | */ | 47 | */ |
48 | public abstract class BaseActivity<P extends BasePresenter, CONTRACT> extends AppCompatActivity implements INetEvent, BaseView { | 48 | public abstract class BaseActivity<P extends BasePresenter, CONTRACT> extends AppCompatActivity implements INetEvent, BaseView { |
49 | 49 | ||
50 | 50 | ||
51 | private LoadingView loading; | 51 | private LoadingView loading; |
52 | 52 | ||
53 | public static INetEvent mINetEvent; | 53 | public static INetEvent mINetEvent; |
54 | protected final String TAG = getClass().getSimpleName(); | 54 | protected final String TAG = getClass().getSimpleName(); |
55 | private CompositeDisposable mCompositeDisposable; | 55 | private CompositeDisposable mCompositeDisposable; |
56 | protected P p; | 56 | protected P p; |
57 | 57 | ||
58 | /** | 58 | /** |
59 | * 获取布局文件 | 59 | * 获取布局文件 |
60 | * | 60 | * |
61 | * @return | 61 | * @return |
62 | */ | 62 | */ |
63 | protected abstract int layoutResId(); | 63 | protected abstract int layoutResId(); |
64 | 64 | ||
65 | /** | 65 | /** |
66 | * 让P层做相应需求 | 66 | * 让P层做相应需求 |
67 | */ | 67 | */ |
68 | public abstract CONTRACT getContract(); | 68 | public abstract CONTRACT getContract(); |
69 | 69 | ||
70 | /** | 70 | /** |
71 | * 子类获取具体契约 | 71 | * 子类获取具体契约 |
72 | */ | 72 | */ |
73 | public abstract P getPresenter(); | 73 | public abstract P getPresenter(); |
74 | 74 | ||
75 | @Override | 75 | @Override |
76 | protected void onCreate(@Nullable Bundle savedInstanceState) { | 76 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
77 | super.onCreate(savedInstanceState); | 77 | super.onCreate(savedInstanceState); |
78 | setNetEvent(); | 78 | setNetEvent(); |
79 | initPresenter(); | 79 | initPresenter(); |
80 | showWhiteStatus(); | 80 | showWhiteStatus(); |
81 | setContentView(layoutResId()); | 81 | setContentView(layoutResId()); |
82 | ButterKnife.bind(this); | 82 | ButterKnife.bind(this); |
83 | registerEventBus(); | 83 | registerEventBus(); |
84 | initView(); | 84 | initView(); |
85 | initListener(); | 85 | initListener(); |
86 | initData(); | 86 | initData(); |
87 | 87 | ||
88 | } | 88 | } |
89 | 89 | ||
90 | 90 | ||
91 | public void showWhiteStatus() { | 91 | public void showWhiteStatus() { |
92 | Window window = getWindow(); | 92 | Window window = getWindow(); |
93 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | 93 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
94 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); | 94 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
95 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); | 95 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
96 | window.setStatusBarColor(Color.TRANSPARENT); | 96 | window.setStatusBarColor(Color.TRANSPARENT); |
97 | } | 97 | } |
98 | 98 | ||
99 | 99 | ||
100 | public void fullScreen(){ | 100 | public void fullScreen(){ |
101 | Window window = getWindow(); | 101 | Window window = getWindow(); |
102 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | 102 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
103 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | 103 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
104 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); | 104 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); |
105 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); | 105 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
106 | window.setStatusBarColor(Color.TRANSPARENT); | 106 | window.setStatusBarColor(Color.TRANSPARENT); |
107 | } | 107 | } |
108 | 108 | ||
109 | protected void blackBar() { | 109 | protected void blackBar() { |
110 | Window window = getWindow(); | 110 | Window window = getWindow(); |
111 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | 111 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
112 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | 112 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
113 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); | 113 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
114 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); | 114 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
115 | window.setStatusBarColor(Color.TRANSPARENT); | 115 | window.setStatusBarColor(Color.TRANSPARENT); |
116 | } | 116 | } |
117 | 117 | ||
118 | @Override | 118 | @Override |
119 | protected void onResume() { | 119 | protected void onResume() { |
120 | super.onResume(); | 120 | super.onResume(); |
121 | } | 121 | } |
122 | 122 | ||
123 | protected abstract void initView(); | 123 | protected abstract void initView(); |
124 | 124 | ||
125 | protected abstract void initListener(); | 125 | protected abstract void initListener(); |
126 | 126 | ||
127 | protected abstract void initData(); | 127 | protected abstract void initData(); |
128 | 128 | ||
129 | 129 | ||
130 | private void setNetEvent() { | 130 | private void setNetEvent() { |
131 | mINetEvent = this; | 131 | mINetEvent = this; |
132 | } | 132 | } |
133 | 133 | ||
134 | private void initPresenter() { | 134 | private void initPresenter() { |
135 | p = getPresenter(); | 135 | p = getPresenter(); |
136 | if (p != null) | 136 | if (p != null) |
137 | p.bindView(this); | 137 | p.bindView(this); |
138 | } | 138 | } |
139 | 139 | ||
140 | 140 | ||
141 | public void showDialog(String title, String message, String confirm, String cancel, DialogInterface.OnClickListener cancelListener, DialogInterface.OnClickListener listener) { | 141 | public void showDialog(String title, String message, String confirm, String cancel, DialogInterface.OnClickListener cancelListener, DialogInterface.OnClickListener listener) { |
142 | AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert); | 142 | AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert); |
143 | builder.setTitle(title); | 143 | builder.setTitle(title); |
144 | builder.setMessage(message); | 144 | builder.setMessage(message); |
145 | builder.setCancelable(false); | 145 | builder.setCancelable(false); |
146 | if (!TextUtils.isEmpty(cancel)) { | 146 | if (!TextUtils.isEmpty(cancel)) { |
147 | builder.setNegativeButton(cancel, cancelListener); | 147 | builder.setNegativeButton(cancel, cancelListener); |
148 | } | 148 | } |
149 | builder.setPositiveButton(confirm, listener); | 149 | builder.setPositiveButton(confirm, listener); |
150 | builder.show(); | 150 | builder.show(); |
151 | } | 151 | } |
152 | 152 | ||
153 | private void setWindow() { | 153 | private void setWindow() { |
154 | Window window = getWindow(); | 154 | Window window = getWindow(); |
155 | WindowManager.LayoutParams params = window.getAttributes(); | 155 | WindowManager.LayoutParams params = window.getAttributes(); |
156 | params.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN | 156 | params.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN |
157 | ; | 157 | ; |
158 | window.setAttributes(params); | 158 | window.setAttributes(params); |
159 | } | 159 | } |
160 | 160 | ||
161 | 161 | ||
162 | // public void openFloatWindow() { | 162 | // public void openFloatWindow() { |
163 | // if (FloatWindow.get("navigation") == null) { | 163 | // if (FloatWindow.get("navigation") == null) { |
164 | // NavigationBarView navigationBarView = new NavigationBarView(this); | 164 | // NavigationBarView navigationBarView = new NavigationBarView(this); |
165 | // FloatWindow.with(CommonApplication.getAppContext()) | 165 | // FloatWindow.with(CommonApplication.getAppContext()) |
166 | // .setView(navigationBarView) | 166 | // .setView(navigationBarView) |
167 | // .setWidth(Screen.width, 1) | 167 | // .setWidth(Screen.width, 1) |
168 | // .setHeight(WindowManager.LayoutParams.WRAP_CONTENT) | 168 | // .setHeight(WindowManager.LayoutParams.WRAP_CONTENT) |
169 | // .setX(0) | 169 | // .setX(0) |
170 | // .setY(0) | 170 | // .setY(0) |
171 | // .setMoveType(MoveType.inactive) | 171 | // .setMoveType(MoveType.inactive) |
172 | // .setDesktopShow(false) | 172 | // .setDesktopShow(false) |
173 | // .setTag("navigation") | 173 | // .setTag("navigation") |
174 | // .build(); | 174 | // .build(); |
175 | // } | 175 | // } |
176 | // if (!FloatWindow.get("navigation").isShowing()) | 176 | // if (!FloatWindow.get("navigation").isShowing()) |
177 | // FloatWindow.get("navigation").show(); | 177 | // FloatWindow.get("navigation").show(); |
178 | // | 178 | // |
179 | // } | 179 | // } |
180 | 180 | ||
181 | 181 | ||
182 | public void showAlertDialogOfFloatWindow() { | 182 | public void showAlertDialogOfFloatWindow() { |
183 | //弹窗提示用户开启权限 | 183 | //弹窗提示用户开启权限 |
184 | // new AlertDialog(this).builder().setTitle(getString(R.string.need_permission)) | 184 | // new AlertDialog(this).builder().setTitle(getString(R.string.need_permission)) |
185 | // .setMsg(getString(R.string.need_permission_window)) | 185 | // .setMsg(getString(R.string.need_permission_window)) |
186 | // .setPositiveButton(getString(R.string.open), new View.OnClickListener() { | 186 | // .setPositiveButton(getString(R.string.open), new View.OnClickListener() { |
187 | // @Override | 187 | // @Override |
188 | // public void onClick(View v) { | 188 | // public void onClick(View v) { |
189 | // Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); | 189 | // Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); |
190 | // intent.setData(Uri.parse("package:" + getPackageName())); | 190 | // intent.setData(Uri.parse("package:" + getPackageName())); |
191 | // startActivity(intent); | 191 | // startActivity(intent); |
192 | // } | 192 | // } |
193 | // }).setNegativeButton(getString(R.string.cancel), new View.OnClickListener() { | 193 | // }).setNegativeButton(getString(R.string.cancel), new View.OnClickListener() { |
194 | // @Override | 194 | // @Override |
195 | // public void onClick(View v) { | 195 | // public void onClick(View v) { |
196 | // } | 196 | // } |
197 | // }).show(); | 197 | // }).show(); |
198 | LogUtil.toast("请开启浮窗权限"); | 198 | LogUtil.toast("请开启浮窗权限"); |
199 | } | 199 | } |
200 | 200 | ||
201 | 201 | ||
202 | public void showLoading(String title) { | 202 | public void showLoading(String title) { |
203 | synchronized (this) { | 203 | synchronized (this) { |
204 | if (loading != null || !isLiving(this)) return; | 204 | if (loading != null || !isLiving(this)) return; |
205 | runOnUiThread(() -> { | 205 | runOnUiThread(() -> { |
206 | loading = new LoadingView(this, R.style.CustomDialog, title); | 206 | loading = new LoadingView(this, R.style.CustomDialog, title); |
207 | loading.show(); | 207 | loading.show(); |
208 | ScreenUtils.hideBottomUIMenu(this); | 208 | ScreenUtils.hideBottomUIMenu(this); |
209 | ScreenUtils.hideDialogBottomUIMenu(loading); | 209 | ScreenUtils.hideDialogBottomUIMenu(loading); |
210 | }); | 210 | }); |
211 | } | 211 | } |
212 | } | 212 | } |
213 | 213 | ||
214 | 214 | ||
215 | public void setCanAble(boolean canAble) { | 215 | public void setCanAble(boolean canAble) { |
216 | if (loading != null) | 216 | if (loading != null) |
217 | loading.setCancelable(canAble); | 217 | loading.setCancelable(canAble); |
218 | 218 | ||
219 | } | 219 | } |
220 | 220 | ||
221 | public boolean isShowLoading() { | 221 | public boolean isShowLoading() { |
222 | if (loading == null) return false; | 222 | if (loading == null) return false; |
223 | return loading.isShowing(); | 223 | return loading.isShowing(); |
224 | } | 224 | } |
225 | 225 | ||
226 | public void dismissLoading() { | 226 | public void dismissLoading() { |
227 | runOnUiThread(() -> { | 227 | runOnUiThread(() -> { |
228 | if (loading == null || loading.isShowing() == false || !isLiving(this)) { | 228 | if (loading == null || loading.isShowing() == false || !isLiving(this)) { |
229 | Log.w(TAG, "loading == null" + | 229 | Log.w(TAG, "loading == null" + |
230 | " || loading.isShowing() == false >> return;"); | 230 | " || loading.isShowing() == false >> return;"); |
231 | return; | 231 | return; |
232 | } | 232 | } |
233 | loading.dismiss(); | 233 | loading.dismiss(); |
234 | loading = null; | 234 | loading = null; |
235 | 235 | ||
236 | }); | 236 | }); |
237 | } | 237 | } |
238 | 238 | ||
239 | 239 | ||
240 | private static boolean isLiving(Activity activity) { | 240 | private static boolean isLiving(Activity activity) { |
241 | 241 | ||
242 | if (activity == null) { | 242 | if (activity == null) { |
243 | Log.d("wisely", "activity == null"); | 243 | Log.d("wisely", "activity == null"); |
244 | return false; | 244 | return false; |
245 | } | 245 | } |
246 | 246 | ||
247 | if (activity.isFinishing()) { | 247 | if (activity.isFinishing()) { |
248 | Log.d("wisely", "activity is finishing"); | 248 | Log.d("wisely", "activity is finishing"); |
249 | return false; | 249 | return false; |
250 | } | 250 | } |
251 | 251 | ||
252 | return true; | 252 | return true; |
253 | } | 253 | } |
254 | 254 | ||
255 | 255 | ||
256 | /** | 256 | /** |
257 | * 是否需要弹窗提示更新 | 257 | * 是否需要弹窗提示更新 |
258 | */ | 258 | */ |
259 | protected void isNeedGetVersionUpdate() { | 259 | protected void isNeedGetVersionUpdate() { |
260 | // View view = findViewById(R.id.view_loading); | 260 | // View view = findViewById(R.id.view_loading); |
261 | //如果显示正在下载的页面,则不弹出更新弹窗 | 261 | //如果显示正在下载的页面,则不弹出更新弹窗 |
262 | // if (null == view || view.getVisibility() == View.VISIBLE) return; | 262 | // if (null == view || view.getVisibility() == View.VISIBLE) return; |
263 | // UpdateUtils.getVersion(this); | 263 | // UpdateUtils.getVersion(this); |
264 | } | 264 | } |
265 | 265 | ||
266 | 266 | ||
267 | /** | 267 | /** |
268 | * 全局检测网络广播的回调 处理网络变化 | 268 | * 全局检测网络广播的回调 处理网络变化 |
269 | * 注:在程序第一次启动的时候,没网并不会回调,需要自己在启动页面,或者主页自己再判断一次 | 269 | * 注:在程序第一次启动的时候,没网并不会回调,需要自己在启动页面,或者主页自己再判断一次 |
270 | * | 270 | * |
271 | * @param netWorkState 网络状态 -1:没网络 0:移动网络 1:WiFi网络 | 271 | * @param netWorkState 网络状态 -1:没网络 0:移动网络 1:WiFi网络 |
272 | */ | 272 | */ |
273 | public abstract void onNetChanged(int netWorkState); | 273 | public abstract void onNetChanged(int netWorkState); |
274 | 274 | ||
275 | @Override | 275 | @Override |
276 | public void onNetChange(int netWorkState) { | 276 | public void onNetChange(int netWorkState) { |
277 | onNetChanged(netWorkState); | 277 | onNetChanged(netWorkState); |
278 | } | 278 | } |
279 | 279 | ||
280 | @Override | 280 | @Override |
281 | protected void onDestroy() { | 281 | protected void onDestroy() { |
282 | super.onDestroy(); | 282 | super.onDestroy(); |
283 | unSubscribe(); | 283 | unSubscribe(); |
284 | cleanFragment(); | 284 | cleanFragment(); |
285 | unregisterEventBus(); | 285 | unregisterEventBus(); |
286 | if (p != null) { | 286 | if (p != null) { |
287 | p.onBindView(); | 287 | p.onBindView(); |
288 | p.cancelAllRequest(); | 288 | p.cancelAllRequest(); |
289 | } | 289 | } |
290 | } | 290 | } |
291 | 291 | ||
292 | @Override | 292 | @Override |
293 | protected void onPause() { | 293 | protected void onPause() { |
294 | super.onPause(); | 294 | super.onPause(); |
295 | 295 | ||
296 | } | 296 | } |
297 | 297 | ||
298 | private void registerEventBus() { | 298 | private void registerEventBus() { |
299 | EventBus.getDefault().register(this); | 299 | EventBus.getDefault().register(this); |
300 | } | 300 | } |
301 | 301 | ||
302 | private void unregisterEventBus() { | 302 | private void unregisterEventBus() { |
303 | EventBus.getDefault().unregister(this); | 303 | EventBus.getDefault().unregister(this); |
304 | } | 304 | } |
305 | 305 | ||
306 | public void showErrorToast() { | 306 | public void showErrorToast() { |
307 | // LogUtil.toast(MainApplication.getAppContext(), getString(R.string.service_error)); | 307 | // LogUtil.toast(MainApplication.getAppContext(), getString(R.string.service_error)); |
308 | } | 308 | } |
309 | 309 | ||
310 | public void showToast(String s) { | 310 | public void showToast(String s) { |
311 | Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show(); | 311 | Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show(); |
312 | } | 312 | } |
313 | 313 | ||
314 | private void unSubscribe() { | 314 | private void unSubscribe() { |
315 | if (mCompositeDisposable != null) { | 315 | if (mCompositeDisposable != null) { |
316 | mCompositeDisposable.clear(); | 316 | mCompositeDisposable.clear(); |
317 | } | 317 | } |
318 | } | 318 | } |
319 | 319 | ||
320 | protected void addSubscribe(Disposable disposable) { | 320 | protected void addSubscribe(Disposable disposable) { |
321 | if (mCompositeDisposable == null) { | 321 | if (mCompositeDisposable == null) { |
322 | mCompositeDisposable = new CompositeDisposable(); | 322 | mCompositeDisposable = new CompositeDisposable(); |
323 | } | 323 | } |
324 | mCompositeDisposable.add(disposable); | 324 | mCompositeDisposable.add(disposable); |
325 | } | 325 | } |
326 | 326 | ||
327 | protected void startActivity(Class clazz) { | 327 | protected void startActivity(Class clazz) { |
328 | startActivity(new Intent(this, clazz)); | 328 | startActivity(new Intent(this, clazz)); |
329 | } | 329 | } |
330 | 330 | ||
331 | /******************** 处理fragment *******************************/ | 331 | /******************** 处理fragment *******************************/ |
332 | List<Fragment> fragmentList = new ArrayList<>(); | 332 | List<Fragment> fragmentList = new ArrayList<>(); |
333 | 333 | ||
334 | protected void showFragment(Fragment fragment, @IdRes int id) { | 334 | protected void showFragment(Fragment fragment, @IdRes int id) { |
335 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); | 335 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); |
336 | 336 | ||
337 | if (!fragmentList.contains(fragment)) { | 337 | if (!fragmentList.contains(fragment)) { |
338 | fragmentList.add(fragment); | 338 | fragmentList.add(fragment); |
339 | fragmentTransaction.add(id, fragment, fragment.getClass().getSimpleName()).setMaxLifecycle(fragment, Lifecycle.State.RESUMED); | 339 | fragmentTransaction.add(id, fragment, fragment.getClass().getSimpleName()); |
340 | } | 340 | } |
341 | 341 | ||
342 | fragmentTransaction.show(fragment).setMaxLifecycle(fragment, Lifecycle.State.RESUMED); | 342 | fragmentTransaction.show(fragment); |
343 | 343 | ||
344 | for (Fragment fragment1 : fragmentList) { | 344 | for (Fragment fragment1 : fragmentList) { |
345 | if (fragment1 != fragment) { | 345 | if (fragment1 != fragment) { |
346 | fragmentTransaction.hide(fragment1).setMaxLifecycle(fragment1, Lifecycle.State.STARTED); | 346 | fragmentTransaction.hide(fragment1); |
347 | } | 347 | } |
348 | } | 348 | } |
349 | 349 | ||
350 | fragmentTransaction.commitAllowingStateLoss(); | 350 | fragmentTransaction.commitAllowingStateLoss(); |
351 | 351 | ||
352 | } | 352 | } |
353 | 353 | ||
354 | protected void cleanFragment() { | 354 | protected void cleanFragment() { |
355 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); | 355 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); |
356 | if (fragmentList.size() > 0) { | 356 | if (fragmentList.size() > 0) { |
357 | for (Fragment fragment : fragmentList) { | 357 | for (Fragment fragment : fragmentList) { |
358 | fragmentTransaction.remove(fragment); | 358 | fragmentTransaction.remove(fragment); |
359 | } | 359 | } |
360 | try { | 360 | try { |
361 | fragmentTransaction.commitNowAllowingStateLoss(); | 361 | fragmentTransaction.commitNowAllowingStateLoss(); |
362 | } catch (Exception e) { | 362 | } catch (Exception e) { |
363 | e.printStackTrace(); | 363 | e.printStackTrace(); |
364 | } | 364 | } |
365 | } | 365 | } |
366 | fragmentList.clear(); | 366 | fragmentList.clear(); |
367 | } | 367 | } |
368 | 368 | ||
369 | //************************* 用于隐藏键盘 ***************************************// | 369 | //************************* 用于隐藏键盘 ***************************************// |
370 | @Override | 370 | @Override |
371 | public boolean dispatchTouchEvent(MotionEvent ev) { | 371 | public boolean dispatchTouchEvent(MotionEvent ev) { |
372 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { | 372 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
373 | 373 | ||
374 | View v = getCurrentFocus(); | 374 | View v = getCurrentFocus(); |
375 | if (isShouldHideInput(v, ev)) { | 375 | if (isShouldHideInput(v, ev)) { |
376 | hideSoftInput(v.getWindowToken()); | 376 | hideSoftInput(v.getWindowToken()); |
377 | } | 377 | } |
378 | } | 378 | } |
379 | return super.dispatchTouchEvent(ev); | 379 | return super.dispatchTouchEvent(ev); |
380 | } | 380 | } |
381 | 381 | ||
382 | private boolean isShouldHideInput(View v, MotionEvent event) { | 382 | private boolean isShouldHideInput(View v, MotionEvent event) { |
383 | if (v != null && (v instanceof EditText)) { | 383 | if (v != null && (v instanceof EditText)) { |
384 | int[] l = {0, 0}; | 384 | int[] l = {0, 0}; |
385 | v.getLocationInWindow(l); | 385 | v.getLocationInWindow(l); |
386 | int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left | 386 | int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left |
387 | + v.getWidth(); | 387 | + v.getWidth(); |
388 | if (event.getX() > left && event.getX() < right | 388 | if (event.getX() > left && event.getX() < right |
389 | && event.getY() > top && event.getY() < bottom) { | 389 | && event.getY() > top && event.getY() < bottom) { |
390 | // 点击EditText的事件,忽略它。 | 390 | // 点击EditText的事件,忽略它。 |
391 | return false; | 391 | return false; |
392 | } else { | 392 | } else { |
393 | return true; | 393 | return true; |
394 | } | 394 | } |
395 | } | 395 | } |
396 | 396 | ||
397 | return false; | 397 | return false; |
398 | } | 398 | } |
399 | 399 | ||
400 | /** | 400 | /** |
401 | * 多种隐藏软件盘方法的其中一种 | 401 | * 多种隐藏软件盘方法的其中一种 |
402 | * | 402 | * |
403 | * @param token | 403 | * @param token |
404 | */ | 404 | */ |
405 | private void hideSoftInput(IBinder token) { | 405 | private void hideSoftInput(IBinder token) { |
406 | LogUtil.i(TAG, "隐藏键盘"); | 406 | LogUtil.i(TAG, "隐藏键盘"); |
407 | if (token != null) { | 407 | if (token != null) { |
408 | InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); | 408 | InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); |
409 | im.hideSoftInputFromWindow(token, | 409 | im.hideSoftInputFromWindow(token, |
410 | InputMethodManager.HIDE_NOT_ALWAYS); | 410 | InputMethodManager.HIDE_NOT_ALWAYS); |
411 | } | 411 | } |
412 | } | 412 | } |
413 | 413 | ||
414 | //************************* 用于隐藏键盘 end***************************************// | 414 | //************************* 用于隐藏键盘 end***************************************// |
415 | 415 | ||
416 | 416 | ||
417 | } | 417 | } |
418 | 418 |