123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package com.example.myapplication.ui;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
- import androidx.fragment.app.Fragment;
- import com.example.myapplication.MyApplication;
- import com.example.myapplication.R;
- import com.example.myapplication.utils.NoticeUtils;
- import com.example.myapplication.utils.SPUtils;
- import io.reactivex.Observable;
- import io.reactivex.android.schedulers.AndroidSchedulers;
- import io.reactivex.functions.Consumer;
- import io.reactivex.functions.Function;
- import io.reactivex.schedulers.Schedulers;
- /*
- *@desc :fragment的基类 封装一些共有的操作
- */
- public abstract class BaseFragment extends Fragment {
- public static final String MOB_EVENT_NAME = "click";
- public final String PAGE_NAME = getClass().getName();
- // protected void showLoadingDialog() {
- // ((BaseActivity) getActivity()).showLoadingDialog();
- // }
- //
- // protected void dismissLoadingDialog() {
- // if (isVisible()) {
- // ((BaseActivity) getActivity()).dismissLoadingDialog();
- // }
- // }
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- }
- protected void showToast(String message) {
- NoticeUtils.showToast(MyApplication.getInstance(), message, Toast.LENGTH_SHORT);
- }
- protected void showToast(int resId) {
- showToast(getResources().getString(resId));
- }
- /**
- * 从sp 获取缓存数据
- *
- * @param cacheFileName 缓存文件名
- * @param cacheKey 缓存key值
- * @param defaultValue 默认值
- * @param action 处理缓存值得action
- * @param <T> 返回缓存的值
- */
- public <T> void getCacheData(final String cacheFileName, final String cacheKey, final T defaultValue, Consumer<T> action) {
- Observable.just(defaultValue)
- .map(new Function<T, T>() {
- @Override
- public T apply(T t) {
- return (T) SPUtils.get(getActivity().getApplicationContext(), cacheFileName, cacheKey, defaultValue);
- }
- })
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(action);
- }
- protected void animStartActivity(Class<?> clazz) {
- animStartActivity(clazz, null);
- }
- protected void animStartActivity(Class<?> clazz, Bundle bundle) {
- Intent intent = new Intent(getActivity(), clazz);
- if (bundle != null) {
- intent.putExtras(bundle);
- }
- startActivity(intent);
- getActivity().overridePendingTransition(R.anim.right_in, R.anim.left_out);
- }
- protected void startActivity(Class<?> clazz) {
- startActivity(clazz, null);
- }
- protected void startActivity(Class<?> clazz, Bundle bundle) {
- Intent intent = new Intent(getActivity(), clazz);
- if (bundle != null) {
- intent.putExtras(bundle);
- }
- startActivity(intent);
- }
- @Override
- public void onResume() {
- super.onResume();
- }
- @Override
- public void onPause() {
- super.onPause();
- }
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- }
- }
|