StatusLayout
LoadingAndRetryManager
StatusLayout实现需要在布局中加入layout,通过动态的view gone visible切换
LoadingAndRetryManager的实现原理是
获取父view,移除内容,换上该显示的layout
public LoadingAndRetryManager(Object activityOrFragmentOrView, OnLoadingAndRetryListener listener)
{
if (listener == null) listener = DEFAULT_LISTENER;
ViewGroup contentParent = null;
Context context;
if (activityOrFragmentOrView instanceof Activity)
{
Activity activity = (Activity) activityOrFragmentOrView;
context = activity;
contentParent = (ViewGroup) activity.findViewById(android.R.id.content);
} else if (activityOrFragmentOrView instanceof Fragment)
{
Fragment fragment = (Fragment) activityOrFragmentOrView;
context = fragment.getActivity();
contentParent = (ViewGroup) (fragment.getView().getParent());
} else if (activityOrFragmentOrView instanceof View)
{
View view = (View) activityOrFragmentOrView;
contentParent = (ViewGroup) (view.getParent());
context = view.getContext();
} else
{
throw new IllegalArgumentException("the argument's type must be Fragment or Activity: init(context)");
}
int childCount = contentParent.getChildCount();
//get contentParent
int index = 0;
View oldContent;
if (activityOrFragmentOrView instanceof View)
{
oldContent = (View) activityOrFragmentOrView;
for (int i = 0; i < childCount; i++)
{
if (contentParent.getChildAt(i) == oldContent)
{
index = i;
break;
}
}
} else
{
oldContent = contentParent.getChildAt(0);
}
contentParent.removeView(oldContent);
//setup content layout
LoadingAndRetryLayout loadingAndRetryLayout = new LoadingAndRetryLayout(context);
ViewGroup.LayoutParams lp = oldContent.getLayoutParams();
contentParent.addView(loadingAndRetryLayout, index, lp);
loadingAndRetryLayout.setContentView(oldContent);
// setup loading,retry,empty layout
setupLoadingLayout(listener, loadingAndRetryLayout);
setupRetryLayout(listener, loadingAndRetryLayout);
setupEmptyLayout(listener, loadingAndRetryLayout);
//callback
listener.setRetryEvent(loadingAndRetryLayout.getRetryView());
listener.setLoadingEvent(loadingAndRetryLayout.getLoadingView());
listener.setEmptyEvent(loadingAndRetryLayout.getEmptyView());
mLoadingAndRetryLayout = loadingAndRetryLayout;
}
网友评论