当前位置:   article > 正文

RecyclerView实现拖拽效果_recycleview实现拖拽

recycleview实现拖拽

Google已经对RecyclerView适配好了,只需调用ItemTouchHelper,重写ItemTouchHelper.Callback再与RecyclerView即可实现拖拽效果。首先新建TestItemTouchHelperCallback.java继承ItemTouchHelper.Callback,代码如下:

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;


public class TestItemTouchHelperCallback extends ItemTouchHelper.Callback {
    @Override
    public int getMovementFlags(RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        if (recyclerView.getLayoutManager() instanceof GridLayoutManager || recyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
            final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
                    ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
            final int swipeFlags = 0;
            return makeMovementFlags(dragFlags, swipeFlags);
        } else {
            final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            final int swipeFlags = 0;
            return makeMovementFlags(dragFlags, swipeFlags);
        }
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        //拖拽中的viewHolder的Position
        int fromPosition = viewHolder.getAdapterPosition();
        //当前拖拽到的item的viewHolder
        int toPosition = target.getAdapterPosition();
        if (recyclerView.getAdapter() instanceof ItemMoveCallback) {
            ((ItemMoveCallback) recyclerView.getAdapter()).onItemMove(fromPosition, toPosition);
        }
        return true;
    }

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

    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE && viewHolder instanceof ItemHolderMoveCallback) {
            ((ItemHolderMoveCallback) viewHolder).onItemHolderMoveStart();
        }
    }

    @Override
    public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        if (viewHolder instanceof ItemHolderMoveCallback) {
            ((ItemHolderMoveCallback) viewHolder).onItemHolderMoveEnd();
        }
    }

    @Override
    public boolean isLongPressDragEnabled() {
        //返回true时自动实现长按拖拽效果
        return true;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

之后再初始化ItemTouchHelper并与RecyclerView绑定

 ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new TestItemTouchHelperCallback());
 temTouchHelper.attachToRecyclerView(mRvTest);
  • 1
  • 2

定义了两个回调,ItemMoveCallback和ItemHolderMoveCallback,回调拖拽状态给RecyclerView适配器处理。

public interface ItemMoveCallback {

    void onItemMove(int fromPosition, int toPosition);
}

public interface ItemHolderMoveCallback {
    void onItemHolderMoveStart();

    void onItemHolderMoveEnd();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

RecyclerView适配器监听ItemTouchHelper的回调,自定义RecyclerView适配器代码TestAdapter如下:

package com.example.myapplication;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;
import java.util.List;

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.TestViewHolder> implements ItemMoveCallback {

    private Context mContext;
    private List<String> mTestList;

    public TestAdapter(Context mContext, List<String> testList) {
        this.mContext = mContext;
        this.mTestList = testList;
    }

    @Override
    public TestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TestViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_test, null, false));
    }

    @Override
    public void onBindViewHolder(final TestViewHolder holder, final int position) {
        holder.tvTest.setText(mTestList.get(position));
    }

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

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(mTestList, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(mTestList, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    static class TestViewHolder extends RecyclerView.ViewHolder implements ItemHolderMoveCallback{
        private TextView tvTest;

        public TestViewHolder(View itemView) {
            super(itemView);
            tvTest = (TextView) itemView.findViewById(R.id.tv_test);
        }

        @Override
        public void onItemHolderMoveStart() {
            tvTest.setTextSize(40);
            itemView.setBackgroundColor(Color.RED);
        }

        @Override
        public void onItemHolderMoveEnd() {
            tvTest.setTextSize(32);
            itemView.setBackgroundResource(R.color.colorAccent);
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

TestAdapter与RecyclerView绑定:

        mRvTest.setLayoutManager(gridLayoutManager);
        mTestList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            mTestList.add("测试文本" + i);
        }
        mTestAdapter = new TestAdapter(this, mTestList);
        mRvTest.setAdapter(mTestAdapter);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

布局文件中的代码:

  <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
  • 1
  • 2
  • 3
  • 4

recyclerview依赖:

implementation'androidx.recyclerview:recyclerview-selection:1.0.0'
  • 1

最终效果如下:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/286802
推荐阅读
相关标签
  

闽ICP备14008679号