1. view adapter中可以由外部动态的设定item是否可见:
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
.......
private boolean isFinishedShown = true;
.......
public void hideFinish() {
isFinishedShown = false;
notifyDataSetChanged();
}
public void showFinish() {
isFinishedShown = true;
notifyDataSetChanged();
}
2. 在onBindViewHolder方法中通过if判断设置每个项目是否可见:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Task task = mTaskList.get(position);
......
if((!isFinishedShown) && task.isTaskFinished()) {
holder.linearLayout.setVisibility(View.GONE);
} else {
holder.linearLayout.setVisibility(View.VISIBLE);
}
}
我这里的业务逻辑是“如果选定了完成不可见,并且task是已经完成的,那么task不可见”
这样设置之后,不