public class BaseFragment extends Fragment {
protected LayoutInflater inflater;
private View contentView;
protected Context mContext;
private ViewGroup container;
protected String TAG = this.getClass().getName();
protected Handler mHandler = new Handler();
public BaseFragment() {
}
protected void le(String msg) {
L.e(this.TAG, new Object[]{msg});
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mContext = this.getActivity();
}
public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.inflater = inflater;
this.container = container;
this.onCreateView(savedInstanceState);
return this.contentView == null?super.onCreateView(inflater, container, savedInstanceState):this.contentView;
}
protected void onCreateView(Bundle savedInstanceState) {
}
protected void delayClick(final View view) {
view.setEnabled(false);
this.mHandler.postDelayed(new Runnable() {
public void run() {
if(view != null) {
view.setEnabled(true);
}
}
}, BaseApplication.getInstance().getDefaultClickDelayTime());
}
protected void delayClick(final View view, long time) {
view.setEnabled(false);
this.mHandler.postDelayed(new Runnable() {
public void run() {
if(view != null) {
view.setEnabled(true);
}
}
}, time);
}
public void onDestroyView() {
NetRequest.getRequestQueue().cancelAll(NetRequest.getDefaultTag(this));
this.mHandler.removeCallbacksAndMessages((Object)null);
super.onDestroyView();
this.contentView = null;
this.container = null;
this.inflater = null;
}
public Context getApplicationContext() {
return this.mContext.getApplicationContext();
}
public void setContentView(int layoutResID) {
this.setContentView((ViewGroup)this.inflater.inflate(layoutResID, this.container, false));
}
public void setContentView(View view) {
this.contentView = view;
}
public View getContentView() {
return this.contentView;
}
public View findViewById(int id) {
return this.contentView != null?this.contentView.findViewById(id):null;
}
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, (Object)null);
} catch (NoSuchFieldException var2) {
throw new RuntimeException(var2);
} catch (IllegalAccessException var3) {
throw new RuntimeException(var3);
}
}
}
public class LazyFragment extends BaseFragment {
private boolean isInit = false;
private Bundle savedInstanceState;
public static final String INTENT_BOOLEAN_LAZYLOAD = "intent_boolean_lazyLoad";
private boolean isLazyLoad = true;
private FrameLayout layout;
protected String TAG = this.getClass().getName();
private boolean isStart = false;
public LazyFragment() {
}
protected void le(String msg) {
L.e(this.TAG, new Object[]{msg});
}
/** @deprecated */
@Deprecated
protected final void onCreateView(Bundle savedInstanceState) {
super.onCreateView(savedInstanceState);
Bundle bundle = this.getArguments();
if(bundle != null) {
this.isLazyLoad = bundle.getBoolean("intent_boolean_lazyLoad", this.isLazyLoad);
}
if(this.isLazyLoad) {
if(this.getUserVisibleHint() && !this.isInit) {
this.isInit = true;
this.savedInstanceState = savedInstanceState;
this.onCreateViewLazy(savedInstanceState);
} else {
this.layout = new FrameLayout(this.getApplicationContext());
this.layout.setLayoutParams(new LayoutParams(-1, -1));
super.setContentView(this.layout);
}
} else {
this.isInit = true;
this.onCreateViewLazy(savedInstanceState);
}
}
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser && !this.isInit && this.getContentView() != null) {
this.isInit = true;
this.onCreateViewLazy(this.savedInstanceState);
this.onResumeLazy();
}
if(this.isInit && this.getContentView() != null) {
if(isVisibleToUser) {
this.isStart = true;
this.onFragmentStartLazy();
} else {
this.isStart = false;
this.onFragmentStopLazy();
}
}
}
/** @deprecated */
@Deprecated
public final void onStart() {
super.onStart();
if(this.isInit && !this.isStart && this.getUserVisibleHint()) {
this.isStart = true;
this.onFragmentStartLazy();
}
}
/** @deprecated */
@Deprecated
public final void onStop() {
super.onStop();
if(this.isInit && this.isStart && this.getUserVisibleHint()) {
this.isStart = false;
this.onFragmentStopLazy();
}
}
protected void onFragmentStartLazy() {
}
protected void onFragmentStopLazy() {
}
protected void onCreateViewLazy(Bundle savedInstanceState) {
}
protected void onResumeLazy() {
}
protected void onPauseLazy() {
}
protected void onDestroyViewLazy() {
}
public void setContentView(int layoutResID) {
if(this.isLazyLoad && this.getContentView() != null && this.getContentView().getParent() != null) {
this.layout.removeAllViews();
View view = this.inflater.inflate(layoutResID, this.layout, false);
this.layout.addView(view);
} else {
super.setContentView(layoutResID);
}
}
public void setContentView(View view) {
if(this.isLazyLoad && this.getContentView() != null && this.getContentView().getParent() != null) {
this.layout.removeAllViews();
this.layout.addView(view);
} else {
super.setContentView(view);
}
}
/** @deprecated */
@Deprecated
public final void onResume() {
super.onResume();
if(this.isInit) {
this.onResumeLazy();
}
}
/** @deprecated */
@Deprecated
public final void onPause() {
super.onPause();
if(this.isInit) {
this.onPauseLazy();
}
}
/** @deprecated */
@Deprecated
public final void onDestroyView() {
super.onDestroyView();
if(this.isInit) {
this.onDestroyViewLazy();
}
this.isInit = false;
}
public void onDestroy() {
super.onDestroy();
}
}
public class AutoLazyFragment extends LazyFragment {
Unbinder unbinder;
@Override
public void setContentView(View view) {
super.setContentView(view);
unbinder = ButterKnife.bind(this, view);
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
unbinder = ButterKnife.bind(this, getContentView());
}
@Override
protected void onDestroyViewLazy() {
super.onDestroyViewLazy();
if(unbinder!=null){
unbinder.unbind();
}
}
}
直接在自己的fragment里继承AutoLazyFragment可实现懒加载
网友评论