如何实现多选的按住和拖动功能?
How to implement hold and drag feature for multi selection?
三星手机有这个功能,你可以用手指按住一个项目然后向下拖动,我可以像 gif
一样快速地多个 select 项目
有什么方法可以在我的应用程序中实现此功能,即使它仅适用于三星设备?
Example image
even if it's just for samsung devices
您可以在任何设备上实现此功能。我第一次在小米设备上发现这个功能。
有点复杂。但是您可以为此目的使用库,例如 DragSelectRecyclerView.
将以下代码添加到您的 build.gradle:
dependencies {
implementation 'com.github.MFlisar:DragSelectRecyclerView:0.3'
}
在您的 java 文件中,添加以下代码:
创建触摸侦听器。
mDragSelectTouchListener = new DragSelectTouchListener()
.withSelectListener(onDragSelectionListener)
// following is all optional
.withMaxScrollDistance(distance) // default: 16; defines the speed of the auto scrolling
.withTopOffset(toolbarHeight) // default: 0; set an offset for the touch region on top of the RecyclerView
.withBottomOffset(toolbarHeight) // default: 0; set an offset for the touch region on bottom of the RecyclerView
.withScrollAboveTopRegion(enabled) // default: true; enable auto scrolling, even if the finger is moved above the top region
.withScrollBelowTopRegion(enabled) // default: true; enable auto scrolling, even if the finger is moved below the top region
.withDebug(enabled);
将此触摸侦听器附加到 RecyclerView
。
recyclerView.addOnItemTouchListener(mDragSelectTouchListener);
item长按,通知监听器开始拖动选择
mDragSelectTouchListener.startDragSelection(position);
使用DragSelectionProcessor
,它实现了上述接口,可以设置4种模式:
Simple
:只需选择您经过的每个项目并在返回时取消选择
ToggleAndUndo
:切换每个项目的原始状态,返回时恢复到原始状态
FirstItemDependent
:切换第一个项目并对您经过的每个项目应用相同的状态,并在向后移动时应用反转状态
FirstItemDependentToggleAndUndo
:切换项目并对您经过的每个项目应用相同的状态,并在返回时恢复到原始状态
在它的构造函数中提供一个ISelectionHandler
并实现这些功能:
onDragSelectionListener = new DragSelectionProcessor(new DragSelectionProcessor.ISelectionHandler() {
@Override
public Set<Integer> getSelection() {
// return a set of all currently selected indizes
return selection;
}
@Override
public boolean isSelected(int index) {
// return the current selection state of the index
return selected;
}
@Override
public void updateSelection(int start, int end, boolean isSelected, boolean calledFromOnStart) {
// update your selection
// range is inclusive start/end positions
// and the processor has already converted all events according to it'smode
}
})
// pass in one of the 4 modes, simple mode is selected by default otherwise
.withMode(DragSelectionProcessor.Mode.FirstItemDependentToggleAndUndo);
你可以看到一个演示 activity here.
是否有任何其他图书馆用于相同目的?
是的,你也可以使用Drag Select Recycler View by afollestad. And you can see practical implementation for afollestad's Drag select Recycler View here。
三星手机有这个功能,你可以用手指按住一个项目然后向下拖动,我可以像 gif
一样快速地多个 select 项目有什么方法可以在我的应用程序中实现此功能,即使它仅适用于三星设备?
Example image
even if it's just for samsung devices
您可以在任何设备上实现此功能。我第一次在小米设备上发现这个功能。
有点复杂。但是您可以为此目的使用库,例如 DragSelectRecyclerView.
将以下代码添加到您的 build.gradle:
dependencies {
implementation 'com.github.MFlisar:DragSelectRecyclerView:0.3'
}
在您的 java 文件中,添加以下代码:
创建触摸侦听器。
mDragSelectTouchListener = new DragSelectTouchListener()
.withSelectListener(onDragSelectionListener)
// following is all optional
.withMaxScrollDistance(distance) // default: 16; defines the speed of the auto scrolling
.withTopOffset(toolbarHeight) // default: 0; set an offset for the touch region on top of the RecyclerView
.withBottomOffset(toolbarHeight) // default: 0; set an offset for the touch region on bottom of the RecyclerView
.withScrollAboveTopRegion(enabled) // default: true; enable auto scrolling, even if the finger is moved above the top region
.withScrollBelowTopRegion(enabled) // default: true; enable auto scrolling, even if the finger is moved below the top region
.withDebug(enabled);
将此触摸侦听器附加到 RecyclerView
。
recyclerView.addOnItemTouchListener(mDragSelectTouchListener);
item长按,通知监听器开始拖动选择
mDragSelectTouchListener.startDragSelection(position);
使用DragSelectionProcessor
,它实现了上述接口,可以设置4种模式:
Simple
:只需选择您经过的每个项目并在返回时取消选择ToggleAndUndo
:切换每个项目的原始状态,返回时恢复到原始状态FirstItemDependent
:切换第一个项目并对您经过的每个项目应用相同的状态,并在向后移动时应用反转状态FirstItemDependentToggleAndUndo
:切换项目并对您经过的每个项目应用相同的状态,并在返回时恢复到原始状态
在它的构造函数中提供一个ISelectionHandler
并实现这些功能:
onDragSelectionListener = new DragSelectionProcessor(new DragSelectionProcessor.ISelectionHandler() {
@Override
public Set<Integer> getSelection() {
// return a set of all currently selected indizes
return selection;
}
@Override
public boolean isSelected(int index) {
// return the current selection state of the index
return selected;
}
@Override
public void updateSelection(int start, int end, boolean isSelected, boolean calledFromOnStart) {
// update your selection
// range is inclusive start/end positions
// and the processor has already converted all events according to it'smode
}
})
// pass in one of the 4 modes, simple mode is selected by default otherwise
.withMode(DragSelectionProcessor.Mode.FirstItemDependentToggleAndUndo);
你可以看到一个演示 activity here.
是否有任何其他图书馆用于相同目的?
是的,你也可以使用Drag Select Recycler View by afollestad. And you can see practical implementation for afollestad's Drag select Recycler View here。