这几天面试,面试官问了我一个recyclerView删除动画你会吗。
这玩意不是recyclerView里面系统带的吗。然后问我如果是批量删除呢?你做过吗?好蒙蔽,当时可能是紧张吧,因为刚开始使用recyclerView(菜鸟的世界,大神不懂)。
回来后赶紧查资料,mmp为什么网上都是批量删除,没有动画。还是自己动手丰衣足食吧
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
delete = (Button) findViewById(R.id.delete);
recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
edit = (Button) findViewById(R.id.edit);
arrayList = new ArrayList<>();
deleteArrayList = new ArrayList<>();
initData();
}
private void initData() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
SimpleDividerItemDecoration simpleDividerItemDecoration = new SimpleDividerItemDecoration();
//设置recyclerview线性布局管理器
recyclerview.setLayoutManager(linearLayoutManager);
//设置条目中间分割线(默认)
recyclerview.addItemDecoration(simpleDividerItemDecoration);
//设置默认系统动画
recyclerview.setItemAnimator(new DefaultItemAnimator());
//模拟数据
for (int i = 0; i < 30; i++) {
isChoose = new IsChoose();
isChoose.setContent("第" + i + "条数据");
arrayList.add(isChoose);
}
myRecyclerviewAdapter = new MyRecyclerviewAdapter(this, arrayList);
recyclerview.setAdapter(myRecyclerviewAdapter);
delete.setOnClickListener(this);
edit.setOnClickListener(this);
myRecyclerviewAdapter.setItemClickListener(new MyRecyclerviewAdapter.OnItemClickListener() {
private IsChoose e;
@Override
public void onItemClick(int position) {
//判断是否显示选择筐
if (iSSHOW == 1) {
//获取当前条目然后设置是否为选中,这里没有直接用boolean值判断
e = arrayList.get(position);
if (e.getSelect() == 0) {
e.setSelect(1);
} else {
e.setSelect(0);
}
deleteArrayList.add(e);
myRecyclerviewAdapter.notifyDataSetChanged();
} else {
Toast.makeText(MainActivity.this, "当前是第" + position + "个条目", Toast.LENGTH_SHORT).show();
}
}
});
}
private int iSSHOW = GONECHECK;
private static final int GONECHECK = 0;
private static final int SHOWCHECK = 1;
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.edit:
iSSHOW = iSSHOW == GONECHECK ? SHOWCHECK : GONECHECK;
myRecyclerviewAdapter.setMode(iSSHOW);
break;
case R.id.delete:
for (int i = 0; i < deleteArrayList.size(); i++) {
myRecyclerviewAdapter.remove(deleteArrayList.get(i));
}
break;
}
}
MainActivity没什么好说的,就是数据然后recyclerView适配和点击 接下来布局也很简单
主页面布局
item布局
适配器
public class MyRecyclerviewAdapter extends RecyclerView.Adapter implements View.OnClickListener {
private final Context MyContext;
private final ArrayList MyarrayList;
private MyViewHolder holder;
private int VISIBLE = 0;
private int ISCHOOSE = 0;
private int isshow = 0;
private IsChoose isChoose;
private int isSelect;
public MyRecyclerviewAdapter(Context context, ArrayList arrayList) {
this.MyContext = context;
this.MyarrayList = arrayList;
}
@Override
public int getItemCount() {
return MyarrayList.size();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(MyContext).inflate(R.layout.item_my_live, null, true);
holder = new MyViewHolder(inflate);
holder.mCheckBox.setVisibility(View.GONE);
//给每条布局设置点击事件 也可以用其他方式
inflate.setOnClickListener(this);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
isChoose = MyarrayList.get(position);
holder.mTvSource.setText(isChoose.getContent());
holder.itemView.setTag(position);
if (isshow == VISIBLE) {
holder.mCheckBox.setVisibility(View.GONE);
} else {
holder.mCheckBox.setVisibility(View.VISIBLE);
isSelect = isChoose.isSelect;
if (isSelect != ISCHOOSE) {
holder.mCheckBox.setImageResource(R.mipmap.ic_checked);
} else {
holder.mCheckBox.setImageResource(R.mipmap.ic_uncheck);
}
}
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick((Integer) v.getTag());
}
}
public OnItemClickListener mItemClickListener;
/**
*是否显示选择筐
*/
public void setMode(int isshow) {
this.isshow = isshow;
notifyDataSetChanged();
}
public void remove(IsChoose deleteItem) {
for (int i = 0; i < MyarrayList.size(); i++) {
if (MyarrayList.contains(deleteItem)) {
int i1 = MyarrayList.indexOf(deleteItem);
MyarrayList.remove(deleteItem);
notifyItemRemoved(i1);
}
}
}
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setItemClickListener(OnItemClickListener itemClickListener) {
mItemClickListener = itemClickListener;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private final ImageView mCheckBox;
private final TextView mTvSource;
public MyViewHolder(View itemView) {
super(itemView);
mCheckBox = itemView.findViewById(R.id.check_box);
mTvSource = itemView.findViewById(R.id.tv_source);
}
}
}
注释差不多都写完了,就是利用recyclerView的适配器中notifyItemRemoved方法做一个循环遍历
我暂时没有想到更好的办法来实现这个批量动画删除。如果哪位大神有更好的办法。欢迎指教!
下载地址
https://download.csdn.net/download/bai936780514/10402234