listview删除动画 折叠效果

参照别人的移除效果,然后集成到项目里,直接上效果吧;


效果就是这样,下面shuoxia实现思路

1.初始化数据源

    private void initData() {
        for (int i = 0; i < 20; i++) {
            listData.add("数据:" + i);
        }
    }

2.适配器部分,重写getView方法:

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final View view;
        ViewHolderDeFriend vh;

        if (convertView == null) {
            view = inflater.inflate(R.layout.item_black_listview, parent, false);
            setViewHolder(view);
        } else if (((ViewHolderDeFriend) convertView.getTag()).needInflate) {
            view = inflater.inflate(R.layout.item_black_listview, parent, false);
            setViewHolder(view);
        } else {
            view = convertView;
        }
        vh = (ViewHolderDeFriend) view.getTag();
        if (flag) {
            vh.iv_delete.setVisibility(View.VISIBLE);
        } else {
            vh.iv_delete.setVisibility(View.GONE);
            vh.btn_delete.setVisibility(View.GONE);
        }
        vh.tv_user_name.setText(listData.get(position));
        vh.iv_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.deleteItem(view,listData.get(position), position);
                }
            }
        });

        return view;
    }

    public interface DeleteItemCallBack {
        void deleteItem(View view, String strName, int position);
    }

    public class ViewHolderDeFriend {

        public RoundOrCircleImageView user_icon_iv;
        public TextView tv_user_name;
        public TextView tv_user_time;
        public ImageView iv_delete;
        public Button btn_delete;
        public boolean needInflate;
    }

    private void setViewHolder(View view) {
        ViewHolderDeFriend viewHolder = new ViewHolderDeFriend();
        viewHolder.btn_delete = (Button) view.findViewById(R.id.delete_btn);
        viewHolder.iv_delete = (ImageView) view.findViewById(R.id.delete_iv);
        viewHolder.user_icon_iv = (RoundOrCircleImageView) view.findViewById(R.id.black_user_icon);
        viewHolder.tv_user_name = (TextView) view.findViewById(R.id.black_user_name);
        viewHolder.tv_user_time = (TextView) view.findViewById(R.id.black_user_time);
        viewHolder.needInflate = false;
        view.setTag(viewHolder);
    }

3.在activity中回调删除的接口

 adapter.setCallBack(new BlackListAdapter.DeleteItemCallBack() {
            @Override
            public void deleteItem(View view, String strName, int position) {
                deletePattern(view, strName, position);
            }
        });

4.设置一个AnimationListener监听,动画结束后舒心数据

    private void deletePattern(final View view, final String name, final int position) {

        Animation.AnimationListener al = new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                listData.remove(position);
                BlackListAdapter.ViewHolderDeFriend vh = (BlackListAdapter.ViewHolderDeFriend) view.getTag();
                vh.needInflate = true;
                adapter.notifyDataSetChanged();
                ToastUtil.show("解除成功  " + name);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        };
        collapse(view, al);

    }

5.自定义一个动画,自定义动画需要重写applyTransformation方法, 关于自定义动画的有参考:

http://blog.csdn.net/startfromweb/article/details/7644405


interpolatedTime会从0.0增大到1.0  

只要开始了动画,且interpolatedTime只要没减到0则此方法会一直运行

这个就是实现逐渐位移往上的关键

  private void collapse(final View view, Animation.AnimationListener al) {
        final int originHeight = view.getMeasuredHeight();

        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1.0f) {
                    view.setVisibility(View.GONE);
                } else {
                    view.getLayoutParams().height = originHeight - (int) (originHeight * interpolatedTime);
                    view.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        if (al != null) {
            animation.setAnimationListener(al);
        }
        animation.setDuration(300);
        view.startAnimation(animation);
    }

end~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值