android-RecyclerView实现拖动排序

发布时间 2023-04-10 14:03:50作者: 夜行过客

android: RecyclerView实现拖动排序

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

1. 布局文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView 
        android:id="@+id/rv_health_habit" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" />
</LinearLayout>

RecyclerView的Item的布局item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"          
        android:layout_width="match_parent"    
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:background="@color/white">

    <LinearLayout android:orientation="horizontal"  
        android:layout_width="match_parent" 
        android:layout_height="60dp">

        <RelativeLayout 
            android:layout_width="match_parent"   
            android:layout_height="match_parent"  
            android:layout_marginLeft="16dp">

            <TextView
                android:id="@+id/tv_content"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content" 
                android:layout_centerVertical="true"
                android:textSize="18dp"   
                android:textColor="@color/black" />

            <ImageView 
                android:layout_width="wrap_content"       
                android:layout_height="wrap_content"   
                android:layout_marginRight="16dp"     
                android:layout_alignParentRight="true"   
                android:layout_centerVertical="true"
                android:src="@drawable/ic_location"  />
        </RelativeLayout>
    </LinearLayout>  
</LinearLayout>

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<ViewHolder> {
        private List<dataItem> mDataList;

        public MyAdapter (List<item> 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<dataItem> 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实现拖动排序