我有一个Recyclerview,允许在编辑模式下拖放项目。一旦用户退出编辑模式,我需要禁用拖放功能。
我已尝试更改ItemTouchCallback中的内容,包括移动标志和isLongPressDragEnabled无效。长按继续开始拖动事件。
我目前的解决方案是分离并重新连接片段,然后再次进入编辑模式时添加回调,但我希望有一些更清洁的东西,我不知道。
回调:
library(data.table)
# Crate a df for this demo.
mydf <- iris
# Choose columns that you want to apply lag() and create new colnames.
cols = names(iris)[sapply(iris, is.numeric)]
anscols = paste("lag_", cols, sep = "")
# Apply shift() to each of the chosen columns.
setDT(mydf)[, (anscols) := shift(.SD, 1, type = "lag"),
.SDcols = cols]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species lag_Sepal.Length lag_Sepal.Width
1: 5.1 3.5 1.4 0.2 setosa NA NA
2: 4.9 3.0 1.4 0.2 setosa 5.1 3.5
3: 4.7 3.2 1.3 0.2 setosa 4.9 3.0
4: 4.6 3.1 1.5 0.2 setosa 4.7 3.2
5: 5.0 3.6 1.4 0.2 setosa 4.6 3.1
---
146: 6.7 3.0 5.2 2.3 virginica 6.7 3.3
147: 6.3 2.5 5.0 1.9 virginica 6.7 3.0
148: 6.5 3.0 5.2 2.0 virginica 6.3 2.5
149: 6.2 3.4 5.4 2.3 virginica 6.5 3.0
150: 5.9 3.0 5.1 1.8 virginica 6.2 3.4
lag_Petal.Length lag_Petal.Width
1: NA NA
2: 1.4 0.2
3: 1.4 0.2
4: 1.3 0.2
5: 1.5 0.2
---
146: 5.7 2.5
147: 5.2 2.3
148: 5.0 1.9
149: 5.2 2.0
150: 5.4 2.3
适配器:
public class ItemTouchCallback extends ItemTouchHelper.Callback {
private final ItemTouchHelperAdapter mAdapter;
public ItemTouchCallback(ItemTouchHelperAdapter mAdapter) {
this.mAdapter = mAdapter;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.START | ItemTouchHelper.END |ItemTouchHelper.DOWN | ItemTouchHelper.UP;
return makeMovementFlags(dragFlags, 0);
}
@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) {
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return false;
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
if (viewHolder instanceof ItemTouchHelperViewHolder) {
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemSelected();
}
}
super.onSelectedChanged(viewHolder, actionState);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof ItemTouchHelperViewHolder) {
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemClear();
}
}
}
片段:
public interface ItemTouchHelperAdapter {
void onItemMove(int fromPosition, int toPosition);
void onItemDismiss(int position);
}
感谢。
答案 0 :(得分:11)
在你的ItemTouchCallback
中boolean mDraggable = false;
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = mDraggable ? UP | DOWN | START | END : 0;
return makeMovementFlags(dragFlags, 0);
}
public void setDraggable(boolean value) {
mDraggable = value;
}
答案 1 :(得分:3)
这就是我这样做的方式:
if(mode == MODE_CREATE){
touchHelper.attachToRecyclerView(recyclerView);
}else if(mode == MODE_EDIT){
touchHelper.attachToRecyclerView(null);
}