模仿QQ左滑删除
需求:
1.左滑删除
2.向左滑动距离超过一半的时候让它自动滑开,向右滑动超过一半的时候自动隐藏
3.一次只允许滑开一个item
还有,根本不需要自定义view来实现,谨防入坑
布局:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/browse_record_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/browse_record_image"
android:layout_width="128dp"
android:layout_height="78dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
android:src="@drawable/banner_zhibo" />
<TextView
android:id="@+id/browse_record_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/browse_record_image"
android:ellipsize="end"
android:includeFontPadding="false"
android:lineSpacingExtra="0dp"
android:lines="2"
android:text="空中读书会"
android:textColor="@color/c32"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="@+id/browse_delete"
android:layout_width="65dp"
android:layout_height="match_parent"
android:background="@color/red"
android:gravity="center"
android:text="删除"
android:textColor="@color/white"
android:textSize="16sp" />
</LinearLayout>
</HorizontalScrollView>
adapter的getview中:
//隐藏删除按钮
ViewGroup.LayoutParams params = holder.browse_record_layout.getLayoutParams();
int screenW = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
params.width = screenW;
//自动滑开和自动隐藏效果
holder.scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
// 手抬起的时候判断滑动距离
int slideDistance = holder.scrollView.getScrollX();// 获取向左滑动的距离,是一个非负数,px
int delete = holder.browse_delete.getWidth();// 删除按钮的宽度,单位px
// 当滑动距离大于删除按钮宽度的一半时,就自动滑动到最左边,完全显示删除按钮,所谓的最左边就是滑动距离等于删除按钮宽度
// 当滑动距离小于删除按钮宽度的一半时,就隐藏删除按钮,即滑动距离等于0的位置
if (slideDistance >= delete / 2) {
holder.scrollView.scrollTo(delete, 0);
currentSlideView = holder.scrollView;// 保存当前滑开的item
} else {
holder.scrollView.scrollTo(0, 0);
currentSlideView = null;// 清空
}
break;
case MotionEvent.ACTION_MOVE:
// 滑动的时候判断当前有没有其它滑开的item,有的话就隐藏
if (currentSlideView != holder.scrollView) {
autoHide();
}
break;
default:
break;
}
return true;
}
});
//删除
holder.browse_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//删除一条记录
list.remove(position);
autoHide();
notifyDataSetChanged();
}
});
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现