简体   繁体   English

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

[英]How to make list view scrollable until the last element is on top of the screen

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. 我不确定布局定义对于回答问题很重要,但如果您这么认为我也可以添加它。

You can achieve this by two ways: 您可以通过两种方式实现这一目标:

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

Using the smoothScroll method is better, because it actually does the transition smoothly - that means it really scrolls to it. 使用smoothScroll方法更好,因为它实际上平滑过渡 - 这意味着它真正滚动到它。 The only downside is that it's only available after API level 11. 唯一的缺点是它只能在API级别11之后才能使用。

The setSelectionFromTop method works since API level 1, but instead of smoothly scrolling, it jumps to the row. setSelectionFromTop方法从API级别1开始工作,但它不是平滑滚动,而是跳转到该行。

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. 如果您不需要定位到屏幕顶部,只需查看行,您还可以使用smoothScrollToPosition,这是API级别8调用。


If you give these methods the position, which is the FIRST in the list, they will work well. 如果给这些方法提供位置,即列表中的FIRST ,它们将很好地工作。 (From your description I think you probably calculate the last position, but I can't be sure). (根据你的描述,我认为你可能会计算最后一个位置,但我不能确定)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM