RecyclerView를 사용하여 표시 할 카드 목록을 생성합니다. 각 카드에는 목록에서 해당 카드를 제거하는 버튼이 있습니다.안드로이드에서 notifyItemRemoved 또는 notifyDataSetChanged를 Android에서 RecyclerView로 사용
notifyItemRemoved()을 사용하여 RecyclerView에서 카드를 제거하면 항목이 제거되고 잘 움직이지만 목록의 데이터는 올바르게 업데이트되지 않습니다.
대신, 나는 으로 변경합니다. notifyDataSetChanged() 목록의 항목은 제거되고 올바르게 업데이트되지만 카드는 움직이지 않습니다.
누군가가 notifyItemRemoved() 사용 경험이 있고 notifyDataSetChanged와 다른 동작을하는 이유를 알고 있습니까? @pskink으로
private List<DetectedIssue> issues = new ArrayList<DetectedIssue>();
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
if(position >0){
RiskViewHolder riskHolder = (RiskViewHolder)holder;
final int index = position - 1;
final DetectedIssue anIssue = issues.get(index);
riskHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int index = issues.indexOf(anIssue);
issues.remove(anIssue);
notifyItemRemoved(index);
//notifyDataSetChanged();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
}
@Override
public int getItemCount() {
return (issues.size()+1);
}
입니다 (인덱스 + 1) – pskink
다른 인덱스 제거 –
인덱스가 정확합니다. 대신에 notifyDataSetChanged()를 사용하면 모든 것이 잘 작동한다고 했으므로 ..... – revolutionary