Comment 3 for bug 1534112

Revision history for this message
Olivier Tilloy (osomon) wrote :

So it appears it’s not a bug in the UITK, but in the way it is being used in the browser and in the standalone example (see comment #1). Changing ViewItems.selectedIndices in onSelectedChanged is not a good idea.

I came up with a different approach which solves that issue:

import QtQuick 2.4
import Ubuntu.Components 1.3

ListView {
  id: listview
  width: 300
  height: 120
  model: 2
  delegate: ListItem {
    ListItemLayout {
      title.text: "selected: " + selected
    }
  }
  property int selectedIndex: -1
  ViewItems.selectMode: true
  ViewItems.onSelectedIndicesChanged: {
    if (ViewItems.selectedIndices.length > 1 && selectedIndex != -1) {
      var selection = ViewItems.selectedIndices
      selection.splice(selection.indexOf(selectedIndex), 1)
      selectedIndex = selection[0]
      ViewItems.selectedIndices = selection
      return
    }
    if (ViewItems.selectedIndices.length > 0) {
      selectedIndex = ViewItems.selectedIndices[0]
    } else {
      selectedIndex = -1
    }
  }
}

However I’m now seeing another issue: after selecting the first item, then the second one, then the first one again, the 'selected' property of the item doesn’t reflect the current state: the first item is selected, selectedIndices is [0], yet the first delegate’s selected property is false.