android实现简单左滑删除控件
作者:qq_20352713 发布时间:2023-11-10 03:01:29
标签:android,删除控件
本文为大家分享了一个简单的android左滑删除控件,供大家参考,具体内容如下
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.support.v4.view.ViewConfigurationCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
public class SwipeLayout extends ViewGroup{
public static String TAG = "SwipeLayout";
//可以滚动的距离
int mSwipeWidth;
PointF firstPoint;
PointF lastPoint;
float mTouchSlop;
ValueAnimator openAnimator;
ValueAnimator closeAnimator;
public SwipeLayout(Context context) {
this(context,null);
}
public SwipeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(ViewConfiguration.get(getContext()));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int left=0;
int childCount = getChildCount();
for (int i=0;i<childCount;++i){
View child = getChildAt(i);
//按顺序从左往右排
// if (i==0){
// child.layout(0,0,child.getMeasuredWidth(),child.getMeasuredHeight());
// }else {
child.layout(left,0,left+child.getMeasuredWidth(),child.getMeasuredHeight());
// }
left += child.getMeasuredWidth();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int childCount = getChildCount();
View mainChild = getChildAt(0);
int width=0;
int height=0;
mSwipeWidth = 0;
// measureChild(mainChild,widthMeasureSpec,heightMeasureSpec);
measure(widthMeasureSpec,heightMeasureSpec);
//滑动距离是 从index开始 所有控件的宽度之和
if (childCount>1) {
for (int i = 1; i < childCount; ++i) {
mSwipeWidth += getChildAt(i).getMeasuredWidth();
}
}
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthValue = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightValue = MeasureSpec.getSize(heightMeasureSpec);
switch (heightMode){
case MeasureSpec.AT_MOST:
case MeasureSpec.UNSPECIFIED:
//没有指定大小 按照第一个子控件的大小来设置
height = mainChild.getMeasuredHeight();
break;
case MeasureSpec.EXACTLY:
height = heightValue;
break;
}
switch (widthMode){
case MeasureSpec.AT_MOST:
case MeasureSpec.UNSPECIFIED:
//没有指定大小 按照第一个子控件的大小来设置
width = mainChild.getMeasuredWidth();
break;
case MeasureSpec.EXACTLY:
width = widthValue;
break;
}
// for (int i=1;i<childCount;++i){
// measureChild(getChildAt(i),widthMeasureSpec,MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY));
// }
setMeasuredDimension(width,height);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
firstPoint = new PointF(ev.getX(),ev.getY());
lastPoint = new PointF(ev.getX(),ev.getY());
break;
case MotionEvent.ACTION_MOVE:
float moveDistance = ev.getX()-firstPoint.x;
//移动距离大于制定值 认为进入控件的滑动模式
if (Math.abs(moveDistance) > mTouchSlop ){
//让父控件不拦截我们的事件
getParent().requestDisallowInterceptTouchEvent(true);
//拦截事件
return true;
}
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_MOVE:
float moveDistance = ev.getX()-lastPoint.x;
lastPoint = new PointF(ev.getX(),ev.getY());
// 这里要注意 x大于0的时候 往左滑动 小于0往右滑动
scrollBy((int) -moveDistance ,0);
//边界判定 超过了边界 直接设置为边界值
if (getScrollX()> mSwipeWidth){
scrollTo(mSwipeWidth,0);
}else if (getScrollX()<0){
scrollTo(0,0);
}
break;
case MotionEvent.ACTION_UP:
//没动 不理他
if (getScrollX()== mSwipeWidth ||getScrollX()==0){
return false;
}
float distance = ev.getX()-firstPoint.x;
//滑动距离超过 可滑动距离指定值 继续完成滑动
if (Math.abs(distance) > mSwipeWidth *0.3 ){
if (distance>0){
smoothClose();
}else if (distance<0){
smoothOpen();
}
}else {
if (distance>0){
smoothOpen();
}else if (distance<0){
smoothClose();
}
}
return true;
}
return super.onTouchEvent(ev);
}
public void smoothOpen(){
clearAnimator();
openAnimator = ValueAnimator.ofInt(getScrollX(), mSwipeWidth);
openAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer integer = (Integer) animation.getAnimatedValue();
scrollTo(integer,0);
}
});
openAnimator.start();
}
public void smoothClose(){
clearAnimator();
closeAnimator = ValueAnimator.ofInt(getScrollX(),0);
closeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer integer = (Integer) animation.getAnimatedValue();
scrollTo(integer,0);
}
});
closeAnimator.start();
}
public void open(){
scrollTo(mSwipeWidth,0);
}
public void close(){
scrollTo(0,0);
}
//执行滑动动画必须先清除动画 不然会鬼畜
private void clearAnimator(){
if (closeAnimator!=null && closeAnimator.isRunning()){
closeAnimator.cancel();
closeAnimator = null;
}
if (openAnimator!=null && openAnimator.isRunning()) {
openAnimator.cancel();
openAnimator = null;
}
}
public void toggle(){
if (getScrollX()==0){
open();
}else {
close();
}
}
}
使用
<com.example.chenweiqi.simplerefreshview.widget.SwipeLayout
android:id="@+id/swipeLayout"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#F3F3F3"
>
<Button
android:id="@+id/btn"
android:text="123"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:background="#FF0000"
android:text="shanchu"
android:layout_width="80dp"
android:layout_height="match_parent" />
<TextView
android:gravity="center"
android:textAlignment="center"
android:background="#0F0"
android:text="123"
android:layout_width="30dp"
android:layout_height="match_parent" />
</com.example.chenweiqi.simplerefreshview.widget.SwipeLayout>
效果
来源:https://blog.csdn.net/qq_20352713/article/details/85063628


猜你喜欢
- 一、系统介绍本系统实现扑克的分发,抢地主,电脑自动出牌等功能。二、系统展示1.扑克分发2.抢地主3.出牌4.游戏胜利三、系统实现Card.j
- 一、前序遍历1.题目描述给你二叉树的根节点 root ,返回它节点值的 前序 遍历。2.输入输出示例示例 1:输入:root = [1,nu
- 手动编写 SQL 语句和映射实体类的过程常常是繁琐且易出错的。这时,我们就可以借助 MyBatis Generator (MBG) 这个强大
- 本文实例讲述了Java正则验证电话,手机,邮箱,日期,金额的方法。分享给大家供大家参考,具体如下:package com.hooypay.t
- 本文实例讲述了C#中DataGridView常用操作。分享给大家供大家参考。具体如下:public void Binder1(){ Data
- demo下载重要代码://1、此layout作为最外层的layout;//2、设置需要调整的view: setAdjustView(View
- 本文实例为大家分享了Android实现3D云标签效果的具体代码,供大家参考,具体内容如下一、自定义Viewpublic class TagC
- 本文是作者结合网上的一些资料封装的java图片处理类,支持图片的缩放,旋转,马赛克化。不多说,上代码:package deal;import
- ClickHouse应用场景:1.绝大多数请求都是用于读访问的2.数据需要以大批次(大于1000行)进行更新,而不是单行更新;或者根本没有更
- 目录两种方案侵入式防抖处理(NoShakeClickListener)Java 版本Kotlin版本RxJava2 clickExt.kt无
- Android里判断是否可以上网,常用的是如下方法:/** * 检测网络是否连接 * * @return */private boolea
- Feign Client 超时时间配置不生效解决方案Feign Client 的 connectTimeout 和 readTimeout
- 前言这段时间比较闲,就看起了jdk源码。一般的一个高级开发工程师, 能阅读一些源码对自己的提升还是蛮大的。本文总结了一些JDK源码中的“小技
- 这是个我在C#调用批处理文件时遇到的问题。首先我通过Process.Start方法调用一个批处理文件,那个批处理文件里面则调用了一大堆程序。
- 前言:在没有接触java8的时候,我们遍历一个集合都是用循环的方式,从第一条数据遍历到最后一条数据,现在思考一个问题,为什么要使用循环,因为
- 该项目主要实现mybatisplus、多数据源、lombok、druid的集成主要参考 https://mp.baomidou.com/gu
- @Lazy用于指定该Bean是否取消预初始化。主要用于修饰Spring Bean类,用于指定该Bean的预初始化行为,使用该Annotati
- 本文实例讲述了C#图像颜色聚类高效方法。分享给大家供大家参考。具体分析如下:图像颜色聚类的方法有很多,但是对于视频监控而言,现有方法很难满足
- 本文介绍了ListView给每个Item上面的按钮添加事件,具体如下:1.先看下效果图:在这里仅供测试,我把数据都写死了,根据需要可以自己进
- 一、ObjectContext对象上下文Entity SQL 语言 - ADO.NET | Microsoft 官当文档ObjectCont