1、触摸事件 dispatchTouchEvent 判断是否处理触摸动作 onTouchEvent 处理触摸动作

2、Android对于控制和获取View在屏幕很强大

ListView:

pointToPosition 根据触摸点获取item的位置

getChildAt 根据索引获取item的View,注意从第一个可视化的item算起

View:

getLocationOnScreen获取View在屏幕的坐标

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;public class MyListView extends ListView
{private static final String TAG = "MyListView";// private static final int VELOCITY_SANP = 200;// private VelocityTracker mVelocityTracker;/*** 用户滑动的最小距离*/private int touchSlop;/*** 是否响应滑动*/private boolean isSliding;/*** 手指按下时的x坐标*/private int xDown;/*** 手指按下时的y坐标*/private int yDown;/*** 手指移动时的x坐标*/private int xMove;/*** 手指移动时的y坐标*/private int yMove;private LayoutInflater mInflater;private PopupWindow mPopupWindow;private int mPopupWindowHeight;private int mPopupWindowWidth;private Button mDelBtn;/*** 为删除按钮提供一个回调接口*/private DelButtonClickListener mListener;/*** 当前手指触摸的View*/private View mCurrentView;/*** 当前手指触摸的位置*/private int mCurrentViewPos;/*** 必要的一些初始化** @param context* @param attrs*/public MyListView(Context context, AttributeSet attrs){super(context, attrs);mInflater = LayoutInflater.from(context);// 获取设备所支持的最小滑动距离touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();View view = mInflater.inflate(R.layout.button_list, null);mDelBtn = (Button) view.findViewById(R.id.id_item_btn1);//
        mPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);/*** 先调用下measure,否则拿不到宽和高*/mPopupWindow.getContentView().measure(0, 0);mPopupWindowHeight = mPopupWindow.getContentView().getMeasuredHeight();mPopupWindowWidth = mPopupWindow.getContentView().getMeasuredWidth();}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev){int action = ev.getAction();int x = (int) ev.getX();int y = (int) ev.getY();switch (action){case MotionEvent.ACTION_DOWN:xDown = x;yDown = y;/*** 如果当前popupWindow显示,则直接隐藏,然后屏蔽ListView的touch事件的下传*/if (mPopupWindow.isShowing()){dismissPopWindow();return false;}// 获得当前手指按下时的item的位置mCurrentViewPos = pointToPosition(xDown, yDown);// 获得当前手指按下时的itemView view = getChildAt(mCurrentViewPos - getFirstVisiblePosition());mCurrentView = view;break;case MotionEvent.ACTION_MOVE:xMove = x;yMove = y;int dx = xMove - xDown;int dy = yMove - yDown;/*** 判断是否是从右到左的滑动*/if (xMove < xDown && Math.abs(dx) > touchSlop && Math.abs(dy) < touchSlop){// Log.e(TAG, "touchslop = " + touchSlop + " , dx = " + dx +// " , dy = " + dy);isSliding = true;}break;}return super.dispatchTouchEvent(ev);}@Overridepublic boolean onTouchEvent(MotionEvent ev){int action = ev.getAction();/*** 如果是从右到左的滑动才相应*/if (isSliding){switch (action){case MotionEvent.ACTION_MOVE:int[] location = new int[2];// 获得当前item的位置x与y
                    mCurrentView.getLocationOnScreen(location);// 设置popupWindow的动画
                    mPopupWindow.setAnimationStyle(R.style.button_anim_style);mPopupWindow.update();mPopupWindow.showAtLocation(mCurrentView, Gravity.LEFT | Gravity.TOP,location[0] + mCurrentView.getWidth(), location[1]);// + mCurrentView.getHeight() / 2 - mPopupWindowHeight / 2);// 设置删除按钮的回调mDelBtn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){if (mListener != null){mListener.clickHappend(mCurrentViewPos);mPopupWindow.dismiss();}}});// Log.e(TAG, "mPopupWindow.getHeight()=" + mPopupWindowHeight);break;case MotionEvent.ACTION_UP:isSliding = false;}// 相应滑动期间屏幕itemClick事件,避免发生冲突return true;}return super.onTouchEvent(ev);}/*** 隐藏popupWindow*/private void dismissPopWindow(){if (mPopupWindow != null && mPopupWindow.isShowing()){mPopupWindow.dismiss();}}public void setDelButtonClickListener(DelButtonClickListener listener){mListener = listener;}interface DelButtonClickListener{public void clickHappend(int position);}}

转载于:https://www.cnblogs.com/xbx2015/p/4699685.html

Android 自定义ListView控件,滑动删除相关推荐

  1. android 自定义listview控件,一个简单又完整的自定义ListView

    ListView 一.简单列表 1.在activity_main中添加控件ListView xmlns:tools="http://schemas.android.com/tools&quo ...

  2. Android 自定义组合控件小结

    Android 自定义组合控件小结 引言 接触Android UI开发的这段时间以来,对自定义组合控件有了一定的了解,为此小结一下,本文小结内容主要讨论的是如何使用Android SDK提供的布局和控 ...

  3. android 全选功能,Android实现ListView控件的多选和全选功能实例

    本文实例讲述了Android实现ListView控件的多选和全选功能.分享给大家供大家参考,具体如下: 主程序代码 MainActivity.Java package yy.test; import ...

  4. Android自定义时间控件不可选择未来时间

    本文出自:http://blog.csdn.net/dt235201314/article/details/78718066 Android自定义时间控件选择开始时间到结束时间 Android自定义时 ...

  5. Android使用ListView控件问题

    Android使用ListView控件问题: The application has stopped unexpectedly, please try again. 开发环境:android 1.6 ...

  6. android中ListView控件onItemClick事件中获取listView传递的数据

    http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...

  7. android自定义table,Android 自定义表格控件

    Android 自定义表格控件 发布时间:2018-08-20 17:07, 浏览次数:487 , 标签: Android 1.简介 tabview是一款开源表格控件,可以通过xml属性设置行列数.设 ...

  8. listview控件Android,Android中ListView控件的简单使用

    文章引自郭霖<第一行代码> ListView允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内,同时屏幕上原有的数据则会滚动出屏幕 使用LIstView控件 1 在布局文件中引入Li ...

  9. Android列表ListView控件的使用

    在Android开发中,ListView是比较常用的控件,它以列表的形式显示具体内容,并且能够根据数据的长度自适应显示. 在ListView中可以根据需要显示自定义的列表内容,包括文字(TextVie ...

最新文章

  1. 【学习笔记】观察者模式
  2. python3.7.2版本怎么安装ipython_Linux升级安装python2.7版本至python3.6版本,系统centos7...
  3. macOS下载、安装、使用tomcat服务器及IntelliJ IDEA for Mac 如何集成、配置、运行tomcat
  4. 2021新职业教育行业发展研究报告
  5. Web服务器Nginx多方位优化策略
  6. 最常用的PHP正则表达式收集整理
  7. python自动化办公手册之python操作PPT
  8. 我的世界服务器显示红心,我的世界手机版红心怎么恢复 | 手游网游页游攻略大全...
  9. u盘linux运行速度慢,linux准确测量U盘读写速度
  10. 360浏览器保存的html没有图标,电脑360浏览器图标不见了怎么办
  11. word文档怎么批量解除锁定_word文档被锁定,怎么解开?
  12. bcc语料库下载_语料库汇总
  13. Android实现自定义铃音
  14. 开发基于 Google Map 的 Android 应用
  15. 什么是FOUC?如何避免FOUC?
  16. 【UE4】搭建局域网内VR直播 UE4.27
  17. pap认证失败_路由器PAP:密码验证失败.无法上网
  18. 大家最关心的问题:大数据培训完一般可以做哪些工作?
  19. 基于51单片机的两路数字电压表Protues仿真设计
  20. 论文解读: PP-YOLOE: An evolved version of YOLO

热门文章

  1. 世界上最贵的车是直通车吗?
  2. 当个年轻的又不太有经验的老板有多难?
  3. 微信支付宝是如何赚钱的?
  4. 如果实现了一种不用暂停世界的GC算法,会对现在编程语言产生什么影响?
  5. 把照片存QQ相册会越来越模糊,你们会把照片存在哪里?
  6. 以太坊可更新智能合约研究与开发综述
  7. Qt——P20 模态和非模态对话框创建
  8. ### Error building SqlSession. ### Cause: org.apache.ibatis.builder.BuilderException: Error creating
  9. 4.2第一个窗口程序
  10. SQL Server中的动态数据屏蔽