如何使列表视图可滚动,直到最后一个元素位于屏幕顶部
我正在开发一个需要显示日历议程的应用程序,就像本机日历中的议程一样。我有一个显示不同事件的列表视图(注意:在问题的上下文中,事件是我的系统的实体)。我想在此屏幕上提供“今天”按钮。当用户单击此按钮时,事件应该滚动,直到当前日程表的第一个事件出现在屏幕顶部。当我从今天开始只安排了几个活动时,就会出现问题 - 活动太少以至于无法填满整个屏幕。然后列表视图会滚动,直到日历中的最后一个事件位于底部。这通常意味着没有达到将今天的第一个事件放在首位的预期效果。
有什么建议可以做到这一点吗?我曾想过在最后添加一些空白元素,但这似乎是丑陋的解决方法,而且它需要特殊的特定于设备的计算来告诉我要插入多少个元素。
编辑:
根据评论中的要求添加一些代码
实际上,我不确定这段代码会让任何人感到惊讶,但是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过两种方式实现此目的:
使用 smoothScroll 方法更好,因为它实际上可以顺利地进行转换 - 这意味着它确实滚动到它。
唯一的缺点是它仅在 API 级别 11 之后可用。setSelectionFromTop
方法从 API 级别 1 开始工作,但它不是平滑滚动,而是跳转到行。
如果您不需要定位到屏幕顶部,只需要查看行,您还可以使用 smoothScrollToPosition,这是 API 级别 8 的调用。
如果您给这些方法指定位置,即列表中的FIRST,那么它们将会很好地工作。 (根据您的描述,我认为您可能计算出最后的位置,但我不能确定)。
You can achieve this by two ways:
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).