Android simple stuff about scrolling - android

As I don't know how to ask it to google, I'll try here.
I basically have a listView displayed from an intent. This listView is feeded by a custom BaseAdapter.
This listView is actually a calendar. I want on the opening of the activity having this calendar listView that it has been already scrolled to te current day.
Basically I'm asking how can I pre-scroll a listview to an index of the listView ? Is this possible ? With a function of custom BaseAdapter ?
Thanks.

You can make use of following methods -
This method to save save list scroll position.
private void saveListScrollPosition() {
// save index and top position
index = _listview.getFirstVisiblePosition();
View view = _listview.getChildAt(0);
top = (view == null) ? 0 : view.getTop();
}
And this one to get it back and scroll it down automatically -
private void restoreListScrollPosition() {
// restore
_listview.setSelectionFromTop(index, top);
}
I think in your case, index will be the current date. Right?

Use,
list.setSelection(index);

Related

How to show the target items in ListView to the top of the screen

I am writing an android app which contains a listview with some items (fetch by database, can be 100+)
i would like the scroll the listview to display the specific item (let say position x in the listview)
i used
listView.post(new Runnable() {
#Override
public void run() {
listView.smoothScrollToPosition(position);
}
});
the view has scrolled. but it stopped once the item appear. (the item is now displayed at the bottom of the screen)
i think better design is to display the item at the top of the screen or at least at the middle of the screen.
How can i do this?
I am now finding the way to find out the index of the listview which is really visible in the screen.
I have tried getFirstVisiblePosition() but it seems always return 0.
You will find setSelection() works better than smoothScrollToPosition(). If you are loading the data backing the ListView in the background, you will want to delay issuing the setSelection() until the data has been loaded.
getFirstVisiblePosition() works fine for me, but again is only useful once all the data has been loaded. Here is some code I use for a ListView that displays data fetched from a remote site:
// the list has been updated, fix the selection. loadFinished will be true if there are no phantom placeholders left.
protected void listLoaded(boolean loadFinished) {
// if the user has scrolled or we previously completed resetting the position, do nothing
if(wasScrolled)
return;
// if we were restored from saved data, reload that data now.
if(listState != null) {
postListView.onRestoreInstanceState(listState);
listState = null;
} else if(initialPos >= 0) // if we had a specific initial position, set it now
setSelection(initialPos);
else if(postId >= 0) // or if there is a specific post ID we want displayed
setSelection(postAdapter.getPostPosition(postId));
else // otherwise scroll to the first unread post
setSelection(postAdapter.getFirstUnread());
if(loadFinished) // if all data has been loaded, set a flag to prevent this being repeated
wasScrolled = true;
}

Android ListView add items to top without list view scroll [duplicate]

This question already has answers here:
Retaining position in ListView after calling notifyDataSetChanged
(7 answers)
Closed 7 years ago.
I have a listView and I want to add new items to the top of the list view but I dont want list view to scroll its content. I want user to look at the same item as he was looking before new items were added.
This is how I add new items to ListView:
this.commentsListViewAdapter.addRangeToTop(comments);
this.commentsListViewAdapter.notifyDataSetChanged();
and this is addRangeToTop method:
public void addRangeToTop(ArrayList<Comment> comments)
{
for (Comment comment : comments)
{
this.insert(comment, 0);
}
}
this is my listView:
<ListView
android:id="#+id/CommentsListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/AddCommentLayout"
android:stackFromBottom="true" >
</ListView>
What I want to do is to load old comments when user scrolls to the top.
Thank you for your help.
I have found solution here Retaining position in ListView after calling notifyDataSetChanged
Sorry for duplicate question.
The final code is this:
int index = this.commentsListView.getFirstVisiblePosition() + comments.size();
View v = this.commentsListView.getChildAt(commentsListView.getHeaderViewsCount());
int top = (v == null) ? 0 : v.getTop();
this.commentsListViewAdapter.AddRangeToTop(comments);
this.commentsListViewAdapter.notifyDataSetChanged();
this.commentsListView.setSelectionFromTop(index, top);
May be this is what you are looking for:
android:transcriptMode="normal"
"This makes list automatically scroll to the bottom when a data set change notification is received and only if the last item is already visible on screen." - as quoted here
Also have a look at ListView's method public void setSelection (int position). After you added new comments, and notified your adapter, you can use it to keep the current item selected.
// Get the current selected index
int previousSelectedIndex = yourListView.getSelectedItemPosition();
// Change your adapter
this.commentsListViewAdapter.AddRangeToTop(comments);
this.commentsListViewAdapter.notifyDataSetChanged();
// Determine how many elements you just inserted
int numberOfInsertedItems = comments.size();
// Update the selected position
yourListView.setSelection(previousSelectedIndex + numberOfInsertedItems);
NOTE: Code is untested. Good luck

Set Cursor on the final record in ListView

I am using a ListView to show records. The user can refresh the ListView to add more records on the ListView.I need to show the cursor to the last record of the ListView before the ListView was refreshed.
The ListView is getting refreshed dynamically, but it is showing the topmost record. I wish to show it on to the first record after it has refreshed.
I have tried implementing this logic,
if(histroyRefreshed){
int index = getListView().getFirstVisiblePosition();
View v = getListView().getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// ...
// restore
getListView().setSelectionFromTop(index, top);
}
Keep your item id into a local field, once you have updated your list view, use your pre-stored id to find out where it is.
getListView().setSelection(location);
This sets the location to the last record,before history is refreshed

Synchronize two ListView positions when you scroll from both list views Android

I have two ListViews. Is there any way to synchronize the position of ListViews when I scroll both the Lists
Use the following method to get the scroll position of your first listView-
private void saveListScrollPosition()
{
// save index and top position
index = _listview1.getFirstVisiblePosition();
View view = _listview1.getChildAt(0);
top = (view == null) ? 0 : view.getTop();
}
And scroll the second listView to that position with-
// restore
_listview2.setSelectionFromTop(index, top);
You could use this in your second list view: smoothScrollToPosition(position)
And in your first ListView you could use a OnScrollListener and check the first visible item with getFirstVisiblePosition.
Best wishes,
Tim

ListView/ListAdapter paging

I'm trying to implement paging in a custom ListAdapter. Right now I'm just making the request for the next page when the last item in the ListView becomes visible, by checking in getView() if position is >= the size of ListAdapter.getCount().
It works fine, but I'm wondering if there's a better way (or a different way) that will only make the request once the last item in the list is actually visible to the user. Anyone know of a way?
I'm doing it almost the same way:
public static final int SCROLLING_OFFSET = 5;
// ...
private final ArrayList<T> items = new ArrayList<T>();
// ...
if (SCROLLING_OFFSET == items.size() - position) {
if (hasNextPage()) {
addNextPage();
}
}
private boolean hasNextPage() {
// basically calculates whether the last 2 pages contained the same # of items
}
private void addNextPage() {
// show spinner
// fetch next page in a background thread
// add to items
notifyDataSetChanged();
}
I think there is a better way to do it. Implementing the OnScrollListener interface. Take a look at this: Endless Scrolling ListView
Try removing the check altogether. In my experience, getView() is only called when the entry is about to come on screen.

Categories

Resources