BaseRecyclerViewAdapterHelper
我是使用第三方 BaseRecyclerViewAdapterHelper 作爲RecyclerView的 adapter。
直達 BaseRecyclerViewAdapterHelper 庫
先看效果
-
首先BRVAH這個庫已經支持了加載動畫,使用起來很方便,實現加載動畫的具體方法在這裏 ,加載動畫默認提供5種方法(漸顯、縮放、從下到上,從左到右、從右到左),當然也支持自定義的動畫效果。
-
添加刪除動畫也很簡單
1. 在Adapter 中添加刪除動畫,執行清理數據的操作
public class MyAdapter extends BaseQuickAdapter<ItemBean,BaseViewHolder> {
private final Resources resources;
List itemList = new ArrayList<View>();
BaseViewHolder mBaseViewHolder;
public MyAdapter(int layoutResId, List data) {
super(layoutResId, data);
resources = Utils.getApp().getResources();
}
@Override
protected void convert(BaseViewHolder baseViewHolder, ItemBean ItemBean) {
//設置item 顯示數據的地方
}
// 刪除動畫
public void removeData(final int position) {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(itemList.get(position), "TranslationX", 0, 1000);
scaleX.setDuration(500);
scaleX.start();
scaleX.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation);
//刪除數據
notifyItemRemoved(position);
//如果不是按照順序刪除就需要整體刷新
// notifyDataSetChanged();
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
//刪除數據
notifyItemRemoved(position);
//如果不是按照順序刪除就需要整體刷新
// notifyDataSetChanged();
}
});
}
}
2. 外部只需要調用Adapter 的removeData 方法就可以刪除數據,並顯示動畫效果
MyAdapter.removeData(itemCount);
就是這麼簡單的實現刪除動畫效果。
BRVAH庫添加動畫的具體實現方法 BaseRecyclerAdapter之添加動畫(策略模式),詳細講解在哪添加動畫、控制動畫執行次數等內容。
在這裏插入代碼片