1.RecyclerView,ListView中獲取position時謹慎使用holder.getAdapterPosition():
1.1 目前測試發現:至少兩種情況下,ArrayIndexOutOfBoundsException: length=10; index=-1,必現:
- 調用removeItem時;
- 同時點擊兩個Item時;
1.2 官方文檔解釋:(點擊看文檔)
Note that if you’ve called notifyDataSetChanged(), until the next layout pass, the return value of this method will be NO_POSITION(-1)。
1.3 建議使用:getLayoutPosition();
2.函數返回值爲ArrayList時,謹慎使用Collections.emptyList(),EMPTY_LIST:
2.1 list長度不能改變的兩種情況:
-
list爲Collections.emptyList()或Collections.EMPTY_LIST
-
list由數組轉換而成時
list = Arrays.asList(“張三”, “李四”, “王五”);
list = Arrays.asList(new String[]{“Lucy”, “Tom”, “Jim”}); 如果對這兩種情況下的list進行增刪直接導致UnsupportedOperationException。
其中AbstractList.remove()和add();/** * <p>This implementation always throws an * {@code UnsupportedOperationException}. */ public void add(int index, E element) { throw new UnsupportedOperationException(); } /** * <p>This implementation always throws an * {@code UnsupportedOperationException}. */ public E remove(int index) { throw new UnsupportedOperationException(); }