Sunteți pe pagina 1din 5

ListView

How it works

ListView is designed for scalability and performance. In practice, this essentially means: 1. It tries to do as few view inflations as possible. 2. It only paints and lays out children that are (or are about to become) visible on screencode. The reason for 1 is simple: layout inflations are expensive operationscode. Although layout files are compiled into binary form for more efficient parsingcode, inflations still involve going through a tree of special XML blockscode and instantiating all respective views. ListView solves this problem by recyclingcode non-visible viewscalled ScrapViews in Androids source codeas you pan around. This means that developers can simply update the contents of recycled viewscode instead of inflating the layout of every single rowmore on that later.

In order to implement 2, ListView uses the view recycler to keep adding recycled views below or above the current viewport and moving active views to a recyclable pool as they move off-screencode while scrolling. This wayListView only needs to keep enough views in memory to fill its allocated space in the layout and some additional recyclable views even when your adapter has hundreds of items. It will fill the space with rows in different waysfrom top, from bottom, etcdepending on how the viewport changedcode. The image below visually summarizes what happens when you pan a ListViewdown.

With this framework in mind, lets move on to the tips. As youve seen above,ListView dynamically inflates and recycles tons of views when scrolling so its key to make your adapters getView() as lightweight as possible. All tips resolve around making getView() faster in one way or another.

View recycling

Every time ListView needs to show a new row on screen, it will call thegetView() method from its adapter. As you know, getView() takes three arguments arguments: the row position, a convertView, and the parentViewGroup. The convertView argument is essentially a ScrapView as described earlier. It will have a nonnull value when ListView is asking you recycle the row layout. So, when convertView is not null, you should simply update its contents instead of inflating a new row layout. The getView() code in your adapter would look a bit like:

public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mInflater.inflate(R.layout.your_layout, null); }

TextView text = (TextView) convertView.findViewById(R.id.text); text.setText("Position " + position);

return convertView; }

About The getView()

An Adapter object acts as a bridge between an AdapterView (such as ListView in your case) and the underlying data for that view. The Adapter provides access to the data items and is also responsible for making a View for each item in the data set.

So, whenever the ListView needs to display a particular row of data, it requests the associated adapter to provide the view corresponding to that the data at that position through getView() method. This may occur whenever the view needs to be updated on screen (eg. during creation/scroll etc.)

getView() of ArrayAdapter is called multiple times.... as an when the new row is added... you scroll up and scroll down the list view.... when the list is notfiedchanged..

S-ar putea să vă placă și