侧滑删除进阶(二)

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" />

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

时间: 2024-11-07 04:16:31

侧滑删除进阶(二)的相关文章

侧滑删除进阶(七、八)

activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"

侧滑删除进阶(三)

附上一张不是效果的效果图,之前图丢了,从新弄下,大概意思就是这个 activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layou

侧滑删除进阶(四)

最近在写滑动删除,网上代码又是看的云里雾里,果断自己研究,虽然效果不太好把,但是思路很清晰,代码逻辑很简单 先来看主页面布局 activity_main.xml--很简单就一个自定义listview <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:la

侧滑删除进阶(五)

*********************************************跟上一篇效果一样*********************************** activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" and

侧滑删除进阶(一)

效果是在某个Item上右滑可以删除某个条目--效果虽然很简单,但是思路很重要 MainActivity package com.yangfuhai.animation1; import java.util.ArrayList; import android.app.ListActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.vie

自定义控件进阶02_侧滑删除,粘性控件

1 快速索引 细节问题: 1.1 把当前被选中的字母索引置为灰色,否则为白色 每一次在快速索引栏上的触摸事件都触发invalidate(),重走onDraw()方法 在onDraw()方法里,做判断,如果通过触摸事件计算的索引与绘制字母数组的索引一致时就更改画笔的颜色,(记得在触摸事件中如果手指抬起,就把计算的索引置为-1) 1.2 弹出吐司不太好看,弹出一个圆角的矩形框会好看一些(实际上就是一个圆角的TextView,平常隐藏,滑动的时候显示) 圆角:定义背景的xml文件,shape根节点,弧

给UITableView的侧滑删除增加多个按钮

一. 需求: cell的侧滑删除默认只有一个删除按钮, 给侧滑添加多个按钮, '删除', '置顶', '更多'. 二. 实现说明: 1) 我们在使用一些应用的时候,在滑动一些联系人的某一行的时候,会出现删除.置顶.更多等等的按钮,在iOS8之前,我们都需要自己去实现.但是到了iOS8,系统已经写好了,只需要一个代理方法和一个类就搞定了 2) iOS8的协议多了一个方法,返回值是数组的tableView:editActionsForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮

2、蛤蟆的数据结构进阶二静态查询之顺序查询

2.蛤蟆的数据结构进阶二静态查询之顺序查询 本篇名言:"我从不把安逸和快乐看作是生活的本身 --这种伦理基础,我叫它猪栏的理想. --爱因斯坦" 这篇我们来看下静态查询中的顺序表查询. 我们先来看下概念,然后是代码实现. 欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/47175103 1.  查找 在计算机科学中定义为:在一些(有序的/无序的)数据元素中,通过一定的方法找出与给定关键字相同的数据元素的过程叫做查找.

Java进阶(二十五)Java连接mysql数据库(底层实现)

Java进阶(二十五)Java连接mysql数据库(底层实现) 前言 很长时间没有系统的使用java做项目了.现在需要使用java完成一个实验,其中涉及到java连接数据库.让自己来写,记忆中已无从搜索.特将之前使用的方法做一简单的总结.也能够在底层理解一下连接数据库的具体步骤. 实现 首先需要导入相关的jar包,我使用的为:mysql-connector-java-5.1.7-bin.jar. 下面来看一下我所使用的数据库连接方法类: MysqlUtil.java package cn.edu