码迷,mamicode.com
首页 > 其他好文 > 详细

侧滑删除进阶(二)

时间:2015-08-12 16:45:56      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:setondismisslistener   pointtoposition   setanimationlistener   popupwindow   getchildat   

技术分享

MainActivity

package com.example.ss;

import java.util.ArrayList;

import android.app.ListActivity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Toast;

import com.example.ss.adapters.NewsListAdapter;
import com.example.ss.adapters.NewsListAdapter.ViewHolder;
import com.example.ss.adapters.User;
public class MainActivity extends ListActivity {
	private ArrayList<User> array;
	private NewsListAdapter adapter;
	private PopupWindow pop;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final ListView listView = getListView();
		array = getData();
		adapter = new NewsListAdapter(this, array);
		listView.setAdapter(adapter);
		//创建popupwindow
		pop = new PopupWindow();
		pop.setTouchable(true);
		pop.setWidth(LayoutParams.WRAP_CONTENT);
		pop.setHeight(LayoutParams.WRAP_CONTENT);
		pop.setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		pop.setBackgroundDrawable(dw);
		//这个方法是:左滑出现删除按钮时候,点击其他位置,删除按钮消失,回归正常状态
		pop.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				unDelete(CurrentView, -2);
			}
		});
		/**
		 * 添加listview滑动接听
		 */
		listView.setOnTouchListener(new OnTouchListener() {
			float x, y, upx, upy;

			public boolean onTouch(View view, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					x = event.getX();
					y = event.getY();
				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					upx = event.getX();
					upy = event.getY();
					int position1 = ((ListView) view).pointToPosition((int) x,
							(int) y);
					int position2 = ((ListView) view).pointToPosition(
							(int) upx, (int) upy);

					if (position1 == position2) {
						View v = ((ListView) view).getChildAt(position1);
						if ((x - upx) > 0) {
							/**
							 * 手指向左滑动,弹出删除按钮,Item出现动画效果
							 */
							prepareDelete(v, position1);
						}
					} 
				}
				return false;
			}

		});
		
		/**
		 * listview 的item 点击事件
		 */
		//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View rowView,
					int positon, long id) {
				if (CurrentView != null) {
					unDelete(CurrentView, positon);
				}
			}
		});
	}
	protected void unDelete(final View rowView, final int positon) {
		if (rowView==null) {
			return;
			
		}
		if ("half_delete".equals(((ViewHolder) rowView.getTag()).status)) {
			final Animation animation = (Animation) AnimationUtils
					.loadAnimation(rowView.getContext(), R.anim.item2);
			animation.setFillAfter(true);
			((ViewHolder) rowView.getTag()).status = "normal";
			CurrentView = null;
			animation.setAnimationListener(new AnimationListener() {
				public void onAnimationStart(Animation animation) {
				}
				public void onAnimationRepeat(Animation animation) {
				}
				public void onAnimationEnd(Animation animation) {
					rowView.clearAnimation();
				}
			});
			rowView.startAnimation(animation);
		}
	}
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	private View CurrentView;
	protected void prepareDelete(final View rowView, final int positon) {
		//如果不是处于删除状态
		if (!"half_delete".equals(((ViewHolder) rowView.getTag()).status)) {
			final Animation animation = (Animation) AnimationUtils
					.loadAnimation(rowView.getContext(), R.anim.item);
			animation.setFillAfter(true);
			((ViewHolder) rowView.getTag()).status = "half_delete";
			//赋值给成员变量
			CurrentView = rowView;
			//添加删除按钮popup
			addDeleteButton(rowView, positon);
			rowView.startAnimation(animation);
		}
	}
	private void addDeleteButton(final View view, final int position) {

		Button button = new Button(MainActivity.this);
		button.setText("删除");
		button.setVisibility(View.VISIBLE);
		pop.setContentView(button);
		pop.showAsDropDown(view, 560, -130);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				//点击删除按钮,进行删除操作
				doDelete(view, position);
				pop.dismiss();
				CurrentView = null;
			}
		});

	}
	protected void doDelete(final View rowView, final int positon) {
		if (rowView == null) {
			return;
		}
		if ("half_delete".equals(((ViewHolder) rowView.getTag()).status)) {
			final Animation animation = (Animation) AnimationUtils
					.loadAnimation(rowView.getContext(), R.anim.item2);
			animation.setDuration(0);
			animation.setFillAfter(true);
			((ViewHolder) rowView.getTag()).status = "normal";
			CurrentView = null;
			animation.setAnimationListener(new AnimationListener() {
				public void onAnimationStart(Animation animation) {
				}

				public void onAnimationRepeat(Animation animation) {
				}

				public void onAnimationEnd(Animation animation) {
					rowView.clearAnimation();
					array.remove(positon);
					adapter.notifyDataSetChanged();
				}
			});

			rowView.startAnimation(animation);
		}

	}

	private ArrayList<User> getData() {
		ArrayList<User> users = new ArrayList<User>();
		User user = new User();
		user.logo = "http://t10.baidu.com/it/u=3485469777,1638135480&fm=56";
		user.name = "1111";
		user.time = "11:11";
		user.sign = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
		users.add(user);
		user = new User();
		user.name = "222222";
		users.add(user);
		user = new User();
		user.name = "3333";
		users.add(user);
		user = new User();
		user.name = "44444";
		users.add(user);
		user = new User();
		user.name = "5555";
		users.add(user);
		user = new User();
		user.name = "6666";
		users.add(user);
		user = new User();
		user.name = "77777";
		users.add(user);
		user = new User();
		user.name = "88888";
		users.add(user);
		user = new User();
		user.name = "99999";
		users.add(user);
		user = new User();
		user.name = "aaaaa";
		users.add(user);
		return users;
	}
}

NewsListAdapter
package com.example.ss.adapters;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.ss.R;

/**
 * Created by xch on 2014/6/14.
 */
public class NewsListAdapter extends BaseAdapter {
    private Context mContext;
    private List<User> mUsers;
    Drawable defaultDrawable;


    public NewsListAdapter(Context context, List<User> users) {
        this.mContext = context;
        this.mUsers = users;
        defaultDrawable = mContext.getResources().getDrawable(R.drawable.ic_launcher);
    }

    @Override
    public int getCount() {
        return mUsers.size();
    }

    @Override
    public Object getItem(int position) {
        return mUsers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        final ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(
                    R.layout.item_new, null);
            holder = new ViewHolder();
            holder.headImage = (ImageView) view.findViewById(R.id.new_it_iv_head);
            holder.nameText = (TextView) view.findViewById(R.id.new_it_tv_name);
            holder.signText = (TextView) view.findViewById(R.id.new_it_tv_sign);
            holder.timeText = (TextView) view.findViewById(R.id.new_it_tv_time);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        User user = mUsers.get(position);
        holder.headImage.setImageDrawable(defaultDrawable);
        holder.nameText.setText(user.name);
        holder.signText.setText(user.sign);
        holder.timeText.setText(user.time);
        return view;
    }

    public class ViewHolder {
        ImageView headImage;
        TextView nameText;
        TextView signText;
        TextView timeText;
       public String status;
    }
}

User
package com.example.ss.adapters;

public class User {

	public long uid;
	public String name;
	public String logo;
	public String time;
	public String sign;
}

item_new.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp" >

    <RelativeLayout
        android:id="@+id/new_it_rl"
        android:layout_width="match_parent"
        android:layout_height="60dp" >

        <ImageView
            android:id="@+id/new_it_iv_head"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_margin="5dp"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/new_it_tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/new_it_iv_head"
            android:text="New Text"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/new_it_tv_sign"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/new_it_iv_head"
            android:layout_marginBottom="5dp"
            android:layout_toRightOf="@+id/new_it_iv_head"
            android:text="New Text" />

        <TextView
            android:id="@+id/new_it_tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/new_it_tv_name"
            android:layout_marginRight="130dp"
            android:text="New Text" />

    </RelativeLayout>
</FrameLayout>

item.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromXDelta="0%"
    android:fromYDelta="0"
    android:toXDelta="-30%p"
    android:toYDelta="0" />

item2.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromXDelta="-30%p"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="0" />

版权声明:本文为博主原创文章,未经博主允许不得转载。

侧滑删除进阶(二)

标签:setondismisslistener   pointtoposition   setanimationlistener   popupwindow   getchildat   

原文地址:http://blog.csdn.net/u013210620/article/details/47446933

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!