引言,有一天我在調(diào)試一個(gè)界面辖所,xml布局里面包含Scroll View惰说,里面嵌套了recyclerView的時(shí)候,界面一進(jìn)去缘回,就自動(dòng)滾動(dòng)到了recyclerView的那部分吆视,百思不得其解,上網(wǎng)查了好多資料酥宴,大部分只是提到了解決的辦法啦吧,但是對(duì)于為什么會(huì)這樣,都沒(méi)有一個(gè)很好的解釋拙寡,本著對(duì)技術(shù)的負(fù)責(zé)的態(tài)度授滓,花費(fèi)了一點(diǎn)時(shí)間將前后理順了下
1.首先在包含ScrollView的xml布局中,我們?cè)谝患虞d進(jìn)來(lái),ScrollView就自動(dòng)滾動(dòng)到獲取焦點(diǎn)的子view的位置般堆,那我們就需要看下我們activity的onCreate中執(zhí)行了什么在孝?
答:當(dāng)我們?cè)赼ctivity的onCreate方法中調(diào)用setContentView(int layRes)的時(shí)候,我們會(huì)調(diào)用LayoutInflater的inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)方法淮摔,這里會(huì)找到xml的rootView浑玛,然后對(duì)rootView進(jìn)行rInflateChildren(parser, temp, attrs, true)加載xml的rootView下面的子View,如果是噩咪,其中會(huì)調(diào)用addView方法顾彰,我們看下addView方法:
public void addView(View child, int index, LayoutParams params) {
......
requestLayout();
invalidate(true);
addViewInner(child, index, params, false);
}
addView的方法內(nèi)部是調(diào)用了ViewGroup的addViewInner(View child, int index, LayoutParams params,boolean preventRequestLayout)方法:
android.view.ViewGroup{
......
private void addViewInner(View child, int index, LayoutParams params,
boolean preventRequestLayout) {
......
if (child.hasFocus()) {
requestChildFocus(child, child.findFocus());
}
......
}
}
}
這里我們看到,我們?cè)谔砑右粋€(gè)hasFocus的子view的時(shí)候胃碾,是會(huì)調(diào)用requestChildFocus方法涨享,在這里我們需要明白view的繪制原理,是view樹(shù)的層級(jí)繪制仆百,是繪制樹(shù)的最頂端厕隧,也就是子view,然后父view的機(jī)制俄周。明白這個(gè)的話吁讨,我們?cè)倮^續(xù)看ViewGroup的requestChildFocus方法,
@Override
public void requestChildFocus(View child, View focused) {
if (DBG) {
System.out.println(this + " requestChildFocus()");
}
if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
return;
}
// Unfocus us, if necessary
super.unFocus(focused);
// We had a previous notion of who had focus. Clear it.
if (mFocused != child) {
if (mFocused != null) {
mFocused.unFocus(focused);
}
mFocused = child;
}
if (mParent != null) {
mParent.requestChildFocus(this, focused);
}
}
在上面會(huì)看到 mParent.requestChildFocus(this, focused);的調(diào)用峦朗,這是Android中典型的也是24種設(shè)計(jì)模式的一種(責(zé)任鏈模式)建丧,會(huì)一直調(diào)用,就這樣波势,我們肯定會(huì)調(diào)用到ScrollView的requestChidlFocus方法翎朱,然后Android的ScrollView控件,重寫了requestChildFocus方法:
@Override
public void requestChildFocus(View child, View focused) {
if (!mIsLayoutDirty) {
scrollToChild(focused);
} else {
mChildToScrollTo = focused;
}
super.requestChildFocus(child, focused);
}
因?yàn)樵赼ddViewInner之前調(diào)用了requestLayout()方法:
@Override
public void requestLayout() {
mIsLayoutDirty = true;
super.requestLayout();
}
所以我們?cè)趫?zhí)行requestChildFocus的時(shí)候尺铣,會(huì)進(jìn)入else的判斷拴曲,mChildToScrollTo = focused。
2.接下來(lái)我們繼續(xù)分析下mParent.requestChildFocus(this, focused)方法凛忿?
android.view.ViewGroup{
@Override
public void requestChildFocus(View child, View focused) {
if (DBG) {
System.out.println(this + " requestChildFocus()");
}
if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
return;
}
// Unfocus us, if necessary
super.unFocus(focused);
// We had a previous notion of who had focus. Clear it.
if (mFocused != child) {
if (mFocused != null) {
mFocused.unFocus(focused);
}
mFocused = child;
}
if (mParent != null) {
mParent.requestChildFocus(this, focused);
}
}
}
首先澈灼,我們會(huì)判斷ViewGroup的descendantFocusability屬性,如果是FOCUS_BLOCK_DESCENDANTS值的話店溢,直接就返回了(這部分后面會(huì)解釋叁熔,也是android:descendantFocusability="blocksDescendants"屬性能解決自動(dòng)滑動(dòng)的原因),我們先來(lái)看看if (mParent != null)mParent.requestChildFocus(this, focused)}成立的情況逞怨,這里會(huì)一直調(diào)用者疤,直到調(diào)用到ViewRootImpl的requestChildFocus方法
@Override
public void requestChildFocus(View child, View focused) {
if (DEBUG_INPUT_RESIZE) {
Log.v(mTag, "Request child focus: focus now " + focused);
}
checkThread();
scheduleTraversals();
}
scheduleTraversals()會(huì)啟動(dòng)一個(gè)runnable福澡,執(zhí)行performTraversals方法進(jìn)行view樹(shù)的重繪制叠赦。
3.那么ScrollView為什么會(huì)滑到獲取焦點(diǎn)的子view的位置了?
答:通過(guò)上面的分析,我們可以看到當(dāng)Scrollview中包含有焦點(diǎn)的view的時(shí)候除秀,最終會(huì)執(zhí)行view樹(shù)的重繪制糯累,所以會(huì)調(diào)用view的onLayout方法,我們看下ScrollView的onLayout方法
android.view.ScrollView{
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
......
if (mChildToScrollTo != null && isViewDescendantOf(mChildToScrollTo, this)) {
scrollToChild(mChildToScrollTo);
}
mChildToScrollTo = null;
......
}
}
從第一步我們可以看到册踩,我們?cè)趓equestChildFocus方法中泳姐,是對(duì)mChildToScrollTo進(jìn)行賦值了,所以這個(gè)時(shí)候暂吉,我們會(huì)進(jìn)入到if判斷的執(zhí)行胖秒,調(diào)用scrollToChild(mChildToScrollTo)方法:
private void scrollToChild(View child) {
child.getDrawingRect(mTempRect);
offsetDescendantRectToMyCoords(child, mTempRect);
int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(mTempRect);
if (scrollDelta != 0) {
scrollBy(0, scrollDelta);
}
}
很明顯,當(dāng)前的方法就是將ScrollView移動(dòng)到獲取制定的view當(dāng)中慕的,在這里我們可以明白了阎肝,為什么ScrollView會(huì)自動(dòng)滑到獲取焦點(diǎn)的子view的位置了。
4.為什么在ScrollView的子viewGroup中增加android:descendantFocusability=”blocksDescendants”屬性能阻止ScrollView的自動(dòng)滑動(dòng)呢肮街?
答:如第一步所說(shuō)的风题,view的繪制原理:是view樹(shù)的層級(jí)繪制,是繪制樹(shù)的最頂端嫉父,也就是子view沛硅,然后父view繪制的機(jī)制,所以我們?cè)赟crollView的直接子view設(shè)置android:descendantFocusability=”blocksDescendants”屬性的時(shí)候绕辖,這個(gè)時(shí)候直接return了,就不會(huì)再繼續(xù)執(zhí)行父view也就是ScrollView的requestChildFocus(View child, View focused)方法了摇肌,導(dǎo)致下面的自動(dòng)滑動(dòng)就不會(huì)觸發(fā)了。
@Override
public void requestChildFocus(View child, View focused) {
......
if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
return;
}
......
if (mParent != null) {
mParent.requestChildFocus(this, focused);
}
}
5.相信在這里有不少人有疑問(wèn)了:如果是按照博主你的解釋仪际,是不是在ScrollView上面加android:descendantFocusability=”blocksDescendants”屬性也能阻止自動(dòng)滑動(dòng)呢朦蕴?
答:按照前面的分析的話,似乎是可以的弟头,但是翻看ScrollView的源碼吩抓,我們可以看到
private void initScrollView() {
mScroller = new OverScroller(getContext());
setFocusable(true);
setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
setWillNotDraw(false);
final ViewConfiguration configuration = ViewConfiguration.get(mContext);
mTouchSlop = configuration.getScaledTouchSlop();
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
mOverscrollDistance = configuration.getScaledOverscrollDistance();
mOverflingDistance = configuration.getScaledOverflingDistance();
}
當(dāng)你開(kāi)心的設(shè)置android:descendantFocusability=”blocksDescendants”屬性以為解決問(wèn)題了,但是殊不知人家ScrollView的代碼里面將這個(gè)descendantFocusability屬性又設(shè)置成了FOCUS_AFTER_DESCENDANTS赴恨,所以你在xml中增加是沒(méi)有任何作用的疹娶。
6.從上面我們分析了,ScrollView一加載就會(huì)滑動(dòng)到獲取焦點(diǎn)的子view的位置了伦连,也明白了增加android:descendantFocusability="blocksDescendants"屬性能阻止ScrollView會(huì)自動(dòng)滾動(dòng)到獲取焦點(diǎn)的子view的原因雨饺,但是為什么在獲取焦點(diǎn)的子view外面套一層view,然后增加focusableInTouchMode=true屬性也可以解決這樣的滑動(dòng)呢惑淳?
答:我們注意到额港,調(diào)用addViewInner方法的時(shí)候,會(huì)先判斷view.hasFocus()歧焦,其中view.hasFocus()的判斷有兩個(gè)規(guī)則:1.是當(dāng)前的view在剛顯示的時(shí)候被展示出來(lái)了移斩,hasFocus()才可能為true;2.同一級(jí)的view有多個(gè)focus的view的話,那么只是第一個(gè)view獲取焦點(diǎn)向瓷。
如果在布局中view標(biāo)簽增加focusableInTouchMode=true屬性的話肠套,意味這當(dāng)我們?cè)诩虞d的時(shí)候,標(biāo)簽view的hasfocus就為true了猖任,然而當(dāng)在獲取其中的子view的hasFocus方法的值的時(shí)候你稚,他們就為false了。(這就意味著scrollview雖然會(huì)滑動(dòng)朱躺,但是滑動(dòng)到添加focusableInTouchMode=true屬性的view的位置刁赖,如果view的位置就是填充了scrollview的話,相當(dāng)于是沒(méi)有滑動(dòng)的长搀,這也就是為什么在外布局增加focusableInTouchMode=true屬性能阻止ScrollView會(huì)自動(dòng)滾動(dòng)到獲取焦點(diǎn)的子view的原因)所以在外部套一層focusableInTouchMode=true并不是嚴(yán)格意義上的說(shuō)法乾闰,因?yàn)殡m然我們套了一層view,如果該view不是鋪滿的scrollview的話盈滴,很可能還是會(huì)出現(xiàn)自動(dòng)滑動(dòng)的涯肩。所以我們?cè)谔譮ocusableInTouchMode=true屬性的情況,最好是在ScrollView的直接子view 上添加就可以了巢钓。
總結(jié)
通過(guò)上面的分析病苗,其實(shí)我們可以得到多種解決ScrollView會(huì)自動(dòng)滾動(dòng)到獲取焦點(diǎn)的子view的方法,比如自定義重寫Scrollview的requestChildFocus方法症汹,直接返回return硫朦,就能中斷Scrollview的自動(dòng)滑動(dòng),本質(zhì)上都是中斷了ScrollView重寫的方法requestChildFocus的進(jìn)行背镇,或者是讓Scrollview中鋪滿ScrollView的子view獲取到焦點(diǎn)咬展,這樣雖然滑動(dòng),但是滑動(dòng)的距離只是為0罷了瞒斩,相當(dāng)于沒(méi)有滑動(dòng)罷了破婆。**
同理我們也可以明白,如果是RecyclerView嵌套了RecyclerView胸囱,導(dǎo)致自動(dòng)滑動(dòng)的話祷舀,那么RecyclerView中也應(yīng)該重寫了requestChildFocus,進(jìn)行自動(dòng)滑動(dòng)的準(zhǔn)備烹笔。也希望大家通過(guò)閱讀源碼自己驗(yàn)證裳扯。
整理下3種方法:
第一種.
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
第二種.
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
第三種.
public class StopAutoScrollView extends ScrollView {
public StopAutoScrollView(Context context) {
super(context);
}
public StopAutoScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public StopAutoScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void requestChildFocus(View child, View focused) {
}
}
如果大家還有更好的解決方案,可以拿出來(lái)大家探討谤职,要是文章有不對(duì)的地方饰豺,歡迎拍磚。
如果你們覺(jué)得文章對(duì)你有啟示作用允蜈,希望你們幫忙點(diǎn)個(gè)贊或者關(guān)注下冤吨,謝謝