, 당신은 꽤 좋은 결과 (애니메이션)을 얻을 수있다 보기 소유자를 다시 사용하십시오. (대 setAdapter, 그것은 새로운 어댑터가 이전 어댑터와 동일한 ViewHolder 세트를 가지고 있다는 것을 모르기 때문에 모든 뷰를 재사용하고 다시 생성해야합니다).
더 나은 방법은 제거 할 항목을 찾고 notifyItemRemoved(index)
을 호출하는 것입니다. 실제로 항목을 제거하는 것을 잊지 마십시오. 이렇게하면 RecyclerView에서 예측 애니메이션을 실행할 수 있습니다. 당신이 내부적으로 ArrayList를 사용하는 어댑터를 가정하면, 구현은 다음과 같습니다
// adapter code
final List<ItemData> mItems = new ArrayList(); //contains your items
public void filterOut(String filter) {
final int size = mItems.size();
for(int i = size - 1; i>= 0; i--) {
if (mItems.get(i).test(filter) == false) {
mItems.remove(i);
notifyItemRemoved(i);
}
}
}
그것은 수행 할 것입니다 당신이 배치 notifyItemRemoved
통화를 할 수있는 대신 notifyItemRangeRemoved
를 사용하는 경우 더 나은. 이 같은 STH 보일 것이다 : (테스트하지) 당신은 이전에 필터링 된 항목을 추가하려면이 코드를 확장해야
public void filterOut(String filter) {
final int size = mItems.size();
int batchCount = 0; // continuous # of items that are being removed
for(int i = size - 1; i>= 0; i--) {
if (mItems.get(i).test(filter) == false) {
mItems.remove(i);
batchCount ++;
} else if (batchCount != 0) { // dispatch batch
notifyItemRangeRemoved(i + 1, batchCount);
batchCount = 0;
}
}
// notify for remaining
if (batchCount != 0) { // dispatch remaining
notifyItemRangeRemoved(0, batchCount);
}
}
을하지만 지금은 볼 수 있어야합니다 (예를 들어, 사용자가 필터 쿼리를 삭제)하지만 난이 사람이해야한다고 생각 기본 아이디어를 줘.
각 알림 항목 호출은 그 다음에 오는 항목 호출에 영향을 미친다는 점에 유의하십시오. 따라서 목록에서 피할 수 있도록 목록을 지나가고 있습니다. 끝에서 이동은 ArrayList의 메소드 제거 제거 (항목 이동 감소)에도 도움이됩니다.
예를 들어 처음부터 목록을 탐색하여 처음 두 항목을 제거한 경우입니다. 당신은
notifyItemRangeRemoved(0, 2); // 2 items starting from index 0
또는 하나
notifyItemRemoved(0);
notifyItemRemoved(0);//because after the previous one is removed, this item is at position 0
방금 그것으로 새로운'ItemsAdapter'을 만들 수있는 또 다른 배열을주고 싶어 다시'setAdapter' 호출하는 경우. –