侧滑删除进阶(一)

效果是在某个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.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {
	private ArrayList<String> array;
	private ArrayAdapter<String> adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ListView listView = getListView();
		array = new ArrayList<String>();
		String aa[] = { "items1", "item2", "items3", "item4", "items5",
				"item6", "items7", "item8", "items9", "item10", "items11",
				"item12" };
		for (int i = 0; i < aa.length; i++) {
			array.add(aa[i]);
		}
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, array);
		listView.setAdapter(adapter);

		listView.setOnTouchListener(new OnTouchListener() {
			float x, y, upx, upy;

			public boolean onTouch(View view, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Toast.makeText(MainActivity.this, "down", 0).show();
					x = event.getX();
					y = event.getY();
				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					Toast.makeText(MainActivity.this, "up", 0).show();
					upx = event.getX();
					upy = event.getY();
					// 我把它理解为通过x和y的位置来确定这个listView里面这个item的位置
					int position1 = ((ListView) view).pointToPosition((int) x,
							(int) y);
					int position2 = ((ListView) view).pointToPosition(
							(int) upx, (int) upy);

					if (position1 == position2 && upx > x
							&& Math.abs(x - upx) > 50) {
						View v = ((ListView) view).getChildAt(position1);
						removeListItem(v, position1);
					}
				}
				return false;
			}

		});

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View rowView,
					int positon, long id) {
				Toast.makeText(rowView.getContext(),
						"你点击了第" + positon + "位置的item", Toast.LENGTH_SHORT)
						.show();
			}
		});
	}

	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) {
				array.remove(positon);
				adapter.notifyDataSetChanged();
				animation.cancel();
			}
		});

		rowView.startAnimation(animation);

	}
}

item_anim.xml

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

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

时间: 2024-08-29 10:58:32

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

侧滑删除进阶(三)

附上一张不是效果的效果图,之前图丢了,从新弄下,大概意思就是这个 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

侧滑删除进阶(七、八)

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"

侧滑删除进阶(二)

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.

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

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

Tableciew的基本属性和侧滑(删除 置顶 更多)

#import <UIKit/UIKit.h> //使用tableview必须遵循的 @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property(strong,nonatomic) UITableView *tableview; //数据源 @property(strong,nonatomic) NSArray *students; @end #i

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

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

有关UITableViewCell的侧滑删除以及使用相关大神框架MGSwipeTableCell遇到的小问题

提起笔,却不知道从何写起了,今天一整天都耗费在了这个可能根本不算是问题的小问题上,至今仍有一种蛋蛋的忧桑..(噢,不是提笔,是键盘手T_T) 表格视图在项目中就像是每日的家常便饭,在cell上添加侧滑删除功能这种需求也是遍地可见.而就是这么一个家常菜却坑了我一天,可能我是真的闲的蛋疼吧,好吧,其实,讲道理还是我太菜,人艰不拆. 好了废话不多说,运用系统自带的API实现侧滑删除功能其实非常简单: //- (void)tableView:(UITableView *)tableView commit