Android使用ItemTouchHelper实现RecyclerView的item拖动位置交换
Posted Mars-xq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android使用ItemTouchHelper实现RecyclerView的item拖动位置交换相关的知识,希望对你有一定的参考价值。
demo:
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainActivity5 extends AppCompatActivity
private final List<String> mData = new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
for (int i = 0; i < 200; i++)
mData.add("source" + i);
RvAdapter rvAdapter = new RvAdapter(mData);
recyclerView.setAdapter(rvAdapter);
recyclerView.setLayoutManager(new GridLayoutManager(this, 8));
MyItemTouchHelper myItemTouchHelper = new MyItemTouchHelper(mData);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(myItemTouchHelper);
itemTouchHelper.attachToRecyclerView(recyclerView);
/**
* 处理RecycleView的选中,拖拽移动,拖拽删除的实现类
*/
public static class MyItemTouchHelper extends ItemTouchHelper.Callback
private final List<String> mData;
public MyItemTouchHelper(List<String> mData)
this.mData = mData;
@Override
public int getMovementFlags(RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder)
int dragFlags;
if (recyclerView.getLayoutManager() instanceof GridLayoutManager)
dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
else
dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
@Override
public boolean isLongPressDragEnabled()
return true; //长按启用拖拽
@Override
public boolean isItemViewSwipeEnabled()
return false; //不启用拖拽删除
@Override
public boolean onMove(@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder source,
@NonNull RecyclerView.ViewHolder target)
//通过接口传递拖拽交换数据的起始位置和目标位置的ViewHolder
int fromPosition = source.getAdapterPosition();
int toPosition = target.getAdapterPosition();
if (fromPosition < mData.size() && toPosition < mData.size())
//交换数据位置
Collections.swap(mData, fromPosition, toPosition);
//刷新位置交换
RvAdapter adapter = (RvAdapter) recyclerView.getAdapter();
if (adapter != null)
adapter.notifyItemMoved(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)
//当滑动或者拖拽view的时候通过接口返回该ViewHolder
//当拖拽选中时放大选中的view
viewHolder.itemView.setScaleX(1.2f);
viewHolder.itemView.setScaleY(1.2f);
viewHolder.itemView.setBackgroundColor(Color.RED);
@Override
public void clearView(@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder)
super.clearView(recyclerView, viewHolder);
if (!recyclerView.isComputingLayout())
//当需要清除之前在onSelectedChanged或者
// onChildDraw,onChildDrawOver设置的状态或者动画时通过接口返回该ViewHolder
viewHolder.itemView.setScaleX(1.0f);
viewHolder.itemView.setScaleY(1.0f);
viewHolder.itemView.setBackgroundColor(Color.BLUE);
public static class RvAdapter extends RecyclerView.Adapter<RvAdapter.VHolder>
private final List<String> mData;
public RvAdapter(List<String> mData)
this.mData = mData;
@NonNull
@Override
public VHolder onCreateViewHolder(ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_rv, parent, false);
return new VHolder(view);
@Override
public void onBindViewHolder(RvAdapter.VHolder holder, int position)
holder.mTextView1.setText(mData.get(position));
holder.mTextView2.setText(String.valueOf(position));
@Override
public int getItemCount()
return mData.size();
public static class VHolder extends RecyclerView.ViewHolder
private final TextView mTextView1;
private final TextView mTextView2;
public VHolder(View itemView)
super(itemView);
mTextView1 = (TextView) itemView.findViewById(R.id.tv1);
mTextView2 = (TextView) itemView.findViewById(R.id.tv2);
activity_main5.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:itemCount="50"
tools:listitem="@layout/item_rv" />
</LinearLayout>
item_rv.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp"
android:background="@android:color/holo_blue_dark"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@android:color/white"
android:textSize="18sp"
tools:text="@tools:sample/cities" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@android:color/white"
android:textSize="14sp"
tools:text="@tools:sample/cities" />
</LinearLayout>
以上是关于Android使用ItemTouchHelper实现RecyclerView的item拖动位置交换的主要内容,如果未能解决你的问题,请参考以下文章
android开发游记:ItemTouchHelper 使用RecyclerView打造可拖拽的GridView
两个recyclerviews的Android ItemTouchHelper
Android开发 在不使用ItemTouchHelper的情况下实现ItemView的左右滑动
NoClassDefFoundError: android.support.v7.widget.helper.ItemTouchHelper$3