我在微信公眾號中開間別人推薦的第三方庫,看到這枚一個庫:該庫可以幫你快速實現(xiàn)TabLayout和CoordinatorLayout的組合效果。效果如下:
Github地址:https://github.com/hugeterry/CoordinatorTabLayout
? ? ? ?看到這個庫我就想在自己的demo中試一下,看到介紹是使用CoordinatorLayout 實現(xiàn)的我就在網(wǎng)上搜了文章看看女器,之后看到別人的總結(jié)(附上鏈接http://blog.csdn.net/huachao1001/article/details/51554608),是關(guān)于 CoordinatorLayout.Behavior的使用,效果圖不好弄出來重贺,就簡單說一下:就是在CoordinatorLayout布局中拖動一個button 另一個button隨之移動,這個感覺很好實現(xiàn)回懦,但是耦合會很大气笙,上面的鏈接中就介紹了用CoordinatorLayout.Behavior怎么實現(xiàn),先去看完上面的鏈接講解再回來看接下的內(nèi)容怯晕。
? ? ? ?最后我有個疑問就是:button 添加了app:layout_behavior="com.hc.studyCoordinatorLayout.MyBehavior"之后怎么跟著tempView隨之移動的潜圃,tempView 中沒有添加任何的監(jiān)聽等多余的代碼?贫贝?秉犹?但是效果真的出來了,跟著這個疑問我就點開了CoordinatorLayout.Behavior的源碼看看是怎么實現(xiàn)的:在MyBehavior這個類中有這個方法layoutDependsOn稚晚,只有這個方法與tempView有關(guān)聯(lián)崇堵,我們來看下你父類中對這個方法的解釋:
/**
* Determine whether the supplied child view has another specific sibling view as a
* layout dependency.
*
This method will be called at least once in response to a layout request. If it
* returns true for a given child and dependency view pair, the parent CoordinatorLayout
* will:
*
Always lay out this child after the dependent child is laid out, regardless
* of child order.
*
Call {@link #onDependentViewChanged} when the dependency view's layout or
* position changes.
*
* @param parent the parent view of the given child
* @param child the child view to test
* @param dependency the proposed dependency of child
* @return true if child's layout depends on the proposed dependency's layout,
* false otherwise
* @see #onDependentViewChanged(CoordinatorLayout, android.view.View, android.view.View)
*/
public boolean layoutDependsOn(CoordinatorLayout parent, V child, View dependency) {
return false;
}
意思是:確定提供子視圖還有另一個特定的兄弟視圖布局的依賴。
此方法將響應(yīng)布局請求至少調(diào)用一次客燕。 如果它
對給定的子視圖和依賴視圖對返回true鸳劳,父CoordinatorLayout將:
總是把這個孩子依賴孩子,不管孩子的秩序。
當(dāng)依賴布局位置大小改變的時候調(diào)用也搓;
/**
* Check if an associated child view depends on another child view of the CoordinatorLayout.
*
* @param parent the parent CoordinatorLayout
* @param child the child to check
* @param dependency the proposed dependency to check
* @return true if child depends on dependency
*/
boolean dependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency == mAnchorDirectChild
|| (mBehavior != null && mBehavior.layoutDependsOn(parent, child, dependency));
}
在CoordinatorLayout布局中檢查子視圖依賴另一個子視圖赏廓;
/**
* Check if the given child has any layout dependencies on other child views.
*/
boolean hasDependencies(View child) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp.mAnchorView != null) {
return true;
}
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View other = getChildAt(i);
if (other == child) {
continue;
}
if (lp.dependsOn(this, child, other)) {
return true;
}
}
return false;
}
就是循環(huán)查看子視圖依賴另一個子視圖;在上面的源碼中我們可以知道通過循環(huán)來檢查視圖的依賴關(guān)系傍妒;
好到這里我們就可以總結(jié)一下它的實現(xiàn)原理了:
? ? ? ? 首先我們自定義的MyBehavior 繼承了CoordinatorLayout.Behavior類幔摸,并重寫這個抽象類的兩個方法layoutDependsOn和onDependentViewChanged,layoutDependsOn是判斷CoordinatorLayout下的子視圖是否其中一個子視圖依賴另一個子視圖颤练,onDependentViewChanged 是當(dāng)被依賴的子視圖發(fā)生改變了都會執(zhí)行這個方法既忆。
? ? ? ? 我們從layoutDependsOn看起,找到CoordinatorLayout.Behavior 中的layoutDependsOn方法,在整個類中查找哪里調(diào)用了layoutDependsOn方法患雇,這個調(diào)用過程是:onMeasure方法第二行調(diào)用了ensurePreDrawListener方法跃脊,ensurePreDrawListener方法里通過循環(huán)遍歷CoordinatorLayout的每個子視圖并調(diào)用hasDependencies方法來判斷CoordinatorLayout下的子視圖是否有依賴關(guān)系的,如果有在ensurePreDrawListener中調(diào)用addPreDrawLinstener方法添加依賴監(jiān)聽苛吱,在addPreDrawLinstener方法中給ViewTreeObserver添加監(jiān)聽(創(chuàng)建的OnPreDrawLintener)酪术,OnPreDrawLintener這個類實現(xiàn)ViewTreeObserver.OnpreDrawListener接口,并在實現(xiàn)的onPredraw方法中調(diào)用DispatchOnDepentViewChanged方法翠储,DispatchOnDepentViewChanged這個方法就是前面說的CoordinatorLayout.Behavior中的一個方法用于當(dāng)被依賴的子視圖發(fā)生改變了都會執(zhí)行這個方法绘雁;
? ? ? ? ?看到這里我們就對CoordinatorLayout.Behavior有了一個大致的了解之后我們就可以用CoordinatorLayout.Behavior寫一下想要的效果了。
? ? ? ? 我這理解的很淺彰亥,只是一些表面上的理解咧七,這是我學(xué)習(xí)之路上的一部分,我會堅持把我學(xué)習(xí)到的東西總結(jié)下來希望對大家起到些作用任斋。
下面粘貼一下源碼: