Android滑动列表(拖拽,左滑删除,右滑完成)功能实现(2)
ItemTouchHelper类
之前我们实现了滑动列表的一些基本功能,为了实现更多的效果,我们来仔细看一下ItemTouchHelper中的类:
ItemTouchHelper.SimpleCallback
这个主要是帮你写好了getMovementFlags,你只要直接传一些参数即可,看一下怎么用:
ItemTouchHelper mIth = new ItemTouchHelper(
new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
ItemTouchHelper.LEFT) {
public abstract boolean onMove(RecyclerView recyclerView,
ViewHolder viewHolder, ViewHolder target) {
final int fromPos = viewHolder.getAdapterPosition();
final int toPos = target.getAdapterPosition();
// move item in `fromPos` to `toPos` in adapter.
return true;// true if moved, false otherwise
}
public void onSwiped(ViewHolder viewHolder, int direction) {
// remove from adapter
}
});
ItemTouchHelper.ViewDropHandler
An interface which can be implemented by LayoutManager for better integration with ItemTouchHelper
.
getItemOffsets
void getItemOffsets (Rect outRect,
View view,
RecyclerView parent,
RecyclerView.State state)
Retrieve any offsets for the given item. Each field of outRect
specifies the number of pixels that the item view should be inset by, similar to padding or margin. The default implementation sets the bounds of outRect to 0 and returns.
If this ItemDecoration does not affect the positioning of item views, it should set all four fields of outRect
(left, top, right, bottom) to zero before returning.
If you need to access Adapter for additional data, you can call getChildAdapterPosition(View)
to get the adapter position of the View.
类似于itemDecoration里的getItemOffsets,也是用于给view设置padding的: outRect.set(l,t,r,b)
onDraw
void onDraw (Canvas c,
RecyclerView parent,
RecyclerView.State state)
Draw any appropriate decorations into the Canvas supplied to the RecyclerView. Any content drawn by this method will be drawn before the item views are drawn, and will thus appear underneath the views.
还有个onDrawOver方法,不过是用于之后的
startDrag | startSwipe
void startDrag (RecyclerView.ViewHolder viewHolder)
Starts dragging the provided ViewHolder. By default, ItemTouchHelper starts a drag when a View is long pressed. You can disable that behavior by overriding isLongPressDragEnabled()
.
For this method to work:
- The provided ViewHolder must be a child of the RecyclerView to which this ItemTouchHelper is attached.
ItemTouchHelper.Callback
must have dragging enabled.- There must be a previous touch event that was reported to the ItemTouchHelper through RecyclerView's ItemTouchListener mechanism. As long as no other ItemTouchListener grabs previous events, this should work as expected.
For example, if you would like to let your user to be able to drag an Item by touching one of its descendants, you may implement it as follows:
viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mItemTouchHelper.startDrag(viewHolder);
}
return false;
}
});
用于开始拖动的一些时间
ItemTouchHelper.Callback
canDropOver
boolean canDropOver (RecyclerView recyclerView,
RecyclerView.ViewHolder current,
RecyclerView.ViewHolder target)
Return true if the current ViewHolder can be dropped over the the target ViewHolder.
This method is used when selecting drop target for the dragged View. After Views are eliminated either via bounds check or via this method, resulting set of views will be passed to chooseDropTarget(ViewHolder, java.util.List, int, int)
.
Default implementation returns true.
用于判断某些target是否能拖过去
chooseDropTarget
RecyclerView.ViewHolder chooseDropTarget (RecyclerView.ViewHolder selected,
List<RecyclerView.ViewHolder> dropTargets,
int curX,
int curY)
Called by ItemTouchHelper to select a drop target from the list of ViewHolders that are under the dragged View.
Default implementation filters the View with which dragged item have changed position in the drag direction. For instance, if the view is dragged UP, it compares the view.getTop()
of the two views before and after drag started. If that value is different, the target view passes the filter.
Among these Views which pass the test, the one closest to the dragged view is chosen.
This method is called on the main thread every time user moves the View. If you want to override it, make sure it does not do any expensive operations.
clearView
void clearView (RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder)
Called by the ItemTouchHelper when the user interaction with an element is over and it also completed its animation.
This is a good place to clear all changes on the View that was done inonSelectedChanged(RecyclerView.ViewHolder, int)
, onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int, boolean)
or onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, boolean)
.
convertToAbsoluteDirection | convertToRelativeDirection
int convertToAbsoluteDirection (int flags,
int layoutDirection)
Converts a given set of flags to absolution direction which means START
and END
are replaced with LEFT
and RIGHT
depending on the layout direction.
onSelectedChanged
void onSelectedChanged (RecyclerView.ViewHolder viewHolder,
int actionState)
Called when the ViewHolder swiped or dragged by the ItemTouchHelper is changed.
If you override this method, you should call super.
实例
实现拖拽时改变颜色
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) { originColor=((ColorDrawable)viewHolder.itemView.getBackground()).getColor();
viewHolder.itemView.setBackgroundColor(Color.BLUE);
}
} @Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setBackgroundColor(originColor);
}
Android滑动列表(拖拽,左滑删除,右滑完成)功能实现(2)的更多相关文章
- Android滑动列表(拖拽,左滑删除,右滑完成)功能实现(1)
场景: 近期做的TODO APP需要在主页添加一个功能,就是可以左滑删除,右滑完成.看了一下当前其他人做的例如仿探探式的效果,核心功能基本一样,但是和我预想的还是有少量区别,于是干脆自己重头学一遍如何 ...
- RecyclerView进阶:使用ItemTouchHelper实现拖拽和侧滑删除
现在RecyclerView的应用越来越广泛了,不同的应用场景需要其作出不同的改变.有时候我们可能需要实现侧滑删除的功能,比如知乎首页的侧滑删除,又或者长按Item进行拖动与其他Item进行位置的交换 ...
- ListView列表拖拽排序
ListView列表拖拽排序能够參考Android源代码下的Music播放列表,他是能够拖拽的,源代码在[packages/apps/Music下的TouchInterceptor.java下]. 首 ...
- h5页面ios,双击向上滑动,拖拽到底部还能继续拖拽(露出黑色背景)
h5页面ios,双击向上滑动,拖拽到底部还能继续拖拽 标签: 手机 2016-02-02 18:09 696人阅读 评论(0) 收藏 举报 在ios下,双击屏幕某些地方,滚动条会自动向上走一段. ...
- Appium(九):Appium API(三) 滑动和拖拽、高级手势、手机操作
1. 滑动和拖拽 我们在做自动化测试的时候,有些按钮是需要滑动几次屏幕后才会出现,此时,我们需要使用代码来模拟手指的滑动,也就是接下来要学的滑动和拖拽了. 1.1 swipe滑动事件 从一个坐标位置滑 ...
- Taro UI开发小程序实现左滑喜欢右滑不喜欢效果
前言:年后入职了一家新公司,与前同事交接完之后,发现公司有一个四端的项目(iOS,Android,H5,小程序),iOS和安卓都实现了左滑右滑的效果,而h5和小程序端没实现,询问得知前同事因网上没找到 ...
- js判断手指的上滑,下滑,左滑,右滑,事件监听
原理:1:当开始一个touchstart事件的时候,获取此刻手指的横坐标startX和staerY: 2:当触发touchmove事件的时候,再获取此时手指的横坐标moveEndX和纵坐标moveEn ...
- Android中GridView拖拽的效果【android进化三十六】
最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的 ...
- Android中GridView拖拽的效果
最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的那么 ...
随机推荐
- php的api接口
在实际工作中,使用PHP写api接口是经常做的,PHP写好接口后,前台就可以通过链接获取接口提供的数据,而返回的数据一般分为两种情况,xml和json,在这个过程中,服务器并不知道,请求的来源是什么, ...
- LOJ #2978「THUSCH 2017」杜老师
听说LOJ传了THUSC题赶紧上去看一波 随便点了一题都不会做想了好久才会写暴力爆了一发过了... LOJ #2978 题意 $ T$次询问,每次询问$ L,R$,问有多少种选取区间中数的方案使得选出 ...
- 开发一个项目之npm
npm (nodejs平台上写的js模块的管理工具 下载.互相依赖等) npm install 本地项目的node_modules文件夹 , -g npm config prefix 目录eg: ...
- 数据库运维平台~Yearning测试与总结
一 部署 1 docker-compose up -d 即可非常方便 2 mysql安装 3 inception安装二 默认账号 默认用户admin 密码: Yearning_admi ...
- JS 循环定时的一些思考
网上也有例子, function doSetTimeout(i) { setTimeout(function() { console.log(i); }, 1000); } for (var i = ...
- 医学图像数据(二)——TCIA完整数据集下载方式
1. 构建下载环境 l TCIA数据集下载文件为.jnlp格式(JNLP(Java Network Launching Protocol )是java提供的一种可以通过浏览器直接执行java应用程序 ...
- iView -- TimePicker 自定义修改时间选择器选择时间面板样式
iView官方组件展示效果: 期望的最终效果: 为什么要修改期望效果? 项目需要只选择小时,分钟跟秒的不需要,而官方并没有直接相关的小时组件或者是设置显示成小时或分钟或秒的时间选择器,因为自己直接修改 ...
- SQLAlchemy+Flask-RESTful使用(四)
前言 顺利出到4啦,其实学习过程中发现了不少错误,不过有些实在是没啥代表性. 最近买了两本小程序和安卓方面的书,其实从初中开始,想搞编程的目的就是写些安卓软件. 现在看来不太可能了.拿来当当兴趣爱好还 ...
- NOIP2018Day1T1 铺设道路
题目描述 春春是一名道路工程师,负责铺设一条长度为 \(n\) 的道路. 铺设道路的主要工作是填平下陷的地表.整段道路可以看作是 \(n\) 块首尾相连的区域,一开始,第 \(i\) 块区域下陷的深度 ...
- Mac 下GitHub 访问慢解决方案
1.GitHub下载是指向了Amazon的服务器 下载地址是http://github-cloud.s3.amazonaws.com/ 解决方案是更改host文件,使该域名指向香港的服务器 2.去 ...