模仿android4.0的通知欄listview滑動刪除item ,有滑動動畫。

jopen 11年前發布 | 31K 次閱讀 Android Android開發 移動開發


代碼詳情
代碼如下 MainActivity.java :

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滑動接聽
     */ 
    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); 

} 

} </pre>


動畫文件 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" /> 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!