I am using PullToRefresh in my android app. It is working fine so far.
Issue I am facing is when I download new x rows on "Release to refresh" from TOP new rows pushes existing rows and starts from 0 position it is annoying for the user. What I want is, after downloading new set of rows list should remain there and let user fling down to view new set of rows.
any idea how can I achieve this?
This is what i use
//Get old position before updating adapter
final int old_pos = mListView.getRefreshableView().getFirstVisiblePosition()+1;
//Set Adapter
mListView.setAdapter(mListAdapter);
//Set the list to old postion
//mListView.getRefreshableView().setSelection(old_pos);
mListAdapter.notifyDataSetChanged();
mListView.onRefreshComplete();
mListView.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
mListView.getRefreshableView().setSelection(old_pos);
}
});
also check this:
Maintain/Save/Restore scroll position when returning to a ListView
Related
I am trying to keep my last scrolled position and populate the adapter with new data but same scrolling position. In my scenerio, I have updating expandable listview every 5 second and updating new items very 5 seconds but with last scrolling position left by user.
I have searched a lot about it and found few solutions but still it is behaving strangly each time I set scrolling position.
I am trying to achieve it with this method:
activity.runOnUiThread(new Runnable()
{
#Override public void run() {
state = list.onSaveInstanceState();
if(settings != null) {
if (mode.equals("Now")) {
info_panel = new esInfoListAdapter(activity, data, expanHash, false, esActivity.lastgrpPosition);
}
else {
info_panel = new esInfoListAdapter(Activity.this, data, tchildData, true, lastgrpPosition);
}
}
list.setAdapter(info_panel);
if(state != null)
list.onRestoreInstanceState(state);
info_panel.notifyDataSetChanged();
}
}
});
So this method gets called just before I updating expandable listview. So in short in every five seconds. I am using Parceable object to save list.onSaveInstanceState(); and then use list.onRestoreInstanceState(state); when updating new data, but the issue is sometimes it works and sometimes not? Am I missing some trick here? Thanks for your help.
I have not yet used ExpandableListView yet but I might. I think you should use combination of methods like getFirstVisiblePosition, getLastVisiblePosition, and getExpandableListPosition. I think getting the groupPosition is sufficient for a user. A link I referenced is ...get index of first/last visible group in an ExpandableListView
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;
}
I'm using a ListView with a custom ArrayAdapter.
The List is an infinite scroll of tweets.
Updates to the list are inserted from the top.
I want to obtain an effect as the Twitter application. I'm not talking about the "scroll to update", but to maintain the position after the update.
I've just implemented some code that works in that way. Here it is:
// get the position of the first visible tweet.
// pausedCounter traces the number of tweets in the waiting line
final int idx = listView.getFirstVisiblePosition() + pausedCounter;
View first = listView.getChildAt(0);
int position = 0;
if (first != null)
position = first.getTop();
// here I update the listView with the new elements
for (Tweet[] tweets1 : pausedTweets)
super.updateTweets(tweets1);
final int finalPosition = position;
// this code maintain the position
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelectionFromTop(idx, finalPosition);
}
});
The problem of this code is that for an instant the listView goes to the first element of the list, then kick in the setSelectionFromTop and it goes to the correct position.
This sort of "flickering" is annoying, and I want to remove it.
I found out only this solution:
// add the new elements to the current ArrayAdapter
for (Tweet[] tweets1 : pausedTweets)
super.updateTweets(tweets1);
// create a NEW ArrayAdapter using the data of the current used ArrayAdapter
// (this is a custom constructor, creates an ArrayAdapter using the data from the passed)
TweetArrayAdapter newTweetArrayAdapter =
new TweetArrayAdapter(context, R.layout.tweet_linearlayout, (TweetArrayAdapter)listView.getAdapter());
// change the ArrayAdapter of the listView with the NEW ArrayAdapter
listView.setAdapter(newTweetArrayAdapter);
// set the position. Remember to add as offset the number of new elements inserted
listView.setSelectionFromTop(idx, position);
In this way I have no "flickering" at all!
I have created a dynamic ListView where objects are added from top.
When the user press a button the listView is updated from the contents of an array, then notifyDataSetChanged() is called on the custom arrayAdapter.
Now I want to mantain the list position when adding, so I added this code:
// pausedCounter trace the number of objects(lines) to add to the listView
int idx = listView.getFirstVisiblePosition() + pausedCounter;
View first = listView.getChildAt(0);
int position = 0;
if (first != null)
position = first.getTop();
// cycle to add the new objects to the listView
for (Tweet[] tweets1 : pausedTweets)
super.updateTweets(tweets1);
listView.setSelectionFromTop(idx, position);
// reset of counter and accumulator
pausedTweets = new ArrayList<Tweet[]>();
pausedCounter = 0;
This code behave in this way: if the getFirstVisiblePosition returns 2, and the pausedCounter is 5, after the update the list will be set to the 3th of the new five elements.
What I want is to have the first visible element of the list set to the 8th.
After further tests I found out that the number of childrens of the listView doesn't change during the run of this piece of code, so it updates the size of the listView after I called setSelectionFromTop. Could be this the problem?
The trick was this:
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelectionFromTop(idx, finalPosition);
}
});
Using the post method permits to wait the update of the ListView before change position.
I set a timer in my app, I could get some information from a web service and resulted in a list view to display. Now my problem is that every time the timer runs, scroll back to the beginning ...
how can i keep Scroll position with every refresh in list view ?
part of my code:
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(DashboardActivity.this,
all_chat,
R.layout.list_item,
new String[] { TAG_FULLNAME,
TAG_DATE,
TAG_MESSAGE },
new int[] { R.id.fullname,
R.id.date,
R.id.message }
);
// updating listview
setListAdapter(adapter);
}
});
TNx.
Do not call setAdapter(). Do something like this:
ListAdapter adapter; // declare as class level variable
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
*/
if (adapter == null) {
adapter = new SimpleAdapter(
DashboardActivity.this, all_chat, R.layout.list_item, new String[]{TAG_FULLNAME, TAG_DATE, TAG_MESSAGE},
new int[]{R.id.fullname, R.id.date, R.id.message});
setListAdapter(adapter);
} else {
//update only dataset
allChat = latestetParedJson;
((SimpleAdapter) adapter).notifyDataSetChanged();
}
// updating listview
}
});
You can add the following attribute to your ListView in xml.
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
Add these attributes and your ListView will always be drawn at bottom like you want it to be in a chat.
or if you want to keep it at the same place it was before, replace alwaysScroll to normal
in the android:transcriptMode attribute.
Cheers!!!
I had the same issue, tried a lot of things to prevent the list from changing its scroll position, including:
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
and not calling listView.setAdapter();
None of it worked until I found this answer:
Which looks like this:
// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - mList.getPaddingTop());
// ...
// restore index and position
mList.setSelectionFromTop(index, top);
Explanation:
ListView.getFirstVisiblePosition() returns the top visible list item. But this item may be partially scrolled out of view, and if you want to restore the exact scroll position of the list you need to get this offset. So ListView.getChildAt(0) returns the View for the top list item, and then View.getTop() - mList.getPaddingTop() returns its relative offset from the top of the ListView. Then, to restore the ListView's scroll position, we call ListView.setSelectionFromTop() with the index of the item we want and an offset to position its top edge from the top of the ListView.
There's a good article by Chris Banes. For the first part, just use ListView#setSelectionFromTop(int) to keep the ListView at the same visible position. To keep the ListView from flickering, the solution is to simply block the ListView from laying out it's children.