如何使列表视图可滚动,直到最后一个元素位于屏幕顶部

发布于 2024-12-23 11:29:15 字数 722 浏览 1 评论 0原文

我正在开发一个需要显示日历议程的应用程序,就像本机日历中的议程一样。我有一个显示不同事件的列表视图(注意:在问题的上下文中,事件是我的系统的实体)。我想在此屏幕上提供“今天”按钮。当用户单击此按钮时,事件应该滚动,直到当前日程表的第一个事件出现在屏幕顶部。当我从今天开始只安排了几个活动时,就会出现问题 - 活动太少以至于无法填满整个屏幕。然后列表视图会滚动,直到日历中的最后一个事件位于底部。这通常意味着没有达到将今天的第一个事件放在首位的预期效果。

有什么建议可以做到这一点吗?我曾想过在最后添加一些空白元素,但这似乎是丑陋的解决方法,而且它需要特殊的特定于设备的计算来告诉我要插入多少个元素。

编辑
根据评论中的要求添加一些代码
实际上,我不确定这段代码会让任何人感到惊讶,但是:

public void onTodayClicked(View target) {
  // calculate the indexOf. It works and is not related to the question
  if (indexOf >= 0) {
     ListView list = (ListView) findViewById(R.id.events_list_view);
     list.setSelection(indexOf);
  }
}

我不确定布局定义对于帮助回答问题很重要,但如果您认为如此,我也可以添加它。

I am developing an application that needs to show calendar agenda, much like the agenda in the native calendar. I have a list view showing different events (Note: in the context of the question events is entity of my system). I want to provide a 'Today' button on this screen. When the user clicks on this button the events are supposed to be scrolled until the first event of the current's day schedule is on top of the screen. The problem occurs when I have only a few events scheduled from today on - so few that they do not fill a whole screen. Then the list view just scrolls until the last event in the calendar is on the bottom. This usually means that the desired effect of having the first today's event on top is not achieved.

Any suggestions how this can be done? I have thought of adding some blank elements at the end, but this seems ugly workaround, and furthermore it will require special device-specific calculations that will tell me how many elements to insert.

Edit:
Adding some code as requested in comment
Actually I am not sure this code will surprise anyone, but:

public void onTodayClicked(View target) {
  // calculate the indexOf. It works and is not related to the question
  if (indexOf >= 0) {
     ListView list = (ListView) findViewById(R.id.events_list_view);
     list.setSelection(indexOf);
  }
}

I am not sure the layout definition is important to aid the answering of the question, but if you think so I can add it too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和影子一齐双人舞 2024-12-30 11:29:15

您可以通过两种方式实现此目的:

  1. 调用 smoothScrollToPositionFromTop 方法
  2. 调用 setSelectionFromTop 方法

使用 smoothScroll 方法更好,因为它实际上可以顺利地进行转换 - 这意味着它确实滚动到它。
唯一的缺点是它仅在 API 级别 11 之后可用。setSelectionFromTop

方法从 API 级别 1 开始工作,但它不是平滑滚动,而是跳转到行。

如果您不需要定位到屏幕顶部,只需要查看行,您还可以使用 smoothScrollToPosition,这是 API 级别 8 的调用。


如果您给这些方法指定位置,即列表中的FIRST,那么它们将会很好地工作。 (根据您的描述,我认为您可能计算出最后的位置,但我不能确定)。

You can achieve this by two ways:

  1. call smoothScrollToPositionFromTop method
  2. call setSelectionFromTop method

Using the smoothScroll method is better, because it actually does the transition smoothly - that means it really scrolls to it.
The only downside is that it's only available after API level 11.

The setSelectionFromTop method works since API level 1, but instead of smoothly scrolling, it jumps to the row.

If you don't need to position to the top of the screen, only to view the row, you can also use smoothScrollToPosition, which is an API level 8 call.


If you give these methods the position, which is the FIRST in the list, they will work well. (From your description I think you probably calculate the last position, but I can't be sure).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文