Making dynamic layout for each row in ListView in android

后端 未结 2 2017
夕颜
夕颜 2021-01-25 07:52

I have a xml file which contains basic layout for each row of ListView(which is a realtive layout and has TextView inside it).

I want to change the attributes of this la

2条回答
  •  离开以前
    2021-01-25 08:23

    Making dynamic layout for each row in ListView in android

    Maybe @Arkde's answer could work but i i think it's little dirty solution.

    What about to create one generic layout and update / change appearance due to provided conditions? (for example due to value in current row).

    different layout width and height of each row

    This can by easily achieved by an usage of "margins". By "margins" i think a creation of empty Views which will work as "margins" and will determine height of row for instance. And then due to mentioned condition(s) showing or hiding them1.

    What are advantages of generic layout?

    • Easier, efficient and more human-readable solution
    • You don't need to implement ViewType and ViewCount for Adapter
    • One layout for each row with changeable appearance
    • You don't need to change UI appearance from application logic in "hardcoded" way

    1 When visibility of View is assigned to View.GONE it won't take place in layout whereas View.VISIBLE takes place.

    Example of "margin":

    
       ...
       
    
       ...
    
       
       ...
    
    

    Note: Suggested an usage of ViewHolder is very neat and effective approach if you want to increase perfomance of ListView.

    I hope that my solution will help to solve your problem you're facing now.

提交回复
热议问题