Google 相册风格的RecyclerView多选效果: drag-select-recyclerview

 
     
0.0
收藏    1收藏

Google 相册风格的RecyclerView多选效果,手指滑动所到之处都被选中。不过与谷歌相册的差距是一次只能选择一屏的item。

Gradle依赖

build.gradle 文件:

repositories {// ...maven { url "https://jitpack.io" }
}dependencies {// ...compile('com.afollestad:drag-select-recyclerview:0.1.0@aar') {transitive = true}
}

介绍

本库主要的两个类是DragSelectRecyclerView和DragSelectRecyclerViewAdapter。两个一起工作为你提供需要的功能。

DragSelectRecyclerView

DragSelectRecyclerView取代你通常在布局中使用的RecyclerView。它拦截触摸事件判断选择模式是否处于激活状态,然后自动向你的adapter报告。

<com.afollestad.dragselectrecyclerview.DragSelectRecyclerViewandroid:id="@+id/list"android:layout_width="match_parent"android:layout_height="match_parent"android:scrollbars="vertical" />

设置基本上和普通RecyclerView一样。你必须为他设置一个LayoutManager与RecyclerView.Adapter:

DragSelectRecyclerView list = (DragSelectRecyclerView) findViewById(R.id.list);
list.setLayoutManager(new GridLayoutManager(this, 3));
list.setAdapter(adapter);

这里最主要的区别就是 setAdapter()里面放入的东西,它不能是普通的RecyclerView.Adapter,必须是DragSelectRecyclerViewAdapter的子类。下面将会讨论。

DragSelectRecyclerViewAdapter

DragSelectRecyclerViewAdapter是一个DragSelectRecyclerView可以与之交互的RecyclerView.Adapter 的子类。它跟踪被选中的索引-让你可以改变它们,清除它们,监听它们的变化以及检查某个索引是否被选中。

最基本的adapter实现如下:

public class MainAdapter extends DragSelectRecyclerViewAdapter<MainAdapter.MainViewHolder>implements View.OnClickListener, View.OnLongClickListener {// Receives View.OnClickListener set in onBindViewHolder(), tag contains index@Overridepublic void onClick(View v) {if (v.getTag() != null) {int index = (int) v.getTag();// Forwards to the adapter's constructor callbackif (mCallback != null) mCallback.onClick(index);}}// Receives View.OnLongClickListener set in onBindViewHolder(), tag contains index@Overridepublic boolean onLongClick(View v) {if (v.getTag() != null) {int index = (int) v.getTag();// Forwards to the adapter's constructor callbackif (mCallback != null) mCallback.onLongClick(index);return true;}return false;}public interface ClickListener {void onClick(int index);void onLongClick(int index);}private final ClickListener mCallback;// Constructor takes click listener callbackprotected MainAdapter(ClickListener callback) {super();mCallback = callback;}@Overridepublic MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.griditem_main, parent, false);return new MainViewHolder(v);}@Overridepublic void onBindViewHolder(MainViewHolder holder, int position) {// Sets position + 1 to a label viewholder.label.setText(String.format("%d", position + 1));if (isIndexSelected(position)) {// Item is selected, change it somehow } else {// Item is not selected, reset it to a non-selected state}// Tag is used to retrieve index from the click/long-click listenersholder.itemView.setTag(position);holder.itemView.setOnClickListener(this);holder.itemView.setOnLongClickListener(this);}@Overridepublic int getItemCount() {return 60;}public class MainViewHolder extends RecyclerView.ViewHolder {public final TextView label;public MainViewHolder(View itemView) {super(itemView);this.label = (TextView) itemView.findViewById(R.id.label);}}
}

You choose what to do when an item is selected (in onBindViewHolder). isIndexSelected(int)returns true or false. The click listener implementation used here will aid in the next section.

你自己选择在一个item被选择(在onBindViewHolder中)的时候做什么。


用户激活

除非你告诉library,否则它不会启用选择模式。用户要能够自己激活它,上面adapter中click listener的实现可以帮助你做到这点。

public class MainActivity extends AppCompatActivity implementsMainAdapter.ClickListener, DragSelectRecyclerViewAdapter.SelectionListener {private DragSelectRecyclerView mList;private MainAdapter mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Setup adapter and callbacksmAdapter = new MainAdapter(this);// Listen for selection changes to update indicatorsmAdapter.setSelectionListener(this);// Restore selected indices after Activity recreationmAdapter.restoreInstanceState(savedInstanceState);// Setup the RecyclerViewmList = (DragSelectRecyclerView) findViewById(R.id.list);mList.setLayoutManager(new GridLayoutManager(this, getResources().getInteger(R.integer.grid_width)));mList.setAdapter(mAdapter);}@Overridepublic void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {super.onSaveInstanceState(outState, outPersistentState);// Save selected indices to be restored after recreationmAdapter.saveInstanceState(outState);}@Overridepublic void onClick(int index) {// Single click will select or deselect an itemmAdapter.toggleSelected(index);}@Overridepublic void onLongClick(int index) {// Long click initializes drag selection, and selects the initial itemmList.setDragSelectActive(true, index);}@Overridepublic void onDragSelectionChanged(int count) {// TODO Selection was changed, updating an indicator, e.g. a Toolbar or contextual action bar}
}

DragSelectRecyclerViewAdapter包含了许多可以帮助你的方法!

// Clears all selected indices
adapter.clearSelected();// Sets an index as selected (true) or unselected (false);
adapter.setSelected(index, true);// If an index is selected, unselect it. Otherwise select it. Returns new selection state.
boolean selectedNow = adapter.toggleSelected(index);// Gets the number of selected indices
int count = adapter.getSelectedCount();// Gets all selected indices
Integer[] selectedItems = adapter.getSelectedIndices();// Checks if an index is selected, useful in adapter subclass
boolean selected = adapter.isIndexSelected(index);// Sets a listener that's notified of selection changes, used in the section above
adapter.setSelectionListener(listener);// Used in section above, saves selected indices to Bundle
adapter.saveInstanceState(outState);// Used in section above, restores selected indices from Bundle
adapter.restoreInstanceState(inState);

项目主页:http://www.open-open.com/lib/view/home/1446619651232

Google 相册风格的RecyclerView多选效果: drag-select-recyclerview相关推荐

  1. C++ Google代码风格

    C++ Google代码风格 0 index(扉页) 0.0 声明 0.1 译者前言 0.2 背景 headers(头文件) 1.1 Self-contained 头文件 1.2. #define 保 ...

  2. “独裁者”Google:开发者别无他选!| 极客头条

    "Google AMP导致我们的网页速度降低,但我们别无他选." 一直以来,Google 都拥有着至上的霸权地位,其现有的网速排名可以轻而易举地限制网站曝光度.AMP 虽然是一个开 ...

  3. 笔记本电脑与台式机同步连接_如何将台式机与Google云端硬盘(和Google相册)同步...

    笔记本电脑与台式机同步连接 Google has been doing its part to make sure everyone has a backup of important data, a ...

  4. Google C++ 风格指南内容整理

    之前一直没有全面的看过Google C++风格指南,现在很多公司进行C++开发都要求按照Google C++风格.在这个网站 http://zh-google-styleguide.readthedo ...

  5. Google相册元数据修复

    前提 承接上一篇<如何批量导出Google相册所有数据> 根据上一篇的方法导出的归档数据,往往许多信息都被抹除了,也就是Meta信息丢失,其中包括但不限于照片的定位信息(经纬度).拍摄时间 ...

  6. 谷歌云端硬盘 转存_如何合并多个Google云端硬盘和Google相册帐户

    谷歌云端硬盘 转存 It isn't possible to merge Google accounts directly, making it tricky to move your data fr ...

  7. Ajax-实现Google Suggest风格搜索

    1.上图简单模拟了下Google Suggest风格的搜索框,主要就是采用Ajax,Html,Css,js技术,后端采用原始的Servlet.实现起来比较容易,这里就不详细介绍了,只就需要注意的一点做 ...

  8. bootstrap-select实现下拉框多选效果

    bootstrap-select实现下拉框多选效果 听语音 在学习bootstrap实现下拉多选效果的时候,觉得该效果很好,所以拿来分享下,这里就不详细的描述了,直接附上代码给各位看看 方法/步骤 1 ...

  9. 复选框全选效果,根据单个复选框的选择情况确定全选复选框是否被选

    复选框全选与全不选效果 逻辑:复选框的checked属性与复选框全选效果对应起来,全选复选框checked:true 时,全部复选框checked:true: 全选框checked:false 时,全 ...

最新文章

  1. springBoot @Scheduled多任务同时开始执行
  2. hp-ux 11.23挂载ISO文件
  3. c#_MessageBox 消息对话框
  4. golang平滑重启
  5. php常用操作字符串函数,php字符串几个常用的操作函数
  6. 双手无法敲代码的程序员,该如何编程?
  7. 水性油墨在纺织品印花中的应用
  8. 【资料分享】干货解读人工智能新时代
  9. Excel常用的功能
  10. 计算机用户登录电脑蓝屏,电脑蓝屏怎么办,教您解决电脑蓝屏的方法
  11. 花生壳域名申请、内网映射到树莓派及与微信公众号对接
  12. html 防网页假死,html5 Web开发:防止浏览器假死的方法
  13. 风陵渡口初相遇,一见杨过误终身
  14. 飞思卡尔 I.MX6Q-vpu视频编解码
  15. C语言负整数在内存中的存储
  16. java 短信_java实现发送手机短信
  17. 系统集成项目管理工程师各种口诀技巧分享(3)
  18. 怎么维护linux环境,Linux系统是如何维护时间的
  19. 全球疫苗接种状况数据分析
  20. 风光储交直流微电网matlab/simulink仿真模型仿真模型

热门文章

  1. Pycharm安装第三方库显示 nothing to show问题
  2. 数据结构——串中kmp算法求模式串中next函数值
  3. java 笔记 @Tim大叔
  4. java简单工厂模式实例造车,JAVA设计模式学习5——工厂方法模式
  5. 农行赵维平:农业银行自主可控的大数据平台建设
  6. 总结CNC编程的工艺方法
  7. 漫画:大数据的社交牛逼症是怎么得的?
  8. python 给QQ好友定时发送消息
  9. 自动售货机控制模块-Verilog HDL
  10. [LabVIEW]简单自动售货机程序,加入文件IO,导出销售记录