Android解決RecyclerView中的item顯示不全方案


最近的項目中實現訂單確定頁面。需要使用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知識及分享個人愛好的文章,有問題可以留言哦:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM