I've got a custom Adapter for a ListView setup and working ok. On a button click something is changed in the underlying data, so a notifyDataSetChanged is required to refresh the ListView. Fine. But I also want the ListView to scroll to the line where the change occurred. For this I call smoothScrollToPosition immediately after the notifyDataSetChanged. And that is not working.
If I do not call notifyDataSetChanged then the scroll works, so the notify blocks the scroll. I'm probably having a fight with events being processed in the future. Can anybody give me a pointer on what is going wrong?
Use the post() method to wait for the list to finish updating after you call notifyDataSetChanged():
adapter.notifyDataSetChanged();
list.post( new Runnable() {
#Override
public void run() {
//call smooth scroll
}
});
I've tried the dmon's example code and I see it not always working.
Actually I've found another approach to perform scrolling:
adapter.notifyDataSetChanged();
channelsListView.setSelection(scrollPos); // it works without smoothScrollToPositionFromTop(..) or smoothScrollToPosition() methods call.
I found the main idea to use above method after this answer reading:
https://stackoverflow.com/a/20997828/2133585
Related
I have a fragment using pull-to-refresh to refresh my listview, I know when user pull by hand, OnRefreshListener triggered, But i want this:
When users open this fragment, they don't have to pull by hand, fragment itself pull to refresh and show loading animation. Please tell me what to do.
Thanks.
It might be a bit late but for reference i had the same issue and this is how i solved it:
When you call:
mPullToRefreshLayout.setRefreshing(true);
It wont trigger the circle to animate automatically. To show the loading circle you need to use a runnable, this adds a delay in the UI thread so that it shows the circle animation inside the ui thread. You do this by using the below code in place of the above:
mPullToRefreshLayout.post(new Runnable() {
#Override public void run() {
mPullToRefreshLayout.setRefreshing(true);
}
});
Hope it helps.
try using youPullToRefreshList.setRefreshing()
I've got a ListView with a method (public ArrayAdapter<String> populateListView(){}) to update the Adapter. Every 500ms I call lv.setAdapter(populateListView();. This works, but if I'm scrolling down the ListView, when the method is called it resets my view and automatically scrolls me to the top. So if I'm 80% down my ListView, when the method is called I'm reset to 0%.
How do I continually populate my ListView without resetting the scroll position?
I've already tried calling lv.getScrollY(); before setting the adapter and then lv.setScrollY(); but this doesn't help. I've also tried calling lv.scrollTo(0, scrollY); but this doesn't help.
How do I stop resetting the scroll position when updating the adapter?
Try updating the items in the adapter and call adapter.notifyDataSetChanged() instead of creating a new adapter.
Example:
adapter.replaceItems(yourNewListWithData);
adapter.notifyDataSetChanged();
You have to implement the replaceItems method yourselves.
public void replaceItems(List<? extends E> newItems) {
entries.clear();
entries.addAll(newItems);
}
I have a listfragment, whose listview is loaded using a cursorloader from the contacts provider. An onScrollListener is also registered.
Requirement : The scroll listener is triggered whenever the user does a scroll through the UI. But i need to trigger it automatically when the listview is loaded for the first time as well.
Problem : I have tried using smoothScrollToPosition(0) in onActivityCreated() to trigger the scroll, but it does not scroll no matter what position i provide as parameter. Based on some other posts on SO, I have tried using setSelection() instead, which does work, but does not trigger the scroll listener. I have tried the post method as well as below, but it makes no difference :
v.post(new Runnable() {
#Override
public void run() {
v.smoothScrollToPosition(0);
}
});
Any other way to trigger a scroll automatically?
After I've downloaded a list of objects from a rest service, obviously in an AsyncTask, I set them into a custom BaseAdapter. When I call again the service to load more data and add them to the List of object, and call the notifyDataSetChanged() the ListView block for a sec or two.
I've tried to move the add and notify into another AsyncTask but since I'm modifying the UI this raised an exception.
I've tried also to change the addAll with a loop where I add an item at time and call the notifyDataSetChanged() everytime but with no success.
Which is the best practice in this case?
Sorry for no code but I'm from my phone, this thing really puzzles me.
How many times you are call notifyDataSetChanged()? You need to call it once, after you add all data. If you show code of your AsyncTask class and how do you use it, I will tell more.
I have a very simple application. There is a refresh button, everytime it is being clicked, the adapter will call notifyDataSetChanged() to update a ListView. However, I got IllegalStateException all the time. Any quick solution? Thanks.