Listview 列表视图拖动&;放弃重新订购项目-最佳实践
拖放后,我不想在Listview 列表视图拖动&;放弃重新订购项目-最佳实践,listview,javafx,Listview,Javafx,拖放后,我不想在列表视图中对项目重新排序。它不像看上去那么容易实现。是否有任何有效的最佳实践来重新排序项目、帮助器类或库。。。或者javafx有一个helper方法 这是我为这个问题写的解决方案。我确信有一个更快/更高效的解决方案,但这对我来说是可行的。这些参数具有以下含义: dropIndex:项目被拖放/移动到的索引 listView:当前的listView 代码如下: //暂时保存当前选择 int[]index=new int[listView.getSelectionModel().
列表视图中对项目重新排序。它不像看上去那么容易实现。是否有任何有效的最佳实践来重新排序项目、帮助器类或库。。。或者javafx有一个helper方法
这是我为这个问题写的解决方案。我确信有一个更快/更高效的解决方案,但这对我来说是可行的。这些参数具有以下含义:
dropIndex
:项目被拖放/移动到的索引
listView
:当前的listView
代码如下:
//暂时保存当前选择
int[]index=new int[listView.getSelectionModel().getSelectedIndices().size()];
int j=0;
对于(整数i:listView.getSelectionModel().getSelectedIndices()){
指数[j]=i;
++j;
}
//添加项目
listView.getItems().addAll(dropIndex,listView.getSelectionModel().getSelectedItems());
//清晰选择
listView.getSelectionModel().clearSelection();
//更改索引以删除项目
int amountSmallerDropIndex=0;
对于(int i=0;i=dropIndex){
指数[i]=指数[i]+指数长度;
}否则{
amountSmallerDropIndex++;
}
}
//删除项目
对于(int i=index.length-1;i>=0;i--){
移除(索引[i]);
}
//选择移动的项目
listView.getSelectionModel().selectRange(dropIndex-amountSmallerDropIndex,dropIndex-amountSmallerDropIndex+index.length);
// save current selection temporarily
int[] indices = new int[listView.getSelectionModel().getSelectedIndices().size()];
int j = 0;
for (Integer i : listView.getSelectionModel().getSelectedIndices()) {
indices[j] = i;
++j;
}
// add items
listView.getItems().addAll(dropIndex, listView.getSelectionModel().getSelectedItems());
// clear selection
listView.getSelectionModel().clearSelection();
// change indices to remove items
int amountSmallerDropIndex = 0;
for (int i = 0; i < indices.length; i++) {
if (indices[i] >= dropIndex) {
indices[i] = indices[i] + indices.length;
} else {
amountSmallerDropIndex++;
}
}
// remove items
for (int i = indices.length - 1; i >= 0; i--) {
listView.getItems().remove(indices[i]);
}
// select moved items
listView.getSelectionModel().selectRange(dropIndex - amountSmallerDropIndex, dropIndex - amountSmallerDropIndex + indices.length);