Fancy dynamic list in Android: TableLayout vs ListView

ぃ、小莉子 提交于 2019-12-11 02:05:58

问题


There is a requirement to have not-so-trivial dynamic list, each record of which consists of several columns (texts, buttons). It should look something like:

Text11 Text12 Button1 Button2
Text21 Text22 Button1 Button2
...

At first obvious way to accomplish that seemed to be TableLayout. I was expecting to have layout/styling data specified in res/layout/*.xml and to populate it with some dataset from java code (as with ListView, for which its possible to specify TextView of item in *.xml and bind it to some array using ArrayAdapter). But after playing for a while, all I found to be possible is fully populating TableLayout programatically. Still, creating TableRow by TableRow and setting layout attributes directly in java code doesn't seem elegant enough.

So the question is: am I at the right path? Is TableLayout really best View to accomplish that? Maybe it's more appropriate to extend ListView or something else to meet such requirements?


回答1:


Using ListView and ArrayAdapter you can do more complicated layouts than just a TextView. You could specify a LinearLayout with 2 TextViews and 2 Buttons for each row in the List.

here's a similar question Android: ListView elements with multiple clickable buttons




回答2:


IMHO it depends on the amount of your data you need to render.

Build layout dinamically via inflate/addView is a quite simple task but is also more slow than using a custom adapter. with a custom adapter you can reuse the convertView parameter and then set the values more efficiently



来源:https://stackoverflow.com/questions/3052678/fancy-dynamic-list-in-android-tablelayout-vs-listview

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