可拖拽移位,可删除的listview功能 recyclerView

                 本来打算想个 文艺的开头,后来想想还是算了。向来 粗暴直接。

源码地址  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上一些好的开源项目,进行了些修改。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值