153 lines
3.7 KiB
Java
153 lines
3.7 KiB
Java
package com.gh.base.fragment;
|
||
|
||
import android.os.Bundle;
|
||
import android.support.annotation.LayoutRes;
|
||
import android.support.annotation.Nullable;
|
||
import android.support.v4.app.Fragment;
|
||
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.common.util.EntranceUtils;
|
||
import com.gh.gamecenter.eventbus.EBMiPush;
|
||
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 butterknife.ButterKnife;
|
||
import rx.Observable;
|
||
import rx.android.schedulers.AndroidSchedulers;
|
||
import rx.schedulers.Schedulers;
|
||
|
||
/**
|
||
* Created by LGT on 2016/9/4.
|
||
* Fragment 基类
|
||
*/
|
||
public abstract class BaseFragment<T> extends Fragment implements OnRequestCallBackListener<T>,
|
||
View.OnClickListener, OnListClickListener{
|
||
|
||
// TODO private view
|
||
protected View view;
|
||
|
||
protected boolean isEverPause;
|
||
|
||
protected String mEntrance;
|
||
|
||
@LayoutRes
|
||
protected abstract int getLayoutId();
|
||
|
||
/**
|
||
* 责任链,谁处理了就返回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);
|
||
mEntrance = getActivity().getIntent().getStringExtra(EntranceUtils.KEY_ENTRANCE);
|
||
isEverPause = false;
|
||
EventBus.getDefault().register(this);
|
||
view = View.inflate(getContext(), getLayoutId(), null);
|
||
ButterKnife.bind(this, view);
|
||
initView(view);
|
||
}
|
||
|
||
//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(view);
|
||
}
|
||
return view;
|
||
}
|
||
|
||
@Override
|
||
public void onResume() {
|
||
super.onResume();
|
||
isEverPause = false;
|
||
}
|
||
|
||
@Override
|
||
public void onPause() {
|
||
super.onPause();
|
||
isEverPause = true;
|
||
}
|
||
|
||
@Override
|
||
public void onDestroy() {
|
||
super.onDestroy();
|
||
RuntimeUtils.getInstance().removeRunnable();
|
||
EventBus.getDefault().unregister(this);
|
||
}
|
||
|
||
public void toast(String msg) {
|
||
Utils.toast(getContext(), msg);
|
||
}
|
||
|
||
public boolean isEverPause() {
|
||
return isEverPause;
|
||
}
|
||
|
||
@Override
|
||
public void loadDone() {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void loadDone(Object obj) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void loadError() {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void loadEmpty() {
|
||
|
||
}
|
||
|
||
@Override
|
||
public <T> void onListClick(View view, int position, T data) {
|
||
|
||
}
|
||
|
||
protected <K> Observable<K> asyncCall(Observable<K> observable) {
|
||
return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
|
||
}
|
||
|
||
}
|