238 lines
6.3 KiB
Java
238 lines
6.3 KiB
Java
package com.gh.base.fragment;
|
||
|
||
import android.arch.lifecycle.Lifecycle;
|
||
import android.content.Intent;
|
||
import android.os.Bundle;
|
||
import android.os.Handler;
|
||
import android.os.Message;
|
||
import android.support.annotation.LayoutRes;
|
||
import android.support.annotation.Nullable;
|
||
import android.support.annotation.StringRes;
|
||
import android.support.v4.app.Fragment;
|
||
import android.support.v4.app.FragmentTransaction;
|
||
import android.text.TextUtils;
|
||
import android.view.LayoutInflater;
|
||
import android.view.View;
|
||
import android.view.ViewGroup;
|
||
|
||
import com.gh.base.OnListClickListener;
|
||
import com.gh.base.OnRequestCallBackListener;
|
||
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<T> extends Fragment implements OnRequestCallBackListener<T>,
|
||
View.OnClickListener, OnListClickListener, OnTitleClickListener {
|
||
|
||
protected View mCachedView;
|
||
|
||
protected boolean isEverPause;
|
||
|
||
protected String mEntrance;
|
||
|
||
protected final Handler mBaseHandler = new BaseFragment.BaseHandler(this);
|
||
|
||
protected static class BaseHandler extends Handler {
|
||
private final WeakReference<BaseFragment> 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);
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
//TODO 尴尬,必须的有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) {
|
||
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
|
||
toast(getString(res));
|
||
}
|
||
|
||
public void toast(String msg) {
|
||
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
|
||
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 <LIST> void onListClick(View view, int position, LIST data) {
|
||
|
||
}
|
||
|
||
protected <K> Observable<K> asyncCall(Observable<K> observable) {
|
||
return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
|
||
}
|
||
|
||
// 将所有的Fragment都置为隐藏状态。
|
||
protected void hideFragments(FragmentTransaction transaction) {
|
||
List<Fragment> list = getChildFragmentManager().getFragments();
|
||
for (Fragment fragment : list) {
|
||
transaction.hide(fragment);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onTitleClick() {
|
||
List<Fragment> list = getChildFragmentManager().getFragments();
|
||
for (Fragment fragment : list) {
|
||
if (fragment instanceof OnTitleClickListener) {
|
||
((OnTitleClickListener) fragment).onTitleClick();
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|