RecyclerView加載多佈局時,一部分是原生數據, 一部分是網絡數據,可能會出現網絡數據加載完全後 會擠掉原來的原生ui的位置,
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:background="@color/write"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
重要的 android:descendantFocusability="blocksDescendants"這句話 出現這種UI界面上移的問題應該是子控件搶佔焦點而致,
android:descendantFocusability 大意是:
該屬性是當一個爲view獲取焦點時,定義viewGroup和其子控件兩者之間的關係。
屬性的值有三種:
-
beforeDescendants:viewgroup會優先其子類控件而獲取到焦點
-
afterDescendants:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點
-
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點
所以我們只需要在每一項Item佈局的根佈局加上android:descendantFocusability=”blocksDescendants”的屬性就好了
或者在初始RecyclerView加上android:descendantFocusability=”blocksDescendants”這句話