android 实现图片选择拖拽控件

发布时间:2020-08-01 11:01:00 作者:wutongkege
来源:网络 阅读:768

1.使用RecyclerView

使用RecyclerView可以轻松实现图片切换时的动画过程,这点要好于GridView。

2. 拖拽的实现

继承View.OnDragListener,对拖拽过程中进行操作,

Action_drag_started 获取到操作的Item

Action_Drag_location 根据每个停留的位置判断是否交换item的位置。

Action_Drag_ended 跟新位置

抽象类,为recyclerView 增加onItemTouchListener和onScrollListener, 记录onTouch的item,并在拖拽过程中判断recyclerview是否可以滚动,从而在拖拽的item快到边界时滚动recyclerView,使可以与本来在屏幕上不可见的item进行交换位置。

ViewHolder 实现startDrag方法


2.itemDecoration


为recyclerview item增加divider,可以有两种方式,覆盖onDraw方法绘制itemDivider,或者覆盖getItemOffsets方法,使item之间可以分隔开。

3.GridlayoutManager

当recyclerview嵌入到scrollview中时,需要复写LayoutManager,在这里复写其中的onMeasure方法,需要计算每个item的高度或者宽度进行叠加,当recyclerview中item很多时,不要采用这种方式,回导致view不能复用,其中在计算item高度时,需要加上每个item的itemOffsets,查看recyclerview的源码发现,无法直接获取到item的offsets,最终采用反射的方式获取到其值:

try {
    Method method = recyclerView.getClass().getDeclaredMethod("getItemDecorInsetsForChild",View.class);
    method.setAccessible(true);
    final Rect insets = (Rect)method.invoke(recyclerView, child);
    itemDecorationHeight = heightUsed + insets.height();
    itemDecorationWidth = widthUsed + insets.width();
} catch (NoSuchMethodException e){
    Log.d("FullGridLayoutManager","no method found");
}catch(IllegalAccessException e){
    Log.d("FullGridLayoutManager","IllegalAccessException");
}catch (InvocationTargetException e){
    Log.d("FullGridLayoutManager","InvocationTargetException");
}



推荐阅读:
  1. Android开发图片开源框架
  2. Android自定义实现侧滑菜单效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

图片 拖拽 android

上一篇:postgresql是什么数据库管理系统

下一篇:tensorflow如何使用freeze_graph.py将ckpt转为pb文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》