在QQ羣裏,不少朋友問如何實現android4.0的通知欄裏面的listview滑動刪除一個item的效果,我這裏簡單實現了一下,看圖吧(一圖勝千言)。java
思路:一、給listview添加一個滑動監聽事件(在該事件中得到滑動的位置,從而得到listview的item)。android
二、讓這個item播放一個動畫(從左滑動到右)網絡
三、item的動畫播放完畢後刪除itemapp
代碼以下 MainActivity.java :(PS: 很簡單,沒有什麼技術含量,老鳥飛過,勿噴。)ide
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; /** * @title 模仿android 4.0 通知欄動畫 * @description listview 滑動刪除item * @company 探索者網絡工做室(www.tsz.net) * @author michael Young (www.YangFuhai.com) * @version 1.0 * @created 2012-9-29 */ 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滑動接聽 */ 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 && Math.abs(x - upx) > 10) { View v = ((ListView) view).getChildAt(position1); removeListItem(v,position1); } } return false; } }); /** * listview 的item 點擊事件 */ 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(); // removeListItem(rowView, positon); } }); } /** * 刪除item,並播放動畫 * @param rowView 播放動畫的view * @param positon 要刪除的item位置 */ 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" />