最近的項目中實現訂單確定頁面。需要使用ScrollView嵌套RecyclerView,當RecyclerView中的item數量比較多時,就會出現item只顯示一部分數據,並沒有將用戶勾選的商品數量全部顯示出來,這個時候就需要我們做一下處理了。
下面來說兩種解決方案:
1、使用5.0的新控件NestedScrollView替換ScrollView.
NestedScrollView支持嵌套滑動,既能填item顯示不全的坑,又可以填嵌套滑動卡頓的坑。不瞭解的童鞋可以去學習一波,這裏就不做詳細的說明了。
用法:
(1)、佈局文件中將ScrollView替換成"android.support.v4.widget.NestedScrollView".
(2)、使用代碼設置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>
說到這我們再來熟悉一下 android:descendantFocusability="blocksDescendants"屬性的作用:
該屬性的含義是:當一個view獲取焦點時,定義ViewGroup和其子控件兩者之間的關係。
它一共有3個屬性值,它們分別是:
beforeDescendants:viewGroup會優先子類控件而獲取焦點;
afterDescendants:viewGroup只有當子類控件不需要獲取焦點的時候纔去獲取焦點;
blocksDescendants:viewGroup會覆蓋子類控件而直接獲取焦點。
兩種方案到這裏就介紹完了。
以下是個人公衆號(longxuanzhigu),之後發佈的文章會同步到該公衆號,方便交流學習Android知識及分享個人愛好的文章,有問題可以留言哦: