RecyclerView实现item移动效果(仅上下)

效果图:

RecyclerView实现item移动效果(仅上下)_第1张图片

大概就是这么个效果,这个完全是design里面效果,下面的代码中有注释,这里就不多讲解了.

代码:

public class MainActivity extends AppCompatActivity {
    private List mStringList = new ArrayList<>();
    private RecyclerView recyclerView;
    private MyAdapter mMyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        for (int i = 0;i<20;i++){
            mStringList.add("拉拉啦"+i);
        }

        mMyAdapter = new MyAdapter(mStringList,this);
        recyclerView.setAdapter(mMyAdapter);

        ItemTouchHelper.Callback callback = new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {

                //监控上下左右
                int swipFlag = 0;//ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT
                int dragflag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;

                return makeMovementFlags(dragflag,swipFlag);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {

                mMyAdapter.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
                //item移动后  也要移动数据
                Collections.swap(mStringList,viewHolder.getAdapterPosition(),target.getAdapterPosition());
                return true;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                //暂不处理  这个主要是做左右拖动的回调

            }

            @Override
            public boolean canDropOver(RecyclerView recyclerView, RecyclerView.ViewHolder current, RecyclerView.ViewHolder target) {
                //需要手动重写的方法,返回true
                //当前item可以被拖动到目标位置后,直接落到target上,后面的item也接着落
                return true;
            }

            @Override
            public boolean isLongPressDragEnabled() {
                //是否开启长按拖动
                //返回true  可以实现长按拖动排序和拖动动画
                return true;
            }

        };

        //创建item helper
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);

        //绑定到recyclerView上面
        itemTouchHelper.attachToRecyclerView(recyclerView);

    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        //设置布局
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
    }
}

适配器:

public class MyAdapter extends RecyclerView.Adapter{
    private List mStringList;
    private Context mContext;

    public MyAdapter(List stringList, Context context) {
        mStringList = stringList;
        mContext = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item, null);
        MyViewHolder holder = new MyViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        holder.text.setText(mStringList.get(position));
    }

    @Override
    public int getItemCount() {
        return mStringList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView text;

        public MyViewHolder(View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
        }
    }
}

布局文件就省略了

 

你可能感兴趣的:(RecyclerView实现item移动效果(仅上下))