سؤال

I'm using image adapter to fill my android ListView.

I know the adapter recycles items in the ListView.

but if i need to nullify every view in each item in the list,

what's the benefit anyway in recycling the items?a

i mean, when wouldn't i want to nullify every list item?

I have tried to nullify every view in any item but then i have thought - what's the point in recycling anyway and how can i avoid the tedious nullifying.

adding:

My problem is when i change an image view in the first item's layout

I then scroll down and see this image has also changed in the 4th and 8th items.

It gives the changed image instead of the default one.

I can overcome this by putting the defualt image everytime (appears in the layout xml anyway)

but then i think: do i have to do it on evry single view in the item's layout?

why not defaults from the xml?

هل كانت مفيدة؟

المحلول

The ListView recycles the Views that are contained in the list. The Views themselves are merely visual representations of the data that the provided Adapter gives it. The ListView itself does absolutely nothing to the actual data that it is showing. You don't have to nullify anything.

The reason this happens is in ListViews, the list items are almost all exactly the same (usually). So when a list item goes out of sight, the ListView will take the View and give it to the adapter. The adapter will then fill the View with the appropriate data and give it back to the ListView. The ListView will then stick it to the bottom (or top) of the visible part of the list. This cuts down on overhead of inflating new Views. It limits memory usage of keeping View objects in memory. Makes an overall smoother scroll.

نصائح أخرى

what's the benefit anyway in recycling the items?

Don't really sure I get you by writing nullifying, But the recycling is very useful.

Let's say your screen can show only 5 rows of your ListView, so why inflating all items and showing only 5? inflate only 5 and when scrolling down inflate more.

when going back from bottom to top the first item was inflated but recycled, so android took the view and use it in the bottom rows, so it's not null but with incorrect values, so you need to change it's values by the correct position.

Hope that's what you meant for.

good luck

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top