4

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.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135

1 Answers1

0

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).

balazsbalazs
  • 4,071
  • 1
  • 24
  • 29
  • Thank you for the answer. However, I do not have at my disposal device supporting more than Gingerbread (Api level 9-10). Probably I will test your suggestion in an emulator later on just to verify your suggestion for future readers, but this will not solve my problem for sure. As you volunteered to try to figure out something for older versions and I myself have run out of ideas I will be most grateful if you can further assist me in my struggle. – Boris Strandjev Dec 30 '11 at 08:07
  • I just now got the chance to test what you proposed. Regretfully it seems you misunderstand what I asked. the methods you propose really do scroll, but as much as I already have it. My problem is: imagine I have many items say 1000 in the list. If I say scroll to the 40th everything is fine and it is displayed on the top of the screen. However if I say 'scroll to 999th' it just scrolls until this item is visible and does not place it on top. The reason being no empty space exists after the linear layout (probably?). I need every item to be placed on top. – Boris Strandjev Jan 02 '12 at 13:29
  • Oh, ok, I misunderstood. Unfortunately, however, with the ListView it's not possible doing what you want to do. What you can do is, to calculate the necessary height (using getFirstVisiblePosition(), getChildAtIndex(), getTop() methods) and then inserting a row with at the end of the list. It won't be easy though, as you have to take care of several things. – balazsbalazs Jan 02 '12 at 16:53