0

StaggeredGridLayoutManager를 기반으로하는 2 열 RecyclerView가 있습니다.diffUtil 변경 후 StaggeredGridLayotManager가 2 개 요소로 점프되는 RecylerView

1 항목의 내용을 변경하고 diffUtil을 트리거 할 때 RecyclerView가 2 항목으로 점프하고 있습니다. 그러나 위로 스크롤 할 때 스크롤 요소가 자연 순서로 변형되는 동안 1 개의 요소와 빈 공간을 볼 수 있습니다. 또한 diff 목록에있는 항목의 양은 동일합니다.

이 성가신 동작을 피하고 diffUtil 중 스크롤 위치를 유지하는 방법은 무엇입니까?

val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL) 
    recyclerView.layoutManager = layoutManager 
    recyclerView.itemAnimator = null 
    recyclerView.setHasFixedSize(true) 
    recyclerView.setItemViewCacheSize(if (App.isTablet()) 3 else 2) 

public void diffUpdate(List<T> newList) { 
    if (getCollection().size() == 0) { 
     addAll(newList); 
     notifyDataSetChanged(); 
    } else { 
     DiffCallback diffCallback = new DiffCallback<T>(getCollection(), newList); 
     DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback); 
     clear(); 
     addAll(newList); 
     diffResult.dispatchUpdatesTo(this); 
    } 
    } 

답변

0

문제는 DiffCallback을 구현할 때 발생했습니다. 이것은 RecyclerView에서 전체보기 레크리에이션을 트리거링 및 아마 StaggeredGridLayoutManager에서 측정 된

@Override public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { 
     T oldItem = oldList.get(oldItemPosition); 
     T newItem = newList.get(newItemPosition); 
     boolean areTheSameInstance = oldItem == newItem; 
     boolean hasTheSameType = oldItem.getClass().equals(newItem.getClass()); 
     boolean hasTheSameHash = oldItem.hashCode() == newItem.hashCode(); 
     return areTheSameInstance || hasTheSameType && hasTheSameHash; 
    } 

: 이전 내가 해시를 사용했다.

id 체크로 변경 한 후, diffCalbback에서 RecyclerView의 뷰 렌더링 만 트리거하기 시작했습니다.

return ((Model) oldItem).id() == ((Model) newItem).id();