I have a ListActivity class that populates a list using a CursorAdapter. What I need to do is have the view scroll down to a specific element within the list, based on the values of elements in the list开发者_如何学编程.
The list is a list of items with 'due dates' and if the due date has already passed, I want the view to scroll past it. Therefore upon launch, the user will see the item with the nearest due date at the top (the view will be scrolled to this item). If they want to see past due dates, they can scroll up.
I hope I'm explaining this properly, I simply want to hide items in my ListActivity if their due date has already passed, based on the time the Activity is launched of course.
This works:
ListView list = getListView();
int count = list.getCount();
long now = new GregorianCalendar().getTimeInMillis();
int scrollTo = 0;
for ( int i = 0; i < count; i++ ) {
Cursor cursor = (Cursor)list.getItemAtPosition( i );
long endtime = cursor.getLong( "endtime" );
if ( endtime > now ) {
//this is the one we want to scroll to
scrollTo = i;
break;
}
}
temp.setSelection( scrollTo );
Look through the items to find the break point then use the smoothScrollToPosition
function to animate to that position
Android Developer Site : AbsListView#smoothScrollToPosition
this is just an idea, but if you have a list of items and you can grab the due date and you also have information on the current date, why dont you adjust the gravity of the list so that it has the current note at the top....
OR
if you have a list of notes in the listview, in your onresume and oncreate methods for the activity, reshuffle the notes in the list so that the ones that are passed get put to the bottom of the list, that way the current notes will always be on top and you dont have to worry about trying to mess with the gravity
精彩评论