android 拖拽交换位置
时间: 2023-10-18 20:08:43 浏览: 214
Android 拖拽控件交换位置
Android 拖拽交换位置可以通过自定义 RecyclerView.ItemTouchHelper.Callback 来实现。具体步骤如下:
1. 自定义 RecyclerView.ItemTouchHelper.Callback 类,并重写以下方法:
- onMove():返回 true 表示可以移动 item,保存被移动的 item 和目标位置的 item,然后交换它们的位置。
- onSwiped():返回 true 表示可以滑动 item,不做任何操作即可。
- getMovementFlags():返回可以支持的移动和滑动方向。
2. 在 RecyclerView 中调用 ItemTouchHelper.attachToRecyclerView() 方法,将自定义的 ItemTouchHelper.Callback 作为参数传入。
3. 在 RecyclerView.Adapter 中实现自定义的 ItemTouchHelper.Adapter 接口,并重写以下方法:
- onItemMove():交换被移动的 item 和目标位置的 item。
- onItemDismiss():不做任何操作即可。
示例代码如下:
```
public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
private final ItemTouchHelperAdapter mAdapter;
public ItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
mAdapter = adapter;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return false;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// do nothing
}
public interface ItemTouchHelperAdapter {
boolean onItemMove(int fromPosition, int toPosition);
void onItemDismiss(int position);
}
}
```
```
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
implements ItemTouchHelperCallback.ItemTouchHelperAdapter {
private List<String> mData;
public MyAdapter(List<String> data) {
mData = data;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.mTextView.setText(mData.get(position));
}
@Override
public int getItemCount() {
return mData.size();
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
Collections.swap(mData, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public void onItemDismiss(int position) {
// do nothing
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View view) {
super(view);
mTextView = view.findViewById(R.id.text_view);
}
}
}
```
```
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
List<String> data = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
data.add("Item " + i);
}
MyAdapter adapter = new MyAdapter(data);
ItemTouchHelperCallback callback = new ItemTouchHelperCallback(adapter);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
itemTouchHelper.attachToRecyclerView(mRecyclerView);
mRecyclerView.setAdapter(adapter);
}
}
```
阅读全文