一、場(chǎng)景以及解決的問題
實(shí)際項(xiàng)目中,經(jīng)常會(huì)遇到,剛進(jìn)入某一個(gè)界面,需要請(qǐng)求數(shù)據(jù)顯示加載中布局,網(wǎng)絡(luò)錯(cuò)誤顯示網(wǎng)絡(luò)錯(cuò)誤布局,服務(wù)器錯(cuò)誤時(shí)顯示服務(wù)器錯(cuò)誤布局,列表數(shù)據(jù)為空時(shí)顯示空布局。
最開始的時(shí)候,會(huì)將這幾個(gè)布局全部堆積在主布局中設(shè)置為Gone,等到需要的時(shí)候再去Visible。這樣子會(huì)有以下幾個(gè)問題:
- 一次性會(huì)將所有情況下對(duì)應(yīng)的布局全部加載
- 導(dǎo)致真正的頁(yè)面布局臃腫,不利于后期維護(hù)
- 做不到按需加載或懶加載
MultiStatusLayout支持按需加載、便于調(diào)用控制、支持?jǐn)U展至任意Layout
二、實(shí)際效果以及項(xiàng)目中配置
1.效果

2.項(xiàng)目中集成配置
詳細(xì)配置以及用法,參見github
1)gradle集成
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.Walll-E.MultiStatusLayout:library:1.0.7'
annotationProcessor 'com.github.Walll-E.MultiStatusLayout:compiler:1.0.7'
}
2)使用
定義一個(gè)類比如MultiStatusInit ,類頂部添加注解MultiStatus,點(diǎn)擊AndroidStudio的build即可。靜靜的等待編譯完畢,雙擊shift按鈕出現(xiàn)搜索框,輸入 MultiStatus 就會(huì)檢索出來相關(guān)的類,如下配置的四個(gè)Layout:編譯生成的類如下:
MultiStatusLayout、MultiStatusConstraintLayout、MultiStatusFrameLayout、MultiStatusLinearLayout
@MultiStatus(value = {
RelativeLayout.class,
ConstraintLayout.class,
FrameLayout.class,
LinearLayout.class},
provider = {
RelativeLayoutConstraintProvider.class,
ConstraintLayoutConstraintProvider.class,
FrameLayoutConstraintProvider.class,
LinearLayoutConstraintProvider.class})
public class MultiStatusInit {
}
三、Talk is cheap,show me the code
1.項(xiàng)目結(jié)構(gòu)介紹:
Annotation:MultiStatus屬性分別是:value和provider。
- value:代表需要?jiǎng)討B(tài)生成的系統(tǒng)以及第三方Layout,例如
RelativeLayout、ConstraintLayout - provider:對(duì)應(yīng)value中Layout的約束提供類,例如
RelativeLayoutConstraintProvider、ConstraintLayoutConstraintProvider這兩個(gè)由sdk內(nèi)部提供
Compiler:根據(jù) 注解MultiStatus中配置的value,provider動(dòng)態(tài)生成相應(yīng)的MultiStatusxxxxxxLayout,apt生成供外部使用的核心類
Library:核心module
-
MultiStatusEvent:利用apt生成的MultiStatusxxxxxxLayout實(shí)現(xiàn)這個(gè)接口,此接口提供生成類的一些行為(showLoading、showContent、showEmpty等) -
OnReloadDataListener:網(wǎng)絡(luò)錯(cuò)誤,服務(wù)器等錯(cuò)誤時(shí),顯示相應(yīng)布局中重試接口 -
MultiStatusHelper:根據(jù)不同情況顯示相應(yīng)布局的核心類
2.MultiStatusxxxxLayout提供的屬性介紹:
| 屬性名稱 | 說明 |
|---|---|
| loadingLayout | 加載中的布局 |
| emptyLayout | 數(shù)據(jù)為空時(shí)的布局 |
| netErrorLayout | 網(wǎng)絡(luò)錯(cuò)誤時(shí)的布局 |
| errorLayout | 加載失敗時(shí)的布局 |
| otherLayout | 擴(kuò)充的布局 |
| targetViewId | 子控件中任何時(shí)候都顯示的控件id |
| netErrorReloadViewId | 網(wǎng)絡(luò)錯(cuò)誤重試按鈕id |
| errorReloadViewId | 加載失敗重試按鈕id |
| contentReferenceIds | showContent()調(diào)用后,contentReferenceIds不受其控制;id之間的間隔英文',' |
| emptyReferenceIds | showEmpty()調(diào)用后,emptyReferenceIds不受其控制;id之間的間隔英文',' |
| errorReferenceIds | showError()調(diào)用后,errorReferenceIds不受其控制;id之間的間隔英文',' |
| netErrorReferenceIds | showNetError()調(diào)用后,netErrorReferenceIds不受其控制;id之間的間隔英文',' |
| otherReferenceIds | showOther()調(diào)用后,otherReferenceIds不受其控制;id之間的間隔英文',' |
| loadingReferenceIds | showLoading()調(diào)用后,loadingReferenceIds不受其控制;id之間的間隔英文',' |
3.核心代碼:
Annotation
MultiStatus代碼如下:
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface MultiStatus {
Class<? extends ViewGroup>[] value() default {};
Class<? extends ViewConstraintProvider>[] provider() default {};
}
- value:繼承自
ViewGroup的View,例如RelativeLayout、LinearLayout、ConstraintLayout等。 - provider:實(shí)現(xiàn)
ViewConstraintProvider這個(gè)接口的類,項(xiàng)目?jī)?nèi)提供RelativeLayoutConstraintProvider,ConstraintLayoutConstraintProvider
需要注意的是:value配置的值和provider值順序必須一致。如果不一致,可能導(dǎo)致一些不可預(yù)測(cè)的bug -.-
MultiStatusHelper
//將xml中獲取的字符串ids,解析為單個(gè)的字符串id
private void setIds(String referenceIds, int type) {
if (referenceIds == null) return;
int begin = 0;
while (true) {
int end = referenceIds.indexOf(",", begin);
if (end == -1) {
addId(referenceIds.substring(begin), type);
return;
}
addId(referenceIds.substring(begin, end), type);
begin = end + 1;
}
}
//將單個(gè)的字符串id解析為能供系統(tǒng)識(shí)別的id
private void addId(String idString, int type) {
if (idString == null || mContext == null) return;
idString = idString.trim();
int tag = 0;
try {
// id.class中的id為:com.wall_e.multiStatusLayout.R.id;
Class res = id.class;
Field field = res.getField(idString);
tag = field.getInt(null);
} catch (Exception e) {
e.printStackTrace();
}
//如果tag==0,證明沒有獲取到相應(yīng)的id
if (tag == 0) {
tag = mContext.getResources().getIdentifier(idString, "id", mContext.getPackageName());
}
if (tag == 0) {
Log.d(TAG, "xml中配置的referenceIds并不能被解析,當(dāng)前的Id:" + idString);
return;
}
//將解析傳來的id放入mReferenceIds 緩存起來
if (mReferenceIds == null) {
mReferenceIds = new ArrayMap<>();
}
if (mReferenceIds.containsKey(type)) {
List<Integer> list = mReferenceIds.get(type);
if (list != null && !list.contains(tag)) {
list.add(tag);
}
} else {
List<Integer> list = new ArrayList<>();
list.add(tag);
mReferenceIds.put(type, list);
}
}
以上代碼是將app:contentReferenceIds="actionButtonCenter,actionButtonRight,actionButtonLeft"
中的contentReferenceIds中的id字符串解析為R.id.actionButtonCenter類型,首先截取出來單個(gè)的字符串id,然后用包(com.wall_e.multiStatusLayout)下的R.id,反射獲取對(duì)應(yīng)的id,如果為0,利用mContext.getResources().getIdentifier(idString, "id", mContext.getPackageName());獲取id,如果不為0,則緩存在mReferenceIds 中以供后面的使用。
/**
* 加載相應(yīng)狀態(tài)的布局,并且添加至ViewGroup中
*
* @param index 存放布局容器的索引
* @param layoutResId 布局資源id
* @return 返回相應(yīng)狀態(tài)的布局
*/
private View inflateAndAddViewInLayout(int index, int layoutResId) {
int realIndex = mRealIndex.get(index, -1);
View view;
if (realIndex == -1) {
view = ViewGroup.inflate(mContext, layoutResId, null);
if (mViewConstraintProvider != null) {
mViewConstraintProvider.addViewBlewTargetView(view, mTargetViewId, mParent);
}
realIndex = mParent.indexOfChild(view);
if (realIndex == -1) {
mParent.addView(view);
realIndex = mParent.getChildCount()-1;
}
mRealIndex.put(index, realIndex);
} else {
view = mParent.getChildAt(realIndex);
}
return view;
}
首先去判斷mRealIndex是否緩存過這個(gè)View在ViewGroup中的索引,如果不為-1,則表示此種type的View還沒有加載進(jìn)ViewGroup,利用ViewGroup.inflate(mContext, layoutResId, null);加載完相應(yīng)的View之后,如果mViewConstraintProvider(View的約束提供)不為空,則將View添加進(jìn)ViewGroup中并且添加相應(yīng)的依賴關(guān)系 ,所謂的依賴關(guān)系主要是mTargetViewId與view的依賴關(guān)系,mTargetViewId可以為界面title的id。
/**
* 如果有背景,則不需要隱藏其他view
*
* @param view
* @return
*/
private boolean hasBackground(View view) {
if (mParent instanceof LinearLayout || mParent instanceof GridLayout) {
return false;
} else {
Drawable drawable = view.getBackground();
if (drawable instanceof ColorDrawable) {
ColorDrawable colorDrawable = (ColorDrawable) drawable;
int color = colorDrawable.getColor();
return color != Color.TRANSPARENT;
}
return drawable instanceof BitmapDrawable;
}
}
如果mParent 是LinearLayout或者GridLayout,直接返回false。調(diào)用showLoading,showEmpty,showError等方法時(shí),需要不顯示布局中其他View,因?yàn)檫@兩種ViewGroup布局原理的問題,需要直接隱藏其他View
如果不是上面的那兩種View,獲取他們的background,如果是ColorDrawable并且 colorDrawable.getColor()的值不是Color.TRANSPARENT;如果是BitmapDrawable則不隱藏,其他的一概隱藏
/**
* 按需隱藏相關(guān)的View
*
* @param type
*/
private void hideViews(int type) {
ViewGroup parent = mParent;
int targetViewId = mTargetViewId;
int count = mParent.getChildCount();
List<Integer> referenceIds = null;
//根據(jù)type獲取對(duì)應(yīng)的不受showEmpty、showLoading等控制的緩存id list
if (mReferenceIds != null) {
referenceIds = mReferenceIds.get(type);
}
int realIndex = mRealIndex.get(type, -1);
type = isCollectionEmpty(referenceIds) ? -1 : type;
List<View> views;
//根據(jù)相應(yīng)的type做出相應(yīng)的隱藏邏輯
switch (type) {
case OTHER_TYPE:
views = accordingToTypeShow(realIndex, referenceIds, mOnOtherReferenceIdsAction, parent, targetViewId);
if (mOnOtherReferenceIdsAction != null) {
mOnOtherReferenceIdsAction.showOtherAction(views);
}
break;
case LOADING_TYPE:
views = accordingToTypeShow(realIndex, referenceIds, mOnLoadingReferenceIdsAction, parent, targetViewId);
if (mOnLoadingReferenceIdsAction != null) {
mOnLoadingReferenceIdsAction.showLoadingAction(views);
}
break;
case EMPTY_TYPE:
views = accordingToTypeShow(realIndex, referenceIds, mOnEmptyReferenceIdsAction, parent, targetViewId);
if (mOnEmptyReferenceIdsAction != null) {
mOnEmptyReferenceIdsAction.showEmptyAction(views);
}
break;
case ERROR_TYPE:
views = accordingToTypeShow(realIndex, referenceIds, mOnErrorReferenceIdsAction, parent, targetViewId);
if (mOnErrorReferenceIdsAction != null) {
mOnErrorReferenceIdsAction.showErrorAction(views);
}
break;
case NET_ERROR_TYPE:
views = accordingToTypeShow(realIndex, referenceIds, mOnNetErrorReferenceIdsAction, parent, targetViewId);
if (mOnNetErrorReferenceIdsAction != null) {
mOnNetErrorReferenceIdsAction.showNetErrorAction(views);
}
break;
default:
for (int i = 0; i < count; i++) {
//如果是當(dāng)前的type在parent中的真正索引等于當(dāng)前所以,跳過
if (i == realIndex) continue;
View view = parent.getChildAt(i);
//如果view的id==targetViewId 并且當(dāng)前View是Gone并且當(dāng)前view是ViewStub 跳過
if (targetViewId != view.getId()
&& view.getVisibility() != GONE
&& !(view instanceof ViewStub)
) {
view.setVisibility(GONE);
}
}
break;
}
}
private List<View> accordingToTypeHide(int realIndex, List<Integer> referenceIds, OnReferenceViewAction action, ViewGroup mParent, int mTargetViewId) {
List<View> views = null;
int childCount = mParent.getChildCount();
for (int i = 0; i < childCount; i++) {
if (i == realIndex) continue;
View view = mParent.getChildAt(i);
int id = view.getId();
//緩存中有此id,則此view不受showLoading、showEmpty等方法控制
if (referenceIds.contains(id)) {
//如果type對(duì)應(yīng)的OnReferenceViewAction不為空,將此id對(duì)應(yīng)的view添加至list中返回給上層,用于相應(yīng)方法調(diào)用時(shí)觸發(fā)
if (action == null)continue;
if (views==null){
views = new ArrayList<>();
}
views.add(view);
continue;
}
//如果view的id==targetViewId 并且當(dāng)前View是Gone并且當(dāng)前view是ViewStub 跳過
if (mTargetViewId != view.getId()
&& view.getVisibility() != GONE
&& !(view instanceof ViewStub)) {
view.setVisibility(GONE);
}
}
return views;
}
上面這段代碼的核心是處理布局中無條件隱藏的view,不必隱藏須滿足以下兩個(gè)條件:
- View的id是mTargetViewId
- View是ViewStub
特殊說明:
首先根據(jù)type獲取相應(yīng)緩存id list,type為L(zhǎng)OADING_TYPE時(shí)對(duì)應(yīng)loadingReferenceIds 和 OnLoadingReferenceIdsAction,OnLoadingReferenceIdsAction會(huì)回調(diào)loadingReferenceIds 中的views,以便對(duì)這些view單獨(dú)做處理。其他的type同理。
/**
* 顯示
*/
public void showContent() {
if (mViewType == CONTENT_TYPE)
return;
mViewType = CONTENT_TYPE;
int count = mParent.getChildCount();
int size = mRealIndex.size();
count -= size;
List<Integer> contentIds = null;
List<View> contentView = null;
if (mReferenceIds != null) {
contentIds = mReferenceIds.get(CONTENT_TYPE);
}
if (contentIds != null) {
contentView = new ArrayList<>();
}
boolean hasContentAction = mOnContentReferenceIdsAction != null;
for (int i = 0; i < count; i++) {
View view = mParent.getChildAt(i);
if (contentIds != null && contentIds.contains(view.getId())) {
if (hasContentAction)
contentView.add(view);
continue;
}
if (!(view instanceof ViewStub) && view.getVisibility() != VISIBLE) {
view.setVisibility(VISIBLE);
}
}
for (int i = 0; i < size; i++) {
mParent.getChildAt(mRealIndex.valueAt(i)).setVisibility(GONE);
}
if (hasContentAction) {
mOnContentReferenceIdsAction.showContentAction(contentView);
}
}
showContent()這個(gè)方法用于顯示布局中原有的控件。網(wǎng)絡(luò)請(qǐng)求成功之后您就可以調(diào)用這個(gè)方法。
首先獲取mReferenceIds緩存的CONTENT_TYPE的ids,遍歷parent中的view如果view的id在contentIds中,并且OnContentReferenceIdsAction不為空,將view添加至contentView中。
最后OnContentReferenceIdsAction不為空,將contentview返回供上層調(diào)用處理。
接下來我們看看自動(dòng)生成相關(guān)Layout的代碼類——MultiStatusProcessor
具體關(guān)于APT相關(guān)介紹不是本文重點(diǎn),如果感興趣自己可以google/baidu。
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(MultiStatus.class);
if (elements == null || elements.isEmpty()) {
return true;
}
mMessager.printMessage(Diagnostic.Kind.NOTE, "<<<<<<<<<<<<<<<<<<< MultiStatusProcessor process START >>>>>>>>>>>>>>>");
Map<String, String> viewProviderMap = new HashMap<>();
List<String> viewClassList = new ArrayList<>();
List<String> providerClassList = new ArrayList<>();
//解析MultiStatus注解里面value和provider
parseParam(elements, viewClassList, providerClassList);
//將解析出來的value和provider合并
mergeList(viewClassList, providerClassList, viewProviderMap);
try {
//真正生成代碼的地方
generate(viewProviderMap);
} catch (IOException e) {
mMessager.printMessage(Diagnostic.Kind.ERROR, "Exception occurred when generating class file.");
e.printStackTrace();
}
mMessager.printMessage(Diagnostic.Kind.NOTE, "<<<<<<<<<<<<<<<<<<< MultiStatusProcessor process END >>>>>>>>>>>>>>>");
return true;
}
APT自動(dòng)化生成代碼的核心方法。
- 解析注解
MultiStatus中的value和provider中的值 - 需要value和provider中的值對(duì)應(yīng)順序一致,然后進(jìn)行合并操作
- 核心的代碼生成邏輯
private void generate(Map<String, String> viewProviderMap) throws IOException {
for (Map.Entry<String, String> entry : viewProviderMap.entrySet()) {
String clazz = entry.getKey();
String provider = entry.getValue();
int lastDotIndex = clazz.lastIndexOf(".");
String superPackageName = clazz.substring(0, lastDotIndex);
String superClassName = clazz.substring(lastDotIndex + 1);
String className;
//因?yàn)榈谝粋€(gè)版本只支持RelativeLayout,當(dāng)時(shí)類名為MultiStatusLayout
//為了兼容后期其他Layout,生成類的前面都加MultiStatus,例如:MultiStatusLinearLayout
if (superClassName.equals("RelativeLayout")) {
className = CLASS_PREFIX + "Layout";
} else {
className = CLASS_PREFIX + superClassName;
}
mMessager.printMessage(Diagnostic.Kind.NOTE, clazz + "=======>" + className);
TypeSpec.Builder builder = TypeSpec.classBuilder(className)
.addJavadoc(CLASS_JAVA_DOC)
// 注釋 1
.addModifiers(Modifier.PUBLIC)
// 注釋 2
.superclass(ClassName.get(superPackageName, superClassName))
// 注釋 3
.addSuperinterface(ClassName.get(PACKAGE_NAME, "MultiStatusEvent"))
//注釋 4
.addField(ClassName.get(PACKAGE_NAME, "MultiStatusHelper"), "mMultiStatusHelper", Modifier.PRIVATE);
//生成方法的具體操作
generateMethod(builder, clazz, provider);
JavaFile javaFile = JavaFile.builder(PACKAGE_NAME, builder.build()).build();
javaFile.writeTo(mFilter);
}
}
1.生成類是public
2.定義生成類的全路徑包名,類名
3.生成的類實(shí)現(xiàn)MultiStatusEvent接口
4.生成類添加成員變量mMultiStatusHelper
private void constructor(TypeSpec.Builder builder, String clazz, String providerClassPath) {
TypeName contextType = ClassName.get("android.content", "Context");
TypeName attributeSetType = ClassName.get("android.util", "AttributeSet");
MethodSpec constructorOne = MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(contextType, "context")
.addStatement("this(context,null)")
.build();
MethodSpec constructorTwo = MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(contextType, "context")
.addParameter(attributeSetType, "attrs")
.addStatement("this(context,attrs,0)")
.build();
MethodSpec constructorThree = MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(contextType, "context")
.addParameter(attributeSetType, "attrs")
.addParameter(TypeName.INT, "defStyleAttr")
.addStatement("super(context,attrs,defStyleAttr)")
.addStatement("mMultiStatusHelper = new MultiStatusHelper(context,attrs,defStyleAttr,this)")
//注釋1
.addStatement("generateProviderClass($S)", providerClassPath)
.build();
builder.addMethod(constructorOne)
.addMethod(constructorTwo)
.addMethod(constructorThree);
}
上面代碼片段是生成類構(gòu)造器,因?yàn)樯傻念愐彩荓ayout,所以需要構(gòu)造View的基本構(gòu)造器,最終還是交由MultiStatusHelper中處理。
注釋1:providerClassPath為,實(shí)現(xiàn)ViewConstraintProvider接口的全路徑,利用generateProviderClass()方法生成相應(yīng)的class,然后供后續(xù)使用。
private void setViewConstraintProviderClass(TypeSpec.Builder builder, String className, String providerClassPath) {
MethodSpec methodSpec = MethodSpec.methodBuilder("generateProviderClass")
.addModifiers(Modifier.PRIVATE)
.addParameter(String.class, "providerClassPath")
.beginControlFlow("if(providerClassPath == null)")
.addStatement("return")
.endControlFlow()
.beginControlFlow("try")
.addStatement("$T providerClass = $T.forName(providerClassPath)", Class.class, Class.class)
.addStatement("mMultiStatusHelper.setViewConstraintProvider(providerClass)")
.addStatement("} catch ($T e) { \n e.printStackTrace()", ClassNotFoundException.class)
.endControlFlow()
.build();
builder.addMethod(methodSpec);
}
上面代碼片段是生成generateProviderClass(String providerClassPath)的代碼。
最終編譯生成的java代碼如下:
private void generateProviderClass(String providerClassPath) {
if(providerClassPath == null) {
return;
}
try {
Class providerClass = Class.forName(providerClassPath);
mMultiStatusHelper.setViewConstraintProvider(providerClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
終于嗶嗶嗶嗶完了,太難了?。?!您只看到我扣在屏幕上的字,卻看不到我滴在鍵盤上的淚(′??Д??`)。
如果有什么問題。請(qǐng)發(fā)送郵件至pittleeeeee@gmail.com