ListView getView() called too often

╄→гoц情女王★ 提交于 2019-12-10 17:33:09

问题


I have a ListView with custom Adapter. To be honest, I have many of them at the same time on screen, and my Tegra 3 device started to lag, what made me really confused... I found than in each ListView's Adapter the getView() method is called for all visible rows every time any animations runs on screen. That gives me like few hundreds of calls per second! Digging more, most of these calls are due to measure() and onMeasure() calls of ListViews' parents, and - this is tke key - they are useless, because all the layouts of my ListViews have const size.

So my question is: how to eliminate these calls? Of course I want to leave proper calls alone (caused by adding items to Adapter and notifyDataSetChanged() ).

I've tried almost anything, but either the whole list doesn't draw itself (when I overriden it's onMeasure() and forced to returned const size without calling super.onMeasure()) or stops updating at some time.


回答1:


How you implemented the getView() method? If you implement it in the correct way there should be nearly no lagging.

Check out this really really good video:

  • http://www.youtube.com/watch?v=wDBM6wVEO70
  • Slides: http://dl.google.com/googleio/2010/android-world-of-listview-android.pdf

As Romain said, work with it not against it. Best is to leave measure() alone and focus on your adapter.




回答2:


Thats how ListView is implemented.. I don't think that will cause a performance Overhead.. Provided you do things properly there..

For example..

Don't instanciate LayoutInflator inside GetView Method, Do it at class level..

And Inflate View Only if the convertView==null or else just return convertView.. Inflating view is a costly process....




回答3:


Well like you said these calls are due to measure() and onMeasure() calls of ListViews parents and I'm sure you are using height=wrap_content also with wrap_content on height your ListView will check without stop if your height has changed.

So the solution is to put the height=fill_parent. I hope this helped you.




回答4:


The underlying reason for this is that ListView.onMeasure() calls AbsListView.obtainView(), which will request a view from your list adapter. So if your view is being remeasured through animations, your performance will be very poor.



来源:https://stackoverflow.com/questions/9940971/listview-getview-called-too-often

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!