如何检测FMX列表视图何时滚动到底部? [英] How to detect when FMX List View is scrolled to the bottom?

查看:109
本文介绍了如何检测FMX列表视图何时滚动到底部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Firemonkey TListView 以显示搜索结果。此列表一次加载25个项目,但可能显示数百个项目。



我需要检测用户何时向下滚动到底部,以便获取接下来要显示在列表中的25个项目。但是,我找不到合适的属性来确定这一点。



OnPullRefresh 事件,但这适用于滚动到列表的顶部并将其拉下。我需要的是类似的内容,但是列表的底部而不是 top



似乎有一个 OnScrollViewChange 事件。还有 ScrollViewPos 属性,该属性也很有用。但是,我无法弄清楚将该数字与什么进行比较-具体地说,就是 ScrollViewPos 的最大可能值。我需要像 ScrollViewMax 这样的东西。



但是我找不到其他东西可以准确地检测到用户已经滚动



如何检测用户何时滚动到Firemonkey TListView 的底部,以便我可以加载更多搜索结果吗?



编辑



如果由于某种原因不可能这样做,则有一个另一种选择是使用加载更多...按钮将虚拟项添加到列表的末尾。但我宁愿它是自动化的。



EDIT2



我忘了...我有搜索框在此列表视图中显示,并且项目的高度可以变化。如果仅根据项目内容进行计算,则此计算必须是 perfect 。我不希望知道用户何时接近或接近底部,而是何时用户恰好到达列表的底部。

解决方案

进一步调查我在

中发现了以下内容函数TListViewBase.GetItemRect(const AItemIndex:Integer):TRectF; 

如果您深入研究

 函数TListViewBase.GetItemRelRect(const Index:Integer; const LocRect:TRectF; 
const SideSpace:Integer = 0):TRectF;

然后,您将意识到最后一项的顶部是

  listview1.GetItemRect(listview1.ItemCount-1).top + listview1.ScrollViewPos-listview1.SideSpace-listview1.LocalRect.top 

无论您的商品的可变高度是多少。这表示 FHeightSums [Index] 的值,这是一个列表,包含直到索引为 Index 的项的高度之和。



出现问题了: 您希望计算是完美的。



这就是它

 过程TForm5.ListView1ScrollViewChange(Sender:TObject); 
var
Tmp_top:单;
开始
Tmp_top:= listview1.GetItemRect(listview1.ItemCount-1).top + listview1.ScrollViewPos-listview1.SideSpace-listview1.LocalRect.top;

如果Tmp_top + listview1.GetItemRect(listview1.ItemCount-1).height-listview1.Height = listview1.ScrollViewPos-2 * listview1.SideSpace然后
showmessage('touch down');

结尾;

编辑::如果您进一步简化此公式,则最终会很少升级的其他答案

 过程TForm5.ListView1ScrollViewChange(Sender:TObject); 
开始

,如果listview1.GetItemRect(listview1.ItemCount-1).bottom = listview1.Height-listview1.SideSpace然后
showmessage('touch down');

结尾;

现在这将涵盖填充,边距,边距,搜索框可见性和搜索框大小更改的任何更改。 / p>

I'm working on a Firemonkey TListView to display search results. This list loads 25 items at a time, but could potentially show hundreds of items.

I need to detect when the user has scrolled down to the bottom, so that I can fetch the next 25 items to show in the list. However, I cannot find the appropriate properties to determine this.

There's the OnPullRefresh event, but that applies to scrolling to the top of the list and pulling it down. What I need is similar, but for the bottom of the list instead of the top.

There's the OnScrollViewChange event, which seems appropriate. There's also the ScrollViewPos property, which is also useful. However, I cannot figure out what to compare that number to - specifically, the maximum possible value for ScrollViewPos. Something like ScrollViewMax is what I would need.

But I cannot find anything more to accurately detect that user has scrolled to the bottom.

How can I detect when a user has scrolled to the bottom of a Firemonkey TListView so that I can load more search results?

EDIT

If this is not possible for whatever reason, there is an alternative to add a dummy item to the end of the list with a "Load More..." button. But I'd rather it be automated.

EDIT2

I forgot to mention... I have the Search Box showing in this list view, and items can have a variable height. If calculating this based on item contents is the only way, then such a calculation needs to be perfect. I don't want to know when user is "approaching" or "near" the bottom, but when user exactly hits the bottom of the list.

解决方案

Further investigation I found the following in

function TListViewBase.GetItemRect(const AItemIndex: Integer): TRectF;

If you go deeper in

function TListViewBase.GetItemRelRect(const Index: Integer; const LocRect: TRectF;
  const SideSpace: Integer = 0): TRectF;

then you will realize that the last item's Top is

listview1.GetItemRect(listview1.ItemCount-1).top+listview1.ScrollViewPos-listview1.SideSpace-listview1.LocalRect.top

whatever was your items variable height. This represents the value of FHeightSums[Index] which is a list containing the sum of height till the item with Index Index

Now to the problem: You want the calculation to be perfect. you want a touch down.

And this is it

procedure TForm5.ListView1ScrollViewChange(Sender: TObject);
var
Tmp_top : single;
begin
Tmp_top := listview1.GetItemRect(listview1.ItemCount-1).top+listview1.ScrollViewPos-listview1.SideSpace-listview1.LocalRect.top;

if Tmp_top+listview1.GetItemRect(listview1.ItemCount-1).height-listview1.Height=listview1.ScrollViewPos-2*listview1.SideSpace then
    showmessage('touch down');

end;

Edit: if you further simplify this formula you will end up in the other answer with few upgrades

 procedure TForm5.ListView1ScrollViewChange(Sender: TObject);
begin

if listview1.GetItemRect(listview1.ItemCount-1).bottom=listview1.Height-listview1.SideSpace then
    showmessage('touch down');

end;

now this will cover any changes in padding, margins, sidespace, searchbox visibility and searchbox size changing.

这篇关于如何检测FMX列表视图何时滚动到底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆