ItemTouchHelper 是 Android 中用于实现拖放和滑动删除功能的类。要自定义 ItemTouchHelper 的动画,你需要重写其 onItemMove()
和 onItemDismiss()
方法。以下是一个简单的示例,展示了如何自定义 ItemTouchHelper 的动画:
- 首先,创建一个自定义的
SimpleItemTouchHelperCallback
类,继承自ItemTouchHelper.SimpleCallback
:
import android.graphics.Canvas; import android.view.View; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.card.MaterialCardView; import com.google.android.material.snackbar.Snackbar; import java.util.List; public class CustomItemTouchHelperCallback extends ItemTouchHelper.SimpleCallback { private final OnItemMoveListener mListener; public CustomItemTouchHelperCallback(int dragDirs, int swipeDirs, OnItemMoveListener listener) { super(dragDirs, swipeDirs); mListener = listener; } @Override public boolean onItemMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { if (viewHolder != target) { mListener.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition()); } return true; } @Override public void onItemDismiss(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { mListener.onItemDismiss(viewHolder.getAdapterPosition()); } public interface OnItemMoveListener { void onItemMove(int fromPosition, int toPosition); void onItemDismiss(int position); } }
- 在你的
Activity
或Fragment
中,实现CustomItemTouchHelperCallback.OnItemMoveListener
接口,并重写onItemMove()
和onItemDismiss()
方法:
public class MainActivity extends AppCompatActivity implements CustomItemTouchHelperCallback.OnItemMoveListener { private RecyclerView mRecyclerView; private MyAdapter mAdapter; private ItemTouchHelper mItemTouchHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = findViewById(R.id.recycler_view); mAdapter = new MyAdapter(); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mItemTouchHelper = new ItemTouchHelper(new CustomItemTouchHelperCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0, this)); mItemTouchHelper.attachToRecyclerView(mRecyclerView); } @Override public void onItemMove(int fromPosition, int toPosition) { // 在这里处理 item 移动的逻辑,例如更新数据集并刷新 RecyclerView mAdapter.moveItem(fromPosition, toPosition); } @Override public void onItemDismiss(int position) { // 在这里处理 item 删除的逻辑,例如更新数据集并刷新 RecyclerView mAdapter.removeItem(position); } }
- 在你的
RecyclerView.Adapter
中,实现moveItem()
和removeItem()
方法,用于更新数据集:
public class MyAdapter extends RecyclerView.Adapter{ private List mData; public MyAdapter() { mData = https://www.yisu.com/ask/new ArrayList<>(); // 初始化数据集 } public void moveItem(int fromPosition, int toPosition) { if (fromPosition < toPosition) { for (int i = fromPosition; i < toPosition; i++) { String temp = mData.get(i); mData.set(i, mData.get(i + 1)); mData.set(i + 1, temp); } } else { for (int i = fromPosition; i > toPosition; i--) { String temp = mData.get(i); mData.set(i, mData.get(i - 1)); mData.set(i - 1, temp); } } notifyItemMoved(fromPosition, toPosition); } public void removeItem(int position) { mData.remove(position); notifyItemRemoved(position); } // 其他适配器方法... }
现在,你已经成功自定义了 ItemTouchHelper 的动画。你可以根据需要进一步自定义动画效果,例如改变拖动的轨迹颜色、调整滑动速度等。