package com.gh.base.fragment; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.LayoutRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.StringRes; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.Lifecycle; import com.gh.base.OnListClickListener; import com.gh.base.OnRequestCallBackListener; import com.gh.common.constant.Constants; import com.gh.gamecenter.BuildConfig; import com.gh.gamecenter.eventbus.EBMiPush; import com.lightgame.OnTitleClickListener; import com.lightgame.utils.RuntimeUtils; import com.lightgame.utils.Utils; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.lang.ref.WeakReference; import java.util.List; import butterknife.ButterKnife; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import static com.gh.common.util.EntranceUtils.KEY_ENTRANCE; /** * Created by LGT on 2016/9/4. * Fragment 基类 */ public abstract class BaseFragment extends Fragment implements OnRequestCallBackListener, View.OnClickListener, OnListClickListener, OnTitleClickListener { public static final int RESULT_REFRESH = 9528; protected View mCachedView; protected boolean isEverPause; @NonNull protected String mEntrance; protected final Handler mBaseHandler = new BaseFragment.BaseHandler(this); protected static class BaseHandler extends Handler { private final WeakReference mFragmentWeakReference; BaseHandler(BaseFragment fragment) { mFragmentWeakReference = new WeakReference<>(fragment); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); BaseFragment fragment = mFragmentWeakReference.get(); if (fragment != null) fragment.handleMessage(msg); } } protected void handleMessage(Message msg) { } @LayoutRes protected abstract int getLayoutId(); /** * 提供 Inflated 的 view ,可用于 data binding. */ protected View getInflatedLayout() { return null; } /** * 责任链,谁处理了就返回true,否则返回super.handleOnClick(View view) * * @return */ protected boolean handleOnClick(View view) { return true; } @Override public void onClick(View v) { handleOnClick(v); } protected void initView(View view) { } protected void postRunnable(Runnable runnable) { RuntimeUtils.getInstance().runOnUiThread(runnable); } // 定时任务全部改用这个方法, 在onDestroy做统一取消定时 protected void postDelayedRunnable(Runnable runnable, long delayMillis) { RuntimeUtils.getInstance().runOnUiThread(runnable, delayMillis); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Intent intent = getActivity().getIntent(); mEntrance = intent.getStringExtra(KEY_ENTRANCE); if (TextUtils.isEmpty(mEntrance) && getArguments() != null) { mEntrance = getArguments().getString(KEY_ENTRANCE); } if (TextUtils.isEmpty(mEntrance)) { mEntrance = Constants.ENTRANCE_UNKNOWN; } if (BuildConfig.DEBUG) { Utils.log("FRAGMENT_ENTRANCE -> " + mEntrance); } isEverPause = false; EventBus.getDefault().register(this); // For data binding. if (getInflatedLayout() != null) { mCachedView = getInflatedLayout(); } else { mCachedView = View.inflate(getContext(), getLayoutId(), null); } ButterKnife.bind(this, mCachedView); initView(mCachedView); } // 必须的有subscribe才能register @Subscribe(threadMode = ThreadMode.BACKGROUND) public void onDummyEvent(EBMiPush push) { } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (container != null) { container.removeView(mCachedView); } return mCachedView; } @Override public void onResume() { super.onResume(); isEverPause = false; } @Override public void onPause() { super.onPause(); isEverPause = true; } @Override public void onDestroy() { super.onDestroy(); mBaseHandler.removeCallbacksAndMessages(null); RuntimeUtils.getInstance().removeRunnable(); EventBus.getDefault().unregister(this); } public void toast(@StringRes int res) { toast(getString(res)); } public void toast(String msg) { Utils.toast(getContext(), msg); } public void toastLong(@StringRes int msg) { toastLong(getString(msg)); } public void toastLong(String msg) { RuntimeUtils.getInstance().toastLong(getContext(), msg); } public boolean isEverPause() { return isEverPause; } @Override public void loadDone() { } @Override public void loadDone(T obj) { } @Override public void loadError() { } @Override public void loadEmpty() { } @Override public void onListClick(View view, int position, LIST data) { } protected Observable asyncCall(Observable observable) { return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()); } // 将所有的Fragment都置为隐藏状态。 protected void hideFragments(FragmentTransaction transaction) { List list = getChildFragmentManager().getFragments(); for (Fragment fragment : list) { transaction.hide(fragment); } } @Override public void onTitleClick() { List list = getChildFragmentManager().getFragments(); for (Fragment fragment : list) { if (fragment instanceof OnTitleClickListener) { ((OnTitleClickListener) fragment).onTitleClick(); } } } // 为 fragment 附加 bundle (setArgument()) public BaseFragment with(Bundle bundle) { this.setArguments(bundle); return this; } }