zoukankan      html  css  js  c++  java
  • Android 仿微信滑动删除

    做这个功能主要是项目需要:找了很多资料但是效果都不理想,后来就自己研究写了一个,拿出来共享给大家,贴上代码大家慢慢看看,还是比较容易懂的。




     主要代码:

    Java代码  收藏代码
    1. package com.zbq.widget;  
    2.   
    3. import android.content.Context;  
    4. import android.util.AttributeSet;  
    5. import android.view.GestureDetector;  
    6. import android.view.MotionEvent;  
    7. import android.view.View;  
    8. import android.widget.ListView;  
    9.   
    10. public class DelSlideListView extends ListView implements  
    11.         GestureDetector.OnGestureListener, View.OnTouchListener {  
    12.   
    13.     private GestureDetector mDetector;  
    14.     private OnDeleteListioner mOnDeleteListioner;  
    15.     private int position;  
    16.     private float velocityX, velocityY;  
    17.   
    18.     private ListViewonSingleTapUpListenner thisonSingleTapUpListenner;  
    19.   
    20.     public DelSlideListView(Context context) {  
    21.         super(context);  
    22.         init(context);  
    23.     }  
    24.   
    25.     public DelSlideListView(Context context, AttributeSet att) {  
    26.         super(context, att);  
    27.   
    28.         init(context);  
    29.     }  
    30.   
    31.     public void setDeleteListioner(OnDeleteListioner mOnDeleteListioner) {  
    32.         this.mOnDeleteListioner = mOnDeleteListioner;  
    33.     }  
    34.   
    35.     public void setSingleTapUpListenner(  
    36.             ListViewonSingleTapUpListenner thisonSingleTapUpListenner) {  
    37.         this.thisonSingleTapUpListenner = thisonSingleTapUpListenner;  
    38.     }  
    39.   
    40.     private int standard_touch_target_size = 0;  
    41.     private float mLastMotionX;  
    42.     public boolean deleteView = false;  
    43.     private ScrollLinerLayout mScrollLinerLayout;  
    44.     private boolean scroll = false;  
    45.     private int pointToPosition;  
    46.     private boolean listViewMoving;  
    47.     private boolean delAll = false;  
    48.     public boolean isLongPress = false;  
    49.   
    50.     public boolean isDelAll() {  
    51.         return delAll;  
    52.     }  
    53.   
    54.     public void setDelAll(boolean delAll) {  
    55.         this.delAll = delAll;  
    56.     }  
    57.   
    58.     private void init(Context mContext) {  
    59.         mDetector = new GestureDetector(mContext, this);  
    60.         mDetector.setIsLongpressEnabled(false);  
    61.         standard_touch_target_size = (int) getResources().getDimension(  
    62.                 R.dimen.delete_action_len);  
    63.         this.setOnTouchListener(this);  
    64.     }  
    65.   
    66.     public boolean onDown(MotionEvent e) {  
    67.         if (thisonSingleTapUpListenner != null) {  
    68.             thisonSingleTapUpListenner.onSingleTapUp();  
    69.         }  
    70.         mLastMotionX = e.getX();  
    71.         pointToPosition = this.pointToPosition((int) e.getX(), (int) e.getY());  
    72.         final int p = pointToPosition - this.getFirstVisiblePosition();  
    73.         if (mScrollLinerLayout != null) {  
    74.             mScrollLinerLayout.onDown();  
    75.             mScrollLinerLayout.setSingleTapUp(true);  
    76.         }  
    77.         if (deleteView && p != position) {  
    78.             deleteView = false;  
    79.             if (mScrollLinerLayout != null) {  
    80.                 mScrollLinerLayout.snapToScreen(0);  
    81.                 mScrollLinerLayout.setSingleTapUp(false);  
    82.             }  
    83.             position = p;  
    84.             scroll = false;  
    85.             return true;  
    86.         }  
    87.         isLongPress = false;  
    88.         position = p;  
    89.         scroll = false;  
    90.         listViewMoving = false;  
    91.         return false;  
    92.     }  
    93.   
    94.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
    95.             float velocityY) {  
    96.         this.velocityX = velocityX;  
    97.         this.velocityY = velocityY;  
    98.         return false;  
    99.     }  
    100.   
    101.     public void onLongPress(MotionEvent e) {  
    102.     }  
    103.   
    104.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
    105.             float distanceY) {  
    106.         if (listViewMoving && !scroll) {  
    107.             if (mScrollLinerLayout != null)  
    108.                 mScrollLinerLayout.snapToScreen(0);  
    109.             return false;  
    110.         } else if (scroll) {  
    111.             if (mScrollLinerLayout != null) {  
    112.                 int deltaX = (int) (mLastMotionX - e2.getX());  
    113.                 if (deleteView) {  
    114.                     deltaX += standard_touch_target_size;  
    115.                 }  
    116.                 if (deltaX >= 0 && deltaX <= standard_touch_target_size) {  
    117.                     mScrollLinerLayout.scrollBy(  
    118.                             deltaX - mScrollLinerLayout.getScrollX(), 0);  
    119.                 }  
    120.             }  
    121.         } else {  
    122.             if (Math.abs(distanceX) > Math.abs(distanceY)) {  
    123.                 final int pointToPosition1 = this.pointToPosition(  
    124.                         (int) e2.getX(), (int) e2.getY());  
    125.                 final int p1 = pointToPosition1  
    126.                         - this.getFirstVisiblePosition();  
    127.                 if (p1 == position && mOnDeleteListioner.isCandelete(p1)) {  
    128.                     mScrollLinerLayout = (ScrollLinerLayout) this  
    129.                             .getChildAt(p1);  
    130.                     if (mScrollLinerLayout != null) {  
    131.                         int deltaX = (int) (mLastMotionX - e2.getX());  
    132.                         if (deleteView) {  
    133.                             deltaX += standard_touch_target_size;  
    134.                         }  
    135.                         if (deltaX >= 0 && deltaX <= standard_touch_target_size  
    136.                                 && Math.abs(distanceY) < 5) {  
    137.                             isLongPress = true;  
    138.                             scroll = true;  
    139.                             listViewMoving = false;  
    140.                             mScrollLinerLayout.setSingleTapUp(false);  
    141.                             mScrollLinerLayout.scrollBy(  
    142.                                     (int) (e1.getX() - e2.getX()), 0);  
    143.   
    144.                         }  
    145.                     }  
    146.                 }  
    147.             }  
    148.         }  
    149.         if (scroll) {  
    150.             return true;  
    151.         }  
    152.         return false;  
    153.     }  
    154.   
    155.     public void onShowPress(MotionEvent e) {  
    156.     }  
    157.   
    158.     public boolean onSingleTapUp(MotionEvent e) {  
    159.         if (deleteView) {  
    160.             position = -1;  
    161.             deleteView = false;  
    162.             mScrollLinerLayout.snapToScreen(0);  
    163.             scroll = false;  
    164.             return true;  
    165.         }  
    166.         return false;  
    167.     }  
    168.   
    169.     public void setScroll(boolean b) {  
    170.         listViewMoving = b;  
    171.   
    172.     }  
    173.   
    174.     @Override  
    175.     public boolean onTouchEvent(MotionEvent event) {  
    176.         if (scroll || deleteView) {  
    177.             return true;  
    178.         }  
    179.         return super.onTouchEvent(event);  
    180.     }  
    181.   
    182.     @Override  
    183.     public boolean onTouch(View v, MotionEvent event) {  
    184.         if (isDelAll()) {  
    185.             return false;  
    186.         } else {  
    187.             if (event.getAction() == MotionEvent.ACTION_UP  
    188.                     || event.getAction() == MotionEvent.ACTION_CANCEL) {  
    189.                 int deltaX2 = (int) (mLastMotionX - event.getX());  
    190.                 if (scroll) {  
    191.                     if (!deleteView  
    192.                             && deltaX2 >= standard_touch_target_size / 2) {  
    193.                         mScrollLinerLayout  
    194.                                 .snapToScreen(standard_touch_target_size);  
    195.                         position = pointToPosition  
    196.                                 - this.getFirstVisiblePosition();  
    197.                         deleteView = true;  
    198.                     } else {  
    199.                         position = -1;  
    200.                         deleteView = false;  
    201.                         mScrollLinerLayout.snapToScreen(0);  
    202.                     }  
    203.                     scroll = false;  
    204.                     return true;  
    205.                 }/* else if (Math.abs(velocityX) > Math.abs(velocityY) 
    206.                         && deltaX2 < -80) { 
    207.                     mOnDeleteListioner.onBack(); 
    208.                     return false; 
    209.                 }*/  
    210.             }  
    211.             return mDetector.onTouchEvent(event);  
    212.         }  
    213.   
    214.     }  
    215.   
    216.     public void deleteItem() {  
    217.         position = -1;  
    218.         deleteView = false;  
    219.         scroll = false;  
    220.         if (mScrollLinerLayout != null) {  
    221.             mScrollLinerLayout.snapToScreen(0);  
    222.         }  
    223.     }  
    224. }  

     
     

  • 相关阅读:
    Qt 模拟一个导航定位系统
    【编程之美】用C语言实现状态机(实用)
    代码面试之链表
    乾坤合一~Linux设备驱动之USB主机和设备驱动
    乾坤合一~Linux设备驱动之I2C核心、总线以及设备驱动
    乾坤合一~Linux设备驱动之终端设备驱动
    乾坤合一~Linux设备驱动之块设备驱动
    蜕变成蝶~Linux设备驱动之watchdog设备驱动
    蜕变成蝶~Linux设备驱动之按键设备驱动
    蜕变成蝶~Linux设备驱动之DMA
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4204700.html
Copyright © 2011-2022 走看看