d09e76e42db53b0f8906ed6ef5ea0b092da25620.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.example.myapplication.ui;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Toast;
  6. import androidx.fragment.app.Fragment;
  7. import com.example.myapplication.MyApplication;
  8. import com.example.myapplication.R;
  9. import com.example.myapplication.utils.NoticeUtils;
  10. import com.example.myapplication.utils.SPUtils;
  11. import io.reactivex.Observable;
  12. import io.reactivex.android.schedulers.AndroidSchedulers;
  13. import io.reactivex.functions.Consumer;
  14. import io.reactivex.functions.Function;
  15. import io.reactivex.schedulers.Schedulers;
  16. /*
  17. *@desc :fragment的基类 封装一些共有的操作
  18. */
  19. public abstract class BaseFragment extends Fragment {
  20. public static final String MOB_EVENT_NAME = "click";
  21. public final String PAGE_NAME = getClass().getName();
  22. // protected void showLoadingDialog() {
  23. // ((BaseActivity) getActivity()).showLoadingDialog();
  24. // }
  25. //
  26. // protected void dismissLoadingDialog() {
  27. // if (isVisible()) {
  28. // ((BaseActivity) getActivity()).dismissLoadingDialog();
  29. // }
  30. // }
  31. @Override
  32. public void onViewCreated(View view, Bundle savedInstanceState) {
  33. super.onViewCreated(view, savedInstanceState);
  34. }
  35. protected void showToast(String message) {
  36. NoticeUtils.showToast(MyApplication.getInstance(), message, Toast.LENGTH_SHORT);
  37. }
  38. protected void showToast(int resId) {
  39. showToast(getResources().getString(resId));
  40. }
  41. /**
  42. * 从sp 获取缓存数据
  43. *
  44. * @param cacheFileName 缓存文件名
  45. * @param cacheKey 缓存key值
  46. * @param defaultValue 默认值
  47. * @param action 处理缓存值得action
  48. * @param <T> 返回缓存的值
  49. */
  50. public <T> void getCacheData(final String cacheFileName, final String cacheKey, final T defaultValue, Consumer<T> action) {
  51. Observable.just(defaultValue)
  52. .map(new Function<T, T>() {
  53. @Override
  54. public T apply(T t) {
  55. return (T) SPUtils.get(getActivity().getApplicationContext(), cacheFileName, cacheKey, defaultValue);
  56. }
  57. })
  58. .subscribeOn(Schedulers.io())
  59. .observeOn(AndroidSchedulers.mainThread())
  60. .subscribe(action);
  61. }
  62. protected void animStartActivity(Class<?> clazz) {
  63. animStartActivity(clazz, null);
  64. }
  65. protected void animStartActivity(Class<?> clazz, Bundle bundle) {
  66. Intent intent = new Intent(getActivity(), clazz);
  67. if (bundle != null) {
  68. intent.putExtras(bundle);
  69. }
  70. startActivity(intent);
  71. getActivity().overridePendingTransition(R.anim.right_in, R.anim.left_out);
  72. }
  73. protected void startActivity(Class<?> clazz) {
  74. startActivity(clazz, null);
  75. }
  76. protected void startActivity(Class<?> clazz, Bundle bundle) {
  77. Intent intent = new Intent(getActivity(), clazz);
  78. if (bundle != null) {
  79. intent.putExtras(bundle);
  80. }
  81. startActivity(intent);
  82. }
  83. @Override
  84. public void onResume() {
  85. super.onResume();
  86. }
  87. @Override
  88. public void onPause() {
  89. super.onPause();
  90. }
  91. @Override
  92. public void onDestroyView() {
  93. super.onDestroyView();
  94. }
  95. }