在項(xiàng)目開發(fā)過程遇到一個問題,切換布局中某個子view的visibility為gone后,RecyclerView 自己莫名其妙的滾動了一點(diǎn)。通過網(wǎng)上查找發(fā)現(xiàn),這是RecyclerView 搶占焦點(diǎn)導(dǎo)致??梢杂靡韵路绞浇鉀Q:
父布局設(shè)置屬性descendantFocusability 為 blocksDescendants,RecyclerView 設(shè)置 屬性overScrollMode 為never。
<com.xxxxxxxxxxxx.Layout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<com.xxxxxxxxxx.view.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never" />
</com.xxxxxxxxxxxx.Layout>
descendantFocusability 屬性有三種屬性值:
1、beforeDescendants:viewgroup會優(yōu)先其子類控件而獲取到焦點(diǎn)
2、afterDescendants:viewgroup只有當(dāng)其子類控件不需要獲取焦點(diǎn)時才獲取焦點(diǎn)
3、blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點(diǎn)