ListView只有在手动滚动到底部一次后才会自动滚动。

huangapple go评论74阅读模式
英文:

ListView auto-scroll only working when manually scrolled to the bottom once

问题

我对JavaFX ListView组件进行了扩展:

HL7ListView extends ListView<String>

目标是在添加新项时,使其自动滚动到父级ScrollPane。我有一个构造函数,它接受要操作的ScrollPane作为参数,并创建了一个addItem()方法如下:

public void addItem(String item)
{
    List<String> items = getItems();
    items.add(item);
    
    scrollTo(items.size());
    
    scrollPane.setVvalue(1.0);
}

自动滚动仅在我手动滚动到底部一次以"启动它"时才有效,如果这有意义的话。一旦滚动到底部,新项目就会如预期般工作,滚动条会自动下滑,跟随ListView的内容。不确定问题可能出在哪里,显然,滚动条只在需要时才会出现,不确定这是否与此有关。

有什么想法吗?

英文:

I made an extension of the JavaFX ListView component :

HL7ListView extends ListView&lt;String&gt;

The goal is for it to auto-scroll the parent ScrollPane as new items are added to it. I have a constructor that takes the ScrollPane in question as an argument and I made an addItem() method like this :

public void addItem(String item)
{
	List&lt;String&gt; items = getItems();
    items.add(item);
    
    scrollTo(items.size());
    
    scrollPane.setVvalue(1.0);
}

The auto scrolling only works when i manually scroll to the bottom once to "get it going" if that makes any sense. Once it is scrolled to the bottom, new items behave as expected and the scrollbar goes down automatically, trailing the ListView's content. Not sure what could be the problem here, obviously the scroll bar only appears when there is a need for it, not sure if that could have anything to do with this.

Any idea?

答案1

得分: 1

我认为您不需要用父级ScrollPane包装您的ListView,因为ListView已经内置了滚动条。您可以使用scrollTo()直接滚动到最后一个索引。

ListView<String> listView = new ListView<>();
listView.getItems().addAll(items);

listView.scrollTo(listView.getItems().size() - 1);

编辑

在您的扩展(HL7ListView)中,使用super.scrollTo而不是this.scrollTo似乎也可以工作,

class HL7ListView extends ListView<String> {

    public HL7ListView() {
    }

    public HL7ListView(List<String> items) {
        this.getItems().addAll(items);
        super.scrollTo(getItems().size() - 1);
    }

    public void addItem(String item) {
        this.getItems().add(item);
        super.scrollTo(getItems().size() - 1);
    }
}

附注:我尝试按照您的方法包装在ScrollPane中,查看了这个 QA - 似乎不起作用,看起来有点问题。

英文:

I think you DON'T need to wrap your ListView with parent ScrollPane as ListView has the built-in scroller. You can use scrollTo() to scroll with the last index directly.

ListView&lt;String&gt; listView = new ListView&lt;&gt;();
listView.getItems().addAll(items);

listView.scrollTo(listView.getItems().size() - 1);

Edit

Using super.scrollTo instead of this.scrollTo seems to work on your extenstion (HL7ListView) as well,

class HL7ListView extends ListView&lt;String&gt; {

    public HL7ListView() {
    }

    public HL7ListView(List&lt;String&gt; items) {
        this.getItems().addAll(items);
        super.scrollTo(getItems().size() - 1);
    }

    public void addItem(String item) {
        this.getItems().add(item);
        super.scrollTo(getItems().size() - 1);

    }
}

>PS: I tried to wrap with ScrollPane as your approach and went through this QA - and that's not working, it seems kinda bug.

huangapple
  • 本文由 发表于 2020年1月4日 01:47:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定