如何使易碎的东西确保其中一件项目的可见性



i具有一个Flickable,其中包含大量的TextField对象,其中每个文本字段都固定在上一个Textfield的底部。一切正常工作,除了我使用选项卡键在这些字段中导航时,最终将焦点放在 Flickable的可见矩形外部的文本字段,然后用户在滚动易变之前看不到光标手动。

本质上,我正在寻找某种"。Div>

您是否考虑过对此进行更模型的方法?我的意思是,如果您使用诸如ListView之类的东西,则可以简单地更改currentItem,此时,如果视图不超出可见范围,则视图将自动滚动。

此外,它将仅加载在可见范围内的文本元素,从而节省某些内存。

但是,即使您当前的方法也不会那么复杂,无法确保可见性。

  Flickable {
    id: flick
    anchors.fill: parent
    contentHeight: col.height
    function ensureVisible(item) {
      var ypos = item.mapToItem(contentItem, 0, 0).y
      var ext = item.height + ypos
      if ( ypos < contentY // begins before
          || ypos > contentY + height // begins after
          || ext < contentY // ends before
          || ext > contentY + height) { // ends after
        // don't exceed bounds
        contentY = Math.max(0, Math.min(ypos - height + item.height, contentHeight - height))
      }
    }
    Column {
      id: col
      Repeater {
        id: rep
        model: 20
        delegate: Text {
          id: del
          text: "this is item " + index
          Keys.onPressed: rep.itemAt((index + 1) % rep.count).focus = true
          focus: index === 0
          color: focus ? "red" : "black"
          font.pointSize: 40
          onFocusChanged: if (focus) flick.ensureVisible(del)
        }
      }
    }
  }

解决方案既快速又粗糙,但是将其置于生产形状上是微不足道的。重要的是要映射到contentItem,而不是可忽视的,因为后者会考虑到当前滚动的数量,给出了错误的结果。使用映射将使解决方案对您可能使用的任何定位方案,也将支持任意级别的嵌套对象。

dtech的答案已亮起。它很容易与精美的快照动画结合在一起,并且易于修改x方向易闪烁。此外,用户可能会故意轻弹或拖动可闪烁的。就我而言,C 代码正在控制可忽视的网格布局中这些项目的文本或显示效果。当C 代码向其发出信号时,需要闪烁的闪烁,但如果用户故意拖动或弹奏,则不需要闪烁。这是DTECH的函数,可修改为x方向可忽略的:

function ensureVisible(item) {
    if (moving || dragging)
        return;
    var xpos = item.mapToItem(contentItem, 0, 0).x
    var ext = item.width + xpos
    if ( xpos < contentX // begins before
              || xpos > contentX + width // begins after
              || ext < contentX // ends before
              || ext > contentX + width) { // ends after
        // don't exceed bounds
        var destinationX = Math.max(0, Math.min(xpos - width + item.width, contentWidth - width))
        ensureVisAnimation.to = destinationX;
        ensureVisAnimation.from = contentX;
        ensureVisAnimation.start();
    }
}
//This animation is for the ensure-visible feature.
NumberAnimation on contentX {
    id: ensureVisAnimation
    to: 0               //Dummy value - will be set up when this animation is called.
    duration: 300
    easing.type: Easing.OutQuad;
}

最新更新