贼简单,但是上次集成完之后忘记整理,所以写的有点简单

SwipeMenu类

继承自ViewGroup

package com.onepilltest.others;import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;//抽屉ListView
public class SwipeMenu extends ViewGroup {private int downX, moveX, moved;private Scroller scroller = new Scroller(getContext());private boolean haveShowRight = false;public static SwipeMenu swipeMenu;public SwipeMenu(Context context) {super(context);}public SwipeMenu(Context context, AttributeSet attrs) {super(context, attrs);}public SwipeMenu(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();if (swipeMenu != null&&swipeMenu==this) {swipeMenu.closeMenus();swipeMenu = null;}}//缓慢滚动到指定位置private void smoothScrollTo(int destX, int destY) {int scrollX = getScrollX();int delta = destX - scrollX;//1000ms内滑动destX,效果就是慢慢滑动scroller.startScroll(scrollX, 0, delta, 0, 100);invalidate();}public void closeMenus() {smoothScrollTo(0, 0);haveShowRight = false;}public static void closeMenu() {swipeMenu.closeMenus();}@Overridepublic void computeScroll() {if (scroller.computeScrollOffset()) {scrollTo(scroller.getCurrX(), scroller.getCurrY());postInvalidate();}}@Overridepublic boolean onTouchEvent(MotionEvent ev) {if (!scroller.isFinished()) {return false;}switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:downX = (int) ev.getRawX();break;case MotionEvent.ACTION_MOVE:if (swipeMenu != null && swipeMenu == this && haveShowRight) {closeMenu();return true;}moveX = (int) ev.getRawX();moved = moveX - downX;if (haveShowRight) {moved -= getChildAt(1).getMeasuredWidth();}scrollTo(-moved, 0);if (getScrollX() <= 0) {scrollTo(0, 0);} else if (getScrollX() >= getChildAt(1).getMeasuredWidth()) {scrollTo(getChildAt(1).getMeasuredWidth(), 0);}break;case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_UP:if (swipeMenu != null) {closeMenu();}
//                设置最小触发滑动的距离if (getScrollX() >= 30) {haveShowRight = true;swipeMenu = this;smoothScrollTo(getChildAt(1).getMeasuredWidth(), 0);} else {haveShowRight = false;smoothScrollTo(0, 0);}break;}return true;}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);}@Overridepublic LayoutParams generateLayoutParams(AttributeSet attrs) {return new MarginLayoutParams(getContext(), attrs);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);measureChildren(widthMeasureSpec, heightMeasureSpec);int width = MeasureSpec.getSize(widthMeasureSpec);View child = getChildAt(0);int margin =((MarginLayoutParams) child.getLayoutParams()).topMargin +((MarginLayoutParams) child.getLayoutParams()).bottomMargin;setMeasuredDimension(width, getChildAt(0).getMeasuredHeight() + margin);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int cCount = getChildCount();for (int i = 0; i < cCount; i++) {View child = getChildAt(i);if (i == 0) {child.layout(l, t, r, b);} else if (i == 1) {child.layout(r, t, r + child.getMeasuredWidth(), b);}}}}
  1. 修改item布局文件
<com.onepilltest.others.SwipeMenuandroid:layout_width="match_parent"android:layout_height="match_parent">第一个View...第二个View(这个View表示侧滑后出现的视图)...
</com.onepilltest.others.SwipeMenu>
  1. 在Adapter中设置监听器方法

Android给ListView添加侧滑菜单功能相关推荐

  1. 【Delphi】Android 桌面图标添加快捷菜单功能

    目录 一.为什么需要桌面图标长按快捷菜单 二.实现Android桌面图标快捷菜单 第一步:Delphi 创建一个 FMX 工程文件:ShortCut_Demo.dpr 第二步:编辑项目中的 Andro ...

  2. Android 打造完美的侧滑菜单/侧滑View控件

    概述 Android 打造完美的侧滑菜单/侧滑View控件,完全自定义实现,支持左右两个方向弹出,代码高度简洁流畅,兼容性高,控件实用方便. 详细 代码下载:http://www.demodashi. ...

  3. ListView Item侧滑菜单

    一 概述 当下从QQ主界面出现Item侧滑菜单出现之后,很多公司的项目都需要添加这种让人感觉炫酷的功能,但是,目前android API中根本没有提供这种实现,因此就要用自定义的方法来实现该功能.本篇 ...

  4. 自定义listView添加滑动删除功能

    今天研究了一下android里面的手势,结合昨天学习的自定义View,做了一个自定义的listview,继承自listView,添加了条目的滑动手势操作,滑动后出现一个删除按钮,点击删除按钮,触发一个 ...

  5. Android自定义HorizontalScrollView实现侧滑菜单效果

    最终效果 具体实现 布局文件 自定义属性 <!--自定义内容宽度--><declare-styleable name="SlideMenuLayout">& ...

  6. 左右侧滑菜单功能的实现

    左右侧滑功能是比较常见的效果,此实例实现如下的效果: 这边使用到的SlideNavigationController开源类(引入源代码中的Source),其为NavigationController子 ...

  7. Android仿QQ5.0侧滑菜单ResideMenu的使用和源码分析

    本文出自Cym的博客(http://blog.csdn.net/cym492224103) ResideMenu github:https://github.com/SpecialCyCi/Andro ...

  8. Android高仿QQ侧滑菜单

    文章目录 效果图 整体思路 实现过程 先分析SwipeMenuLayout 再分析下SwipeRecycleView 踩过的坑 后记 效果图 GIF图有点模糊,源码已上传Github:Android仿 ...

  9. android实现美团首页滑动菜单功能,Android模仿美团顶部的滑动菜单实例代码

    前言 本文主要给大家介绍了关于android模仿美团顶部滑动菜单的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 先来看下效果图: 实现方法 这是通过 viewpager ...

最新文章

  1. C语言截取指定长度子字符串方法
  2. How to create a site with AJAX enabled in MVC framework.
  3. [js] 纯函数和函数式编程有什么关系?
  4. vue 高德地图 不同区域显示不同颜色_高德百度哪家强?苹果Carplay第三方分屏功能评测...
  5. C++PrimerPlus学习——第九章编程练习
  6. 从应用启动看Activity的创建过程
  7. 7 SDImageCache
  8. hdoj 1114 Piggy-Bank(完全背包+dp)
  9. 生命游戏c语言代码,c++生命游戏源码
  10. SQLyog 报错2058 :连接 mysql 8.0.12 解决方法
  11. Zxing 扫描码的ios简化版搭建过程
  12. 计算机中DW用户名和域名,如何获取线程关联用户名和域名
  13. numpy中的数组复制
  14. Qt 70行代码实现控件任意拆分 选中控件水平垂直拆分 窗口分割
  15. sscom串口调试助手
  16. 使用python进行windows系统UI自动化
  17. 关于瑞萨RH850中断的异常处理函数地址Exception Handler Address的理解
  18. cordova打包APP更改图标和APP名字
  19. CTF工具-010editor
  20. 国际上炒黄金平台有哪些

热门文章

  1. java字面量和符号引用_JVM中的直接引用和符号引用
  2. 组网胖模式_胖AP和瘦AP的区别,组网优缺点分析
  3. spring cloud简介之最好参考
  4. php中读取文件内容的几种方法。(file_get_contents:将文件内容读入一个字符串)...
  5. 从三级界面直接跳回一级调用方法
  6. 【视频专访】他用3年时间,从0开始打造专属大学生的IT教育品牌,只为将他们送入国内最顶尖IT名企。...
  7. SSM-水果商城-pom.xml-学习-3
  8. Mysql安装及其配置
  9. linux命令行 正则,Linux shell基础(四)正则表达式与grep命令 beta
  10. app里面可以运行php,我可以在googleappengine for Python上运行PHP文件吗?