最近的項(xiàng)目中實(shí)現(xiàn)訂單確定頁(yè)面。需要使用ScrollView嵌套R(shí)ecyclerView竹海,當(dāng)RecyclerView中的item數(shù)量比較多時(shí),就會(huì)出現(xiàn)item只顯示一部分?jǐn)?shù)據(jù)汤锨,并沒(méi)有將用戶勾選的商品數(shù)量全部顯示出來(lái)扭吁,這個(gè)時(shí)候就需要我們做一下處理了痕支。
下面來(lái)說(shuō)兩種解決方案:
1啃勉、使用5.0的新控件NestedScrollView替換ScrollView.
NestedScrollView支持嵌套滑動(dòng)埠巨,既能填item顯示不全的坑义辕,又可以填嵌套滑動(dòng)卡頓的坑虾标。不了解的童鞋可以去學(xué)習(xí)一波,這里就不做詳細(xì)的說(shuō)明了灌砖。
用法:
(1)璧函、布局文件中將ScrollView替換成"android.support.v4.widget.NestedScrollView".
(2)、使用代碼設(shè)置recyclerView.setNestedScrollingEnabled(false)即可基显。
2蘸吓、在RecyclerView的外面嵌套一層RelativeLayout,然后添加屬性 android:descendantFocusability="blocksDescendants".
用法參考:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"/>
</RelativeLayout>
說(shuō)到這我們?cè)賮?lái)熟悉一下 android:descendantFocusability="blocksDescendants"屬性的作用:
該屬性的含義是:當(dāng)一個(gè)view獲取焦點(diǎn)時(shí)撩幽,定義ViewGroup和其子控件兩者之間的關(guān)系库继。
它一共有3個(gè)屬性值,它們分別是:
beforeDescendants:viewGroup會(huì)優(yōu)先子類控件而獲取焦點(diǎn)窜醉;
afterDescendants:viewGroup只有當(dāng)子類控件不需要獲取焦點(diǎn)的時(shí)候才去獲取焦點(diǎn)宪萄;
blocksDescendants:viewGroup會(huì)覆蓋子類控件而直接獲取焦點(diǎn)。
兩種方案到這里就介紹完了榨惰。