RecyclerView实现拖动排序

最近项目中需要实现对某一类条目进行拖动排序功能,实现技术方案就是利用ItemTouchHelper绑定RecyclerView、ItemTouchHelper.Callback来实现UI更新,并且实现动态控制是否开启拖动功能。其中,ItemTouchHelper是Google在support-v7包中添加的,其于RecyclerView配合可以比较容易地实现这个功能。

1.布局文件

(1)activity_main.xml



    

(2)RecyclerView的Item的布局item.xml




    

        

            

            
        
      

2.实现ItemTouchHelper.Callback

callback = new ItemTouchHelper.Callback() {
        public int getMovementFlags(RecyclerView recyclerView, android.support.v7.widget.RecyclerView.ViewHolder viewHolder) {
                //首先回调的方法,返回int表示是否监听该方向
                int dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP;//拖拽
                int swipeFlag = 0;//侧滑删除
                return makeMovementFlags(dragFlag, swipeFlag);
            }

        public boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, android.support.v7.widget.RecyclerView.ViewHolder target) {
                if (mAdapter != null) {
                    mAdapter.onMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
                }
                return true;
            }

        public void onSelectedChanged(ViewHolder viewHolder, int actionState) {
                if (actionState != 0) {
                    viewHolder.itemView.setAlpha(0.9f);
                }
                super.onSelectedChanged(viewHolder, actionState);
            }

        public void clearView(RecyclerView recyclerView, android.support.v7.widget.RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);
                viewHolder.itemView.setAlpha(1.0f);
                if (mAdapter != null) {
                    mAdapter.notifyDataSetChanged();
                    mSortedList = mAdapter.getSortedDataList();
                }
            }
        };

3.RecyclerView绑定ItemTouchHelper

ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(recyclerView);

4.定义自己的Adapter

private class MyAdapter extends RecyclerView.Adapter {
        private List mDataList;

        public MyAdapter (List dataList) {
            this.mDataList = dataList;
        }

        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
        }

        public void onBindViewHolder(ViewHolder holder, final int position) {
            final dataItem item= mDataList.get(position);
            if (item != null) {
                holder.tvContent.setText(item.name);
            }
        }

        public int getItemCount() {
            return mDataList.size();
        }

        public void onMove(int fromPosition, int toPosition) {
            //对原数据进行移动
            Collections.swap(mDataList, fromPosition, toPosition);
            //通知数据移动
            notifyItemMoved(fromPosition, toPosition);
        }

        public List getSortedDataList() {
            return this.mDataList;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvContent;
        public ViewHolder(View view) {
            super(view);
            this.tvContent = (TextView)view.findViewById(R.id.tv_content);
        }
    }

你可能感兴趣的:(RecyclerView实现拖动排序)