android实现简单左滑删除控件
作者:qq_20352713 时间:2023-11-10 03:01:29
本文为大家分享了一个简单的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
标签:android,删除控件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解C# 匿名对象(匿名类型)、var、动态类型 dynamic
2022-03-26 18:43:02
![](https://img.aspxhome.com/file/2023/8/72458_0s.png)
springboot logback调整mybatis日志级别无效的解决
2023-05-03 05:36:21
实例讲解Java 自旋锁
2021-09-17 09:38:50
![](https://img.aspxhome.com/file/2023/4/70914_0s.png)
Java Maven高级之插件开发详解
2023-05-11 19:10:02
![](https://img.aspxhome.com/file/2023/6/79306_0s.png)
通过Java连接SQL Server数据库的超详细操作流程
2022-08-01 12:46:01
![](https://img.aspxhome.com/file/2023/8/63098_0s.png)
使用代理模式来进行C#设计模式开发的基础教程
2023-06-08 17:01:56
![](https://img.aspxhome.com/file/2023/2/104272_0s.gif)
Mybatis实现单个和批量定义别名typeAliases
2023-02-11 06:27:03
Jackson反序列化@JsonFormat 不生效的解决方案
2023-06-15 20:25:37
![](https://img.aspxhome.com/file/2023/0/128820_0s.jpg)
详解基于java的Socket聊天程序——客户端(附demo)
2021-06-28 04:19:54
![](https://img.aspxhome.com/file/2023/6/114036_0s.png)
java泛型详解
2023-06-07 23:54:47
react native打包apk文件安装好之后进入应用闪退的解决方案
2022-11-04 06:13:09
java中拼接字符串的5种方法效率对比
2022-01-08 05:46:18
![](https://img.aspxhome.com/file/2023/5/65155_0s.jpg)
Java Swing GridBagLayout网格袋布局的实现
2023-07-11 19:34:26
![](https://img.aspxhome.com/file/2023/8/74908_0s.png)
基于Spring概念模型:PathMatcher 路径匹配器
2022-08-20 12:52:38
解决fastjson从1.1.41升级到1.2.28后报错问题详解
2021-12-30 21:55:35
Springboot通过谷歌Kaptcha 组件生成图形验证码功能
2021-07-24 23:42:47
![](https://img.aspxhome.com/file/2023/9/69659_0s.png)
Android编程实现任务管理器的方法
2022-11-30 00:32:15
![](https://img.aspxhome.com/file/2023/5/122105_0s.png)
java生成json实现隐藏掉关键属性
2021-12-07 17:31:29
springboot编程式事务TransactionTemplate的使用说明
2022-03-01 15:19:37
C#语法相比其它语言比较独特的地方(二)
2021-12-20 20:56:03