Google 相册风格的RecyclerView多选效果: drag-select-recyclerview
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.DragSelectRecyclerView android: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 @Override public void onClick(View v) { if (v.getTag() != null) { int index = (int) v.getTag(); // Forwards to the adapter's constructor callback if (mCallback != null) mCallback.onClick(index); } } // Receives View.OnLongClickListener set in onBindViewHolder(), tag contains index @Override public boolean onLongClick(View v) { if (v.getTag() != null) { int index = (int) v.getTag(); // Forwards to the adapter's constructor callback if (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 callback protected MainAdapter(ClickListener callback) { super(); mCallback = callback; } @Override public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.griditem_main, parent, false); return new MainViewHolder(v); } @Override public void onBindViewHolder(MainViewHolder holder, int position) { // Sets position + 1 to a label view holder.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 listeners holder.itemView.setTag(position); holder.itemView.setOnClickListener(this); holder.itemView.setOnLongClickListener(this); } @Override public 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 implements MainAdapter.ClickListener, DragSelectRecyclerViewAdapter.SelectionListener { private DragSelectRecyclerView mList; private MainAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Setup adapter and callbacks mAdapter = new MainAdapter(this); // Listen for selection changes to update indicators mAdapter.setSelectionListener(this); // Restore selected indices after Activity recreation mAdapter.restoreInstanceState(savedInstanceState); // Setup the RecyclerView mList = (DragSelectRecyclerView) findViewById(R.id.list); mList.setLayoutManager(new GridLayoutManager(this, getResources().getInteger(R.integer.grid_width))); mList.setAdapter(mAdapter); } @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { super.onSaveInstanceState(outState, outPersistentState); // Save selected indices to be restored after recreation mAdapter.saveInstanceState(outState); } @Override public void onClick(int index) { // Single click will select or deselect an item mAdapter.toggleSelected(index); } @Override public void onLongClick(int index) { // Long click initializes drag selection, and selects the initial item mList.setDragSelectActive(true, index); } @Override public 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);