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<MyAdapter.ViewHolder> {
private List<String> mData;
public MyAdapter() {
mData = 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 的動畫。你可以根據需要進一步自定義動畫效果,例如改變拖動的軌跡顏色、調整滑動速度等。