ListView-item-删除

来源:互联网 发布:传奇霸业内功等级数据 编辑:程序博客网 时间:2024/06/06 03:03
自定义的Baseadapter的getview()中view = inflater.inflate(R.layout.myorder_onelist, null, false);view一定不能设置成全局变量,否则会出现点击删除混乱的情况;

getview()如下:

public View getView(final int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubViewHolder holder=null;View view = null;LayoutInflater inflater = LayoutInflater.from(mycontext);Map<String,String> map = mylist.get(position);if(convertView==null){holder = new ViewHolder();convertView = inflater.inflate(R.layout.myorder_onelist, null, false);convertView.setTag(holder);}else if (((ViewHolder)convertView.getTag()).needInflate) {//折叠式的这里不能缺少holder = new ViewHolder();convertView = inflater.inflate(R.layout.myorder_onelist, null, false);convertView.setTag(holder);}else{holder = (ViewHolder) convertView.getTag();}holder.StartCity = (TextView) convertView.findViewById(R.id.onelist_startcity);holder.EndCity = (TextView) convertView.findViewById(R.id.onelist_endcity);holder.StartTime = (TextView) convertView.findViewById(R.id.onelist_starttime);holder.EndTime = (TextView) convertView.findViewById(R.id.onelist_endtime);holder.delected = (ImageView) convertView.findViewById(R.id.onelist_delecet);holder.date = (TextView) convertView.findViewById(R.id.onelist_time);holder.Photo = (ImageButton) convertView.findViewById(R.id.onelist_photo);holder.delected.setOnClickListener(new Click(convertView,position));holder.StartCity.setText(map.get("Start"));holder.EndCity.setText(map.get("End"));holder.EndTime.setText(map.get("Etime"));holder.StartTime.setText(map.get("Stime"));holder.date.setText(map.get("date"));return convertView;}
介绍两种方式:
1、折叠式的删除:

private void deleteCell(final View v, final int index) {AnimationListener al = new AnimationListener() {@Overridepublic void onAnimationEnd(Animation arg0) {removeItem(index);//自定义的函数ViewHolder vh = (ViewHolder)v.getTag();vh.needInflate = true;}@Override public void onAnimationRepeat(Animation animation) {}@Override public void onAnimationStart(Animation animation) {}};collapse(v, al);}private void collapse(final View v, AnimationListener al) {final int initialHeight = v.getMeasuredHeight();Animation anim = new Animation() {@Overrideprotected void applyTransformation(float interpolatedTime, Transformation t) {if (interpolatedTime == 1) {v.setVisibility(View.GONE);}else {v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);v.requestLayout();}}@Overridepublic boolean willChangeBounds() {return true;}};if (al!=null) {anim.setAnimationListener(al);}anim.setDuration(300);v.startAnimation(anim);}
2、侧滑式:
protected void removeListItem(View rowView, final int positon) {final Animation animation = (Animation) AnimationUtils.loadAnimation(rowView.getContext(), R.anim.item_anim);animation.setAnimationListener(new AnimationListener() {public void onAnimationStart(Animation animation) {}public void onAnimationRepeat(Animation animation) {}public void onAnimationEnd(Animation animation) {removeItem(positon);animation.cancel();}});rowView.startAnimation(animation);}

上面两种方式都有单独的android demo,链接为:

http://download.csdn.net/detail/hanyuboke/9387170


全部代码:

package com.hpu.Init;import java.util.ArrayList;import java.util.Map;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.Context;import android.content.DialogInterface;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.view.animation.Transformation;import android.view.animation.Animation.AnimationListener;import android.widget.BaseAdapter;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.TextView;import com.example.traffic.R;import com.hpu.Framement.OrderFragment;public class MyOrderBaseAdapter extends BaseAdapter{private Context mycontext;private static ArrayList<Map<String,String>> mylist;public MyOrderBaseAdapter(Context context,ArrayList<Map<String,String>> list){this.mycontext = context;this.mylist = list;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn mylist.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn mylist.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}public void removeItem(int position){     mylist.remove(position);     this.notifyDataSetChanged(); }    @Overridepublic View getView(final int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubViewHolder holder=null;View view = null;LayoutInflater inflater = LayoutInflater.from(mycontext);Map<String,String> map = mylist.get(position);if(convertView==null){holder = new ViewHolder();convertView = inflater.inflate(R.layout.myorder_onelist, null, false);convertView.setTag(holder);}else if (((ViewHolder)convertView.getTag()).needInflate) {holder = new ViewHolder();convertView = inflater.inflate(R.layout.myorder_onelist, null, false);convertView.setTag(holder);}else{holder = (ViewHolder) convertView.getTag();}holder.StartCity = (TextView) convertView.findViewById(R.id.onelist_startcity);holder.EndCity = (TextView) convertView.findViewById(R.id.onelist_endcity);holder.StartTime = (TextView) convertView.findViewById(R.id.onelist_starttime);holder.EndTime = (TextView) convertView.findViewById(R.id.onelist_endtime);holder.delected = (ImageView) convertView.findViewById(R.id.onelist_delecet);holder.date = (TextView) convertView.findViewById(R.id.onelist_time);holder.Photo = (ImageButton) convertView.findViewById(R.id.onelist_photo);holder.delected.setOnClickListener(new Click(convertView,position));holder.StartCity.setText(map.get("Start"));holder.EndCity.setText(map.get("End"));holder.EndTime.setText(map.get("Etime"));holder.StartTime.setText(map.get("Stime"));holder.date.setText(map.get("date"));return convertView;}private void AlterDiaLogListen(final View convertView,final int pos) {// TODO Auto-generated method stubAlertDialog.Builder alt = new Builder(mycontext);alt.setTitle("提示");alt.setMessage("确定要删除?");alt.setIcon(android.R.drawable.ic_dialog_info);alt.setCancelable(false);alt.setPositiveButton("确认",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub//mylist.remove(p);//notifyDataSetChanged();//侧滑removeListItem(convertView,pos);//折叠式//deleteCell(convertView, pos);}});alt.setNeutralButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});alt.show();}private void deleteCell(final View v, final int index) {AnimationListener al = new AnimationListener() {@Overridepublic void onAnimationEnd(Animation arg0) {removeItem(index);ViewHolder vh = (ViewHolder)v.getTag();vh.needInflate = true;}@Override public void onAnimationRepeat(Animation animation) {}@Override public void onAnimationStart(Animation animation) {}};collapse(v, al);}private void collapse(final View v, AnimationListener al) {final int initialHeight = v.getMeasuredHeight();Animation anim = new Animation() {@Overrideprotected void applyTransformation(float interpolatedTime, Transformation t) {if (interpolatedTime == 1) {v.setVisibility(View.GONE);}else {v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);v.requestLayout();}}@Overridepublic boolean willChangeBounds() {return true;}};if (al!=null) {anim.setAnimationListener(al);}anim.setDuration(300);v.startAnimation(anim);}//侧滑protected void removeListItem(View rowView, final int positon) {final Animation animation = (Animation) AnimationUtils.loadAnimation(rowView.getContext(), R.anim.item_anim);animation.setAnimationListener(new AnimationListener() {public void onAnimationStart(Animation animation) {}public void onAnimationRepeat(Animation animation) {}public void onAnimationEnd(Animation animation) {removeItem(positon);animation.cancel();}});rowView.startAnimation(animation);} public class ViewHolder{private TextView StartCity;private TextView EndCity;private TextView StartTime;private TextView EndTime;private ImageView delected;private TextView date;private ImageButton Photo;public boolean needInflate;} public class Click implements OnClickListener{private int position;private View view;public Click(View view,int pos){this.position = pos;this.view = view;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.onelist_delecet:AlterDiaLogListen(view,position);break;default:break;}}}}



0 0