【问题标题】:ListView drag&drop reorder items - best practiceListView 拖放重新排序项目 - 最佳实践
【发布时间】:2014-02-07 07:34:29
【问题描述】:

我不想在拖放后重新排序 ListView 中的项目。它并不像看起来那么容易实现。是否有任何有效的最佳实践来重新排序项目、辅助类或库...或者 javafx 是否有辅助方法?

【问题讨论】:

标签: listview javafx


【解决方案1】:

这是我刚刚为该问题编写的解决方案。我确信有一个更快/更有效的解决方案,但这对我有用。参数含义如下:

  • dropIndex:项目被删除/移动到的索引。
  • listView:当前ListView

代码如下:

// 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多