Is there any difference between ListView.invalidateViews() and Adapter.notifyDataSetChanged()?
Well yes, there is.
ListView.invalidateViews() is used to tell the ListView to invalidate all its child item views (redraw them).
Note that there not need to be an equal number of views than items. That's because a ListView recycles its item views and moves them around the screen in a smart way while you scroll.
Adapter.notifyDataSetChanged() on the other hand, is to tell the observer of the adapter that the contents of what is being adapted have changed. Notifying the dataset changed will cause the listview to invoke your adapters methods again to adjust scrollbars, regenerate item views, etc...
Most of the time you would want to use notifyDataSetChanged instead of invalidateViews, but it certainly depends on what you are trying to accomplish.
Related
every time when I call notifyDataSetChanged() on my adapter the listview displayes the last item in the adapter array.
Is there any possible way to scroll the listview to the top off screen, since the setSelection(n) method within the post runnable is noticable, because the listview jumps to the bottom and then to the top. This movement can be seen on the screen unfortunately.
Thanks in advance.
When you call notifyDataSetChanged(), you're telling the ListView to refresh its contents, and it will actually try not to scroll if possible. It knows what it's currently scrolled to based on the items' positions and ids that it has obtained from the adapter. Your issue may be that your adapter is giving out inconsistent ids from getItemId that are confusing the ListView, causing it to scroll unnecessarily. Can you explain a little more about what your code is doing at the time you call notifyDataSetChanged()? Are you adding or removing items in the adapter at the same time?
I think this is what you need:
lv.setSelection(0);
I have a ListView which onItemClick selected item changes its layout, pops different buttons. If any other item is selected, the previous selected one returns to normal. My ListView adapter works fine but refreshing the whole list with notifyDataSetChanged() in my adapter takes too much time.
My problem is to refresh only the changed items in the ListView.
And also I would like to have suggestions for better scrolling performance.
try to implement View Holder Pattern it increases the performance of loading and scrolling of ListViews
Making ListView Scrolling Smooth | Android Developers
Using lists in Android (ListView) - Tutorial - Vogella
from the docs:
Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.
you can define one method in adapter class which will return current item view. in onitemclick use this method to make changes in clicked item. You can define class view type class variable in activity and store previous view there...
ListView scrolling performance slows down when widgests like textviews, images are at the bottom of the layout hierarchy.
So for improving list performance one should design item xmls with minimum layout tree levels.
I'm trying to achieve the effect of the footer for ListView but without using the addFooterView method of the ListView. My intention is to treat the last visible item of the list as a pinned footer. In my view I can achieve this by detecting the last visible item on the list and dynamically change it's layout. I did some research and I think I must extend the BaseAdapter class providing two types of items. One for ordinary item on the list indicating that adapter should inflate the item with ordinary layout. And the second one indicating that adapter should inflate the current item with layout of footer. I think i must override the onScroll method to detect the last visible item. And here are my questions. Should i call the getView method from the onScrollmethod ? Is it the proper way to implement such effect? Is it possible at all? I would be grateful for any suggestions.
Thank in advance.
And here are my questions.
Should i call the getView method from the onScrollmethod ?
no, you should never directly call getView(). Only classes that extend AbsListView call getView
Is it the proper way to implement such effect?
No. The proper way of doing it is calling addFooterView to your ListView object.
Is it possible at all? I would be grateful for any suggestions.
No, it's not possible. Using scroll listeners you will never be able to find out when the AbsListView is requesting the "last view". That's because ListView does not guarantee the order it queries for Views. That's specially true when you 1st set the adapter or call notifyDataSetChanged which causes the ListView to get the views in several points to be able to layout and measure stuff it needs.
The suggestion is to use the addFooterView method, that's the correct way of doing, that's why it's there.
I've got a question about the performance of a ListView. My application uses a ListView with approximately 20 items and i was wondering what should i do if the data of one item has changed. Should i call notifyDataSetChanged() on the adapter and cause a redraw of the whole list or should i take care only of the item to refresh itself?
What is the cost of notifyDataSetChanged()? Can i use this without hesitation? An ListView item of mine has about 3-4 TextViews and an ImageView.
Any suggestions?
notifyDatasetChanged refreshes the whole list each time its called so it is better if you call that after you have all of your data that you want so you are not doing unnecessary work
mMyListView.invalidate();
mMyListView.invalidateViews();
My problem is similar to ListView getChildAt returning null for visible children, but despite searching I cannot find a solution.
I have a ListView with a Scroll. The ListView has 10 items, 7 of which are visible and 3 are hidden by scroll. I also have an external method (out of adapter) that must get all of the children from this ListView (e.g. using getChildAt()).
I need all 10 of the items, but the last 3 are null objects. I've tried code like the following:
getListView().smoothScrollToPosition();
But this doesn't work.
I think that I don't need to post the rest of my code, as the description says everything?
As you have already seen you can't get all the child row views from a ListView simply because a ListView holds only the views for the visible rows(plus some recycled rows but you can't reach those). The correct way to do what you want is to store whatever data in the adapter's data and retrieve it from there.
But the ListView doesn't keep the current values from RadioGroup in
running time.
I've seen that you have some problems with this so I've adapted some old code to build a basic example, code that you can find here.
I don't think so you need to add scroll view for a listView. Scroll automatically works on ListView. Try your application without adding scroll view and I'm sure it'll work as you needed.
The reason those children are null it's because they really do not exist and they will never exist, if only 7 children are on the screen at one time, the system will only create 7 and re-use by passing the convertView back to the adapter getView() method.
If you want to grab information regarding your whole dataset you should search on the dataset itself, instead of the views on the screen. E.g. if it's an ArrayAdapter, loop the array; if it's a CursorAdapter, loop the cursor; etc.
The non-visible children of a listView don't actually exist. When they become visible, one of the redundant views is recycled or a new view is generated. So you can't actually access all the views. Why do you want to? Whatever changes you want to make should be made to the data that populates the views rather than the views themselves.
There are a few point that you need to take care of:
1. List view provides inbuilt scroll functionality, So don't use Scroll view. It will only mess up things.
2. List view doesn't contain ALL the children. When you scroll it, it creates only visible items on run time.
3. If you want to get all the children altogether, Better keep an ArrayList of the child objects that your list has. You can add or remove children to this ArrayList as per requirement.