开发者

Android: Building View with lots of elements is incredibly slow (10+ seconds)

First of all: I've started developing for Android only recently, so please don't toss stuff at me for asking stupid / obvious questions! ;D

The problem I am encountering is the following: I want to display a list of clickable items of unknown length - my sample data set is around 300. Using a LinearLayout and adding them all as TextViews takes like half a second on the emulator, which is totally fine. To enhance the user experience I have replaced the LinearLayouts now with Relative Layouts, each with a background image, 2 TextViews and 3 ImageViews. All images are included in the resources, so no loss of time for downloading or whatnot.

Problem is: It now takes around 10 seconds to build this view, which is obviously no viable solution. My assumption would be that the complete UI is completely built despite only always a part of it being visible (using some scroll container)

As I am far too lazy to write logic to manually make sure only the visible part of the UI is actually populated, I was wondering if anyone knows of some simple standard solution for this problem.

Is there some sort of UI proxie开发者_StackOverflow社区s I simply have to activate? Or would you recommend creating the screen with like the first 10 elements and load the others in the background in a separate Thread? (No idea how scroll containers - or android UIs in general for that matter - would react to that)

Thanks for all help in advance! =)

tl;dr: Building large Views way too slow - need help! puppy eyes


I want to display a list of clickable items of unknown length - my sample data set is around 300

Use a ListView, or any other subclass of AdapterView.

My assumption would be that the complete UI is completely built despite only always a part of it being visible (using some scroll container)

That's what AdapterView subclasses, like ListView, are for -- only create widgets for what's visible, not the entire data model.

As I am far too lazy to write logic to manually make sure only the visible part of the UI is actually populated, I was wondering if anyone knows of some simple standard solution for this problem.

Use a ListView, or any other subclass of AdapterView.

Is there some sort of UI proxies I simply have to activate?

No.

Or would you recommend creating the screen with like the first 10 elements and load the others in the background in a separate Thread?

No. Mostly because you'll crash. You can only modify the UI from the main application thread, not a background thread that you fork.


Obviously Mark's answer is correct and the way you need to go for this particular problem.

However, in the future I suggest give the documentation a quick glance next time you have an issue. The View documentation in particular is often my go-to page for discovering new (to me) Android framework capabilities.

In general, if you need something and think it might be too much effort to do but that it's common enough that it should've already been done by someone, I recommend these steps:

  • Open the view docs and look under "Indirect Subclasses" for something that sounds what you need.
  • Open up the API Demos sample app and see if you can find something that looks like what you're trying to achieve. It has quite good coverage of the API and you can easily find the code in the Sample Code section.
  • Check out the other sample projects. There are some pretty involved ones in that directory.

Of course, Stack Overflow is a great resource, but sometimes the answers are too specific and don't lend themselves to discovering new features.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜