2 baidu 19694879 baidu_19694879 于 2016.04.26 11:33 提问

android listView 删除动画 当前Item 删了,但是动画总是从最后一行执行

点击ListView 的item中的控件,删除当前item时,想让它一动画的方式 消失,但是,点击删除时,item可以删除掉,但是动画总是执行的最后一行 求解、??

这是主要代码

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mActivity).inflate(R.layout.layout_dialog_item_detial, null);
holder = new ViewHolder();
holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
holder.tv_score = (TextView) convertView.findViewById(R.id.tv_score);
holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

    DiagHistory dh = historyLists.get(position);
    holder.tv_time.setText(dh.getScanTime());
    holder.tv_score.setText(dh.getDiaScore());
    holder.iv_delete.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            View itemView = (View) holder.iv_delete.getParent();
            removeListItem(itemView, position);
            SQLLiteService.getInstance(mActivity).deleteDialogHistory(holder.tv_time.getText().toString().trim());
        }
    });

    return convertView;
}

class ViewHolder {
    TextView tv_time, tv_score;
    ImageView iv_delete;
}

private void removeListItem(View rowView, final int positon) {
    final Animation animation = (Animation) AnimationUtils.loadAnimation(rowView.getContext(),
            R.anim.remove_dialog_list_item);
    animation.setAnimationListener(new AnimationListener() {
        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationRepeat(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            historyLists.remove(positon);
            DialogItemDetailAdapter.this.notifyDataSetChanged();
            animation.cancel();
        }
    });
    rowView.startAnimation(animation);
}

6个回答

danielinbiti
danielinbiti   Ds   Rxr 2016.04.26 12:17
已采纳
 View itemView = (View) holder.iv_delete.getParent();  //holder是个全局变量,最后指向界面显示的最后一条,所以你点随便哪一条,都是操作最后一条。这里需要当做参数传入到clicklisten中
            removeListItem(itemView, position);
重新定义OnClickListener
class SelfListener implements OnClickListener{
    private View itemView;
        private int positon;
        private String txt;
        public SelfListener(View itemView,int positon,String txt){
            this.itemView  = itemView;
                this.positon = positon;
                this.txt = txt;
        }
        @Override
        public void onClick(View v) {
            removeListItem(this.itemView, this.position);
            SQLLiteService.getInstance(mActivity).deleteDialogHistory(this.txt);
        }
}
//设置监听
holder.iv_delete.setOnClickListener(new SelfListener((View) holder.iv_delete.getParent(),position,holder.tv_time.getText().toString().trim()));
baidu_19694879
baidu_19694879 感谢!!!问题已解决。。。
一年多之前 回复
CSDNXIAON
CSDNXIAON   2016.04.26 11:42

android删除listview item(动画删除)
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

q610098308
q610098308   2016.04.26 11:49

1.removeListItem 这个传参,直接传convertView就可以;
2.另外你可以用 RecyclerView 代替ListView 这个默认就有动画
可以看我的博客,并且有demo
http://blog.csdn.net/q610098308/article/details/50173983

molashaonian
molashaonian   2016.04.26 13:09

动画可以手动控制啊,当删除item的时候停住动画的播放,并且赋值为null

baidu_23086307
baidu_23086307   2016.04.27 20:10

View itemView = (View) holder.iv_delete.getParent(); //holder是个全局变量,最后指向界面显示的最后一条,所以你点随便哪一条,都是操作最后一条。这里需要当做参数传入到clicklisten中
removeListItem(itemView, position);
重新定义OnClickListener
class SelfListener implements OnClickListener{
private View itemView;
private int positon;
private String txt;
public SelfListener(View itemView,int positon,String txt){
this.itemView = itemView;
this.positon = positon;
this.txt = txt;
}
@Override
public void onClick(View v) {
removeListItem(this.itemView, this.position);
SQLLiteService.getInstance(mActivity).deleteDialogHistory(this.txt);
}
}
//设置监听
holder.iv_delete.setOnClickListener(new SelfListener((View) holder.iv_delete.getParent(),position,holder.tv_time.getText().toString().trim()));

qq_27578023
qq_27578023   2016.05.29 11:01

View itemView = (View) holder.iv_delete.getParent(); //holder是个全局变量,最后指向界面显示的最后一条,所以你点随便哪一条,都是操作最后一条。这里需要当做参数传入到clicklisten中
removeListItem(itemView, position);
重新定义OnClickListener
class SelfListener implements OnClickListener{
private View itemView;
private int positon;
private String txt;
public SelfListener(View itemView,int positon,String txt){
this.itemView = itemView;
this.positon = positon;
this.txt = txt;
}
@Override
public void onClick(View v) {
removeListItem(this.itemView, this.position);
SQLLiteService.getInstance(mActivity).deleteDialogHistory(this.txt);
}
}
//设置监听
holder.iv_delete.setOnClickListener(new SelfListener((View) holder.iv_delete.getParent(),position,holder.tv_time.getText().toString().trim()));

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!