Android 滑动删除效果实现

本文将使用上篇文章介绍的Scroller类来完成一个比较常见的效果——滑动删除效果。
代码如下:

simpleSwipeListView:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cn.edu.chd.simpleswipelistview;  
  2. import android.content.Context;  
  3. import android.util.AttributeSet;  
  4. import android.util.Log;  
  5. import android.view.MotionEvent;  
  6. import android.view.View;  
  7. import android.view.ViewConfiguration;  
  8. import android.view.WindowManager;  
  9. import android.widget.AbsListView;  
  10. import android.widget.ListView;  
  11. import android.widget.Scroller;  
  12. /** 
  13.  * @author Rowandjj 
  14.  * 
  15.  *滑动删除示例 
  16.  */  
  17. public class SimpleSwipeListView extends ListView  
  18. {  
  19.     private static final String TAG = "SimpleSwipeListView";  
  20.     private static final boolean DEBUG = true;  
  21.     private Scroller mScroller = null;  
  22.     private int mTouchSlop;  
  23.       
  24.     /** 
  25.      * 屏幕宽度 
  26.      */  
  27.     private int mScreenWidth;  
  28.       
  29.     private View targetView;  
  30.       
  31.     /** 
  32.      * 当前item的位置 
  33.      */  
  34.     private int position;  
  35.       
  36.     /** 
  37.      * 手指按下的位置 
  38.      */  
  39.     private int downX,downY;  
  40.       
  41.     /** 
  42.      * 是否能删除item 
  43.      */  
  44.     private boolean canRemove = true;  
  45.       
  46.     private boolean isSwipe;  
  47.       
  48.     /** 
  49.      * item被删除的回调事件 
  50.      */  
  51.     private OnItemRemovedListener mListener;  
  52.     public interface OnItemRemovedListener  
  53.     {  
  54.         public void removeItem(int position);  
  55.     }  
  56.       
  57.     public void setOnItemRemovedListener(OnItemRemovedListener listener)  
  58.     {  
  59.         this.mListener = listener;  
  60.     }  
  61.       
  62.       
  63.     public SimpleSwipeListView(Context context)  
  64.     {  
  65.         this(context, null);  
  66.     }  
  67.     public SimpleSwipeListView(Context context, AttributeSet attrs)  
  68.     {  
  69.         this(context, attrs, 0);  
  70.     }  
  71.     public SimpleSwipeListView(Context context, AttributeSet attrs,  
  72.             int defStyleAttr)  
  73.     {  
  74.         super(context, attrs, defStyleAttr);  
  75.         init(context);  
  76.     }  
  77.     private void init(Context context)  
  78.     {  
  79.         mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();  
  80.         mScreenWidth = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();  
  81.         mScroller = new Scroller(context);  
  82.     }  
  83.       
  84.     @Override  
  85.     public boolean onTouchEvent(MotionEvent ev)  
  86.     {  
  87.           
  88.         switch (ev.getAction())  
  89.         {  
  90.         case MotionEvent.ACTION_DOWN:  
  91.             if(!mScroller.isFinished())  
  92.                 return super.onTouchEvent(ev);  
  93.               
  94.             downX = (int) ev.getX();  
  95.             downY = (int) ev.getY();  
  96.             //根据坐标得到position  
  97.             position = pointToPosition(downX, downY);  
  98.             if(position == AbsListView.INVALID_POSITION)  
  99.                 return super.onTouchEvent(ev);  
  100.             //根据position得到代表item的view对象  
  101.             targetView = getChildAt(position-getFirstVisiblePosition());  
  102.             break;  
  103.         case MotionEvent.ACTION_MOVE:  
  104.             int x = (int) ev.getX();  
  105.             int y = (int) ev.getY();  
  106.             if(Math.abs(x-downX)>mTouchSlop&&Math.abs(y-downY)<mTouchSlop)  
  107.             {  
  108.                 int deltaX = downX-x;  
  109.                 downX = x;  
  110.                 downY = y;  
  111.                 targetView.scrollBy(deltaX,0);  
  112.                 isSwipe = true;  
  113.             }  
  114.             if(isSwipe)//禁止listView滚动  
  115.                 return true;  
  116.             break;  
  117.         case MotionEvent.ACTION_UP:  
  118.             scrollByOffset();  
  119.             isSwipe = false;  
  120.             break;  
  121.         }  
  122.         return super.onTouchEvent(ev);  
  123.     }  
  124.       
  125.     /** 
  126.      * 根据偏移量判断是向左还是向右滚动 
  127.      */  
  128.     private void scrollByOffset()  
  129.     {  
  130.         int scrollX = targetView.getScrollX();//当前偏移  
  131.         if(scrollX > mScreenWidth/3)//应该向左滚  
  132.         {  
  133.             scrollToLeft(scrollX);  
  134.         }else if(scrollX < -mScreenWidth/3)//应该向右滚  
  135.         {  
  136.             scrollToRight(scrollX);  
  137.         }else//滚回原点  
  138.         {  
  139.             scrollToOrigin(scrollX);  
  140.         }  
  141.     }  
  142.       
  143.     private void scrollToOrigin(int scrollX)  
  144.     {  
  145.         mScroller.startScroll(scrollX,0,-scrollX,0,Math.abs(scrollX));  
  146.         postInvalidate();  
  147.         canRemove = false;  
  148.     }  
  149.       
  150.     /** 
  151.      * 向右滚动 
  152.      */  
  153.     private void scrollToRight(int scrollX)  
  154.     {  
  155.         int deltaX = mScreenWidth+scrollX;  
  156.         mScroller.startScroll(scrollX, 0, -deltaX, 0, Math.abs(deltaX));  
  157.         invalidate();  
  158.         canRemove = true;  
  159.     }  
  160.       
  161.     /** 
  162.      * 向左滚动 
  163.      */  
  164.     private void scrollToLeft(int scrollX)  
  165.     {  
  166.         int deltaX = mScreenWidth - scrollX;  
  167.         mScroller.startScroll(scrollX, 0, deltaX, 0, Math.abs(deltaX));  
  168.         invalidate();  
  169.         canRemove = true;  
  170.     }  
  171.       
  172.     @Override  
  173.     public void computeScroll()  
  174.     {  
  175.         if(mScroller != null)  
  176.         {  
  177.             if(mScroller.computeScrollOffset())  
  178.             {  
  179.                 int currX = mScroller.getCurrX();  
  180.                 int currY = mScroller.getCurrY();  
  181.                 targetView.scrollTo(currX,currY);  
  182.                 postInvalidate();  
  183.                   
  184.                 if(mScroller.isFinished())  
  185.                 {  
  186.                     targetView.scrollTo(00);  
  187.                     if(canRemove)  
  188.                     {  
  189.                         if(mListener == null)  
  190.                             throw new RuntimeException("you must call setOnRemoveItemListener to set a listener");  
  191.                         if(DEBUG)  
  192.                             Log.d(TAG,"position:"+position);  
  193.                         //删除item  
  194.                         mListener.removeItem(position);  
  195.                         canRemove = false;  
  196.                     }  
  197.                 }  
  198.             }  
  199.         }  
  200.     }  
  201. }  

布局:
activity_main.xml
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#ededed"  
  6.     tools:context="cn.edu.chd.simpleswipelistview.MainActivity" >  
  7.     <cn.edu.chd.simpleswipelistview.SimpleSwipeListView   
  8.         android:id="@+id/lv"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_marginTop="2dp"  
  11.         android:layout_marginBottom="2dp"  
  12.         android:layout_height="wrap_content"  
  13.         android:listSelector="@android:color/transparent"  
  14.         android:cacheColorHint="@android:color/transparent"  
  15.         android:divider="@null"  
  16.         android:dividerHeight="2dp"  
  17.         ></cn.edu.chd.simpleswipelistview.SimpleSwipeListView>  
  18.       
  19. </RelativeLayout>  
item.xml:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="3dp"  
  6.     android:descendantFocusability="beforeDescendants"  
  7.     android:paddingRight="3dp" >  
  8.     <!--滚动的是下面的内容-->  
  9.     <LinearLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="55dp"  
  12.         android:background="@drawable/card_background_selector"  
  13.         android:gravity="center_vertical|left"  
  14.         android:descendantFocusability="afterDescendants"  
  15.         android:orientation="vertical" >  
  16.         <TextView  
  17.             android:id="@+id/tv"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_marginLeft="10dp"  
  21.             android:textColor="@android:color/black"  
  22.             android:textSize="18sp" />  
  23.     </LinearLayout>  
  24. </FrameLayout>  

主界面代码:
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cn.edu.chd.simpleswipelistview;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.widget.ArrayAdapter;  
  7. import cn.edu.chd.simpleswipelistview.SimpleSwipeListView.OnItemRemovedListener;  
  8. public class MainActivity extends Activity  
  9. {  
  10.     private SimpleSwipeListView mListView = null;  
  11.     private ArrayAdapter<String> mAdapter = null;  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState)  
  14.     {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.           
  18.         mListView = (SimpleSwipeListView) findViewById(R.id.lv);  
  19.         final List<String> data = new ArrayList<String>();  
  20.         for(int i = 0; i < 20; i++)  
  21.         {  
  22.             data.add("this is text num"+i);  
  23.         }  
  24.         mAdapter = new ArrayAdapter<>(this,R.layout.item, R.id.tv, data);  
  25.         mListView.setAdapter(mAdapter);  
  26.           
  27.         mListView.setOnItemRemovedListener(new OnItemRemovedListener()  
  28.         {  
  29.             @Override  
  30.             public void removeItem(int position)  
  31.             {  
  32.                 //此处并不用调用notifyDataSetChanged,因为remove方法内部调用了  
  33.                 mAdapter.remove(data.get(position));  
  34.             }  
  35.         });  
  36.     }  
  37. }  

效果截屏不方便,大家自己看吧。

相信大家结合上篇文章介绍的Scroller理解代码应该很easy。
重点需要注意以下几点:
1.如何根据坐标找到view对象。(pointToPosition和getChildAt方法的使用)
2.Scroller的使用,特别是scrollTo、scrollBy以及computeScroll方法。
3.需要注意如果希望对某个view进行滚动,应该调用其父view的scrollTo、scrollBy方法。
4.处理滑动冲突,横向滑动删除时应避免listView滚动。

转自:http://blog.csdn.net/chdjj/article/details/41702961
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值