Bắt đầu bằng cách thêm một draglistener để chế độ xem thổi phồng của bạn ở onCreateViewHolder
của bộ điều hợp máy tái chế.
view.setOnDragListener(new OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
switch (dragEvent.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// drag has started, return true to tell that you're listening to the drag
return true;
case DragEvent.ACTION_DROP:
// the dragged item was dropped into this view
Category a = items.get(getAdapterPosition());
a.setText("dropped");
notifyItemChanged(getAdapterPosition());
return true;
case DragEvent.ACTION_DRAG_ENDED:
// the drag has ended
return false;
}
return false;
}
});
Trong trường hợp ACTION_DROP
bạn có thể thay đổi mô hình và gọi notifyItemChanged()
, hoặc sửa đổi trực tiếp xem (mà sẽ không xử lý các trường hợp rebinding). Cũng trong onCreateViewHolder
thêm một longClickListener
-View
của bạn, và trong onLongClick
bắt đầu kéo:
ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(view.getTag().toString(),
mimeTypes, item);
view.setVisibility(View.GONE);
HeptagonDragShadowBuilder myShadow = new HeptagonDragShadowBuilder(Heptagon.this, 1.1f);
if (VERSION.SDK_INT >= VERSION_CODES.N) {
view.startDragAndDrop(dragData, myShadow, null, 0);
} else {
view.startDrag(dragData, myShadow, null, 0);
}
Để biết thêm thông tin về kéo và thả kiểm tra android developers site
Nguồn
2017-02-06 11:56:42
Cảm ơn rất nhiều, có vẻ Greate, nhưng tôi có câu hỏi đơn giản làm thế nào để có được vị trí xem bộ chuyển đổi? Cảm ơn –
với 'viewholder.getAdapterPosition()' –
bạn được chào đón ^^ –