本来打算想个 文艺的开头,后来想想还是算了。向来 粗暴直接。
源码地址 https://github.com/alex1127/SpecialEffectsBox此源码还包含其他功能特效。请确保 build.gradle文件中
compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:recyclerview-v7:23.1.1'自己使用过24的包,但是启动报错。后续将分离出独立的功能参考了git上一些好的开源项目,进行了些修改。
此功能是通过 RecyclerView 来实现的。关于次view的使用,可以查看官方文档。
实现起来 还是比较简单的。
核心代码:
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setAdapter(myAdapterRecyclerView);
//这个 不解释,用过listview的 应该很清楚。
ItemTouchHelper.Callback callback = new MyItemTouchHelperCallback(this);//这里的this 是当前activity
activity实现了ItemTouchCallback。
// MyItemTouchHelperCallback是实现了ItemTouchHelper.Callback 的子类。
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
//创建 Itemtouchhelper 用来绑定 RecyclerView touchHelper.attachToRecyclerView(mRecyclerView); // Attach ItemTouchHelper to RecyclerView
之后RecyclerView的 item的 touch 拖拽事件会通过 MyItemTouchHelperCallback 回调出来。
//============================我是分割线================================================
MyItemTouchHelperCallback.java
public class MyItemTouchHelperCallback extends ItemTouchHelper.Callback { CallbackItemTouch callbackItemTouch; // interface public MyItemTouchHelperCallback(CallbackItemTouch callbackItemTouch) { this.callbackItemTouch = callbackItemTouch; } @Override public boolean isLongPressDragEnabled() { return true; } @Override public boolean isItemViewSwipeEnabled() { return true; // swiped disabled } @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; // movements drag int swipeFlags = ItemTouchHelper.START| ItemTouchHelper.END; return makeMovementFlags(dragFlags, swipeFlags); // as parameter, action drag and flags drag } @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { callbackItemTouch.itemTouchOnMove(viewHolder.getAdapterPosition(), target.getAdapterPosition()); // information to the interface return true; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { callbackItemTouch.itemTouchOnSwiped(viewHolder.getAdapterPosition()); } }源码地址 https://github.com/alex1127/SpecialEffectsBox此源码还包含其他功能特效。请确保 build.gradle文件中
compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:recyclerview-v7:23.1.1'自己使用过24的包,但是启动报错。后续将分离出独立的功能参考了git上一些好的开源项目,进行了些修改。