前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RecycleView的拖动排序

RecycleView的拖动排序

作者头像
code_horse
发布2018-07-02 15:15:53
1.5K0
发布2018-07-02 15:15:53
举报
文章被收录于专栏:Android Note

列表拖动排序功能也算是比较常见的了。在RecycleView还没有出现的那个年代,依稀记得是重写GridView实现拖拽,WindowManager实现拖拽的镜像。

不过,现在好了,有了RecycleView之后,一切都变得简单了。因为RecycleView提供了拖动的回调ItemTouchHelper.Callback,通过实现该类,会让我们少写很多代码。

先看一下,简化后的Demo效果图。

这里的实现的效果是: ① 第一个标签不可编辑 ② 除了第一个标签外,其它标签可拖拽排序和删除

ItemTouchHelper.Callback的实现类

代码语言:javascript
复制
public class ItemDragCallback extends ItemTouchHelper.Callback {
    private ChannelAdapter mAdapter;
    private boolean mEdit;

    public ItemDragCallback(ChannelAdapter mAdapter) {
        this.mAdapter = mAdapter;
    }

    
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        ChannelAdapter adapter = (ChannelAdapter) recyclerView.getAdapter();
        mEdit = adapter.isEdit;
        //是否是编辑状态
        if (!mEdit) {
            return 0;
        }
        int position = viewHolder.getLayoutPosition();
        //第一个item不用交换
        if (position == 0) {
            return 0;
        }
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        int swipeFlags = 0;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int fromPosition = viewHolder.getAdapterPosition();   //拖动的position
        int toPosition = target.getAdapterPosition();     //释放的position
        int position = viewHolder.getLayoutPosition();
        //第一个item不用交换
        if (position == 0) {
            return false;
        }
        mAdapter.itemMove(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    }


    @Override
    public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        if (dX != 0 && dY != 0 || isCurrentlyActive) {
            ChannelAdapter adapter = (ChannelAdapter) recyclerView.getAdapter();
            mEdit = adapter.isEdit;
        }
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
        if (actionState == ACTION_STATE_DRAG) {
            //长按时调用
            ChannelAdapter.ChannelHolder holder = (ChannelAdapter.ChannelHolder) viewHolder;
            holder.name.setBackgroundColor(Color.parseColor("#FDFDFE"));
            holder.name.setBackground(ContextCompat.getDrawable(ProjectApp.getApp(), R.drawable.shape_channel_bg));
            holder.delete.setVisibility(View.GONE);
        }
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        //松手时调用
        ChannelAdapter.ChannelHolder holder = (ChannelAdapter.ChannelHolder) viewHolder;
        holder.name.setBackground(ContextCompat.getDrawable(ProjectApp.getApp(), R.drawable.shape_channel_bg));
        holder.delete.setVisibility(View.VISIBLE);
    }
}
代码语言:javascript
复制
public class ChannelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<ChannelBean> mSortedList;
    public boolean isEdit;//是否是编辑状态

    public ChannelAdapter(Context mContext, List<ChannelBean> list) {
        this.mContext = mContext;
        mSortedList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_channel, parent, false);
        return new ChannelHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        setChannel((ChannelHolder) holder, mSortedList.get(position));
    }


    private void setChannel(final ChannelHolder holder, final ChannelBean bean) {
        holder.name.setText(bean.getName());
        int position = holder.getLayoutPosition();
        //点击标签删除已选标签
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!bean.isLock()) {
                    if (isEdit) {
                        removeFromSelected(holder);
                    }
                } else {
                }
            }
        });
        //长按进入编辑状态
        holder.name.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                    isEdit = true;
                    notifyDataSetChanged();
                //返回true 防止长按拖拽事件跟点击事件冲突
                return true;
            }
        });
        //点击X删除已选标签
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeFromSelected(holder);
            }
        });
        //是否是编辑状态且第一个不可编辑
        if (isEdit&&position!=0) {
            holder.delete.setVisibility(View.VISIBLE);
        } else {
            holder.delete.setVisibility(View.GONE);
        }
    }

    /**
     * 删除已选标签
     * @param holder
     */
    private void removeFromSelected(ChannelHolder holder) {
        int position = holder.getLayoutPosition();
        holder.delete.setVisibility(View.GONE);
        notifyItemRemoved(position);
        mSortedList.remove(position);
        notifyDataSetChanged();
    }

    /**
     * 对拖拽的元素进行排序
     * @param fromPosition
     * @param toPosition
     */
    void itemMove(int fromPosition, int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(mSortedList, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(mSortedList, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public int getItemCount() {
        return mSortedList == null ? 0 : mSortedList.size();
    }

    class ChannelHolder extends RecyclerView.ViewHolder {
        TextView name;
        ImageView delete;

        public ChannelHolder(View itemView) {
            super(itemView);

            int validWidth = ScreenUtil.getScreenWidth() - Px2DpUtil.dp2px(mContext, 45);
            int childWidth = validWidth / 4;
            name = (TextView) itemView.findViewById(R.id.channel_name);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(childWidth, Px2DpUtil.dp2px(mContext, 35));
            params.setMargins(0, Px2DpUtil.dp2px(mContext, 5), 0, 0);
            name.setLayoutParams(params);
            name.setGravity(Gravity.CENTER);
            delete = (ImageView) itemView.findViewById(R.id.channel_delete);
        }
    }
}

调用就比较简单了

代码语言:javascript
复制
ItemDragCallback callback = new ItemDragCallback(mAdapter);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(mRecyclerView);

到这里就结束了。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.05.03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档