Gehrman
V2EX  ›  Android

布局有二十几个 textview,然后通过解析获取的 jason 数据填充相应的内容.

  •  
  •   Gehrman · Sep 19, 2016 · 14358 views
    This topic created in 3706 days ago, the information mentioned may be changed or developed.
    有没有什么好的方法?不想一个一个搞啊.
    10 replies  •  2016-09-19 16:28:56 +08:00
    nicktogo
    nicktogo
    Sep 19, 2016 via iPhone
    1
    data binding?
    Gehrman
    Gehrman
    OP
    Sep 19, 2016
    2
    @nicktogo 好像是解决这个需求的,我研究下,不知道能不能行,谢谢!
    yuyu2140
    yuyu2140
    Sep 19, 2016❤️3
    3
    咦?怎么跟我这边的需求很像?!
    我的解决方案是,每个 textview 加上 tag 属性,例如:
    <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="name"/>
    然后遍历根布局下所有子控件,取出 tag 作为 json 的 key ,取出 textview 的文字作为 json 的 value 即可。
    yuyu2140
    yuyu2140
    Sep 19, 2016
    4
    上面的是取值的过程,填充的过程是一样的,通过 tag 值遍历填充即可
    Gehrman
    Gehrman
    OP
    Sep 19, 2016
    5
    @yuyu2140 这到是个不错的方法,试试
    lawlietxxl
    lawlietxxl
    Sep 19, 2016
    6
    动态添加 textview 可好?
    rockyou12
    rockyou12
    Sep 19, 2016
    7
    databinding 是最好的!连 findviewById 都不同写,也不像 butterknife 还要整一堆注解。 mvvm 大法好!可惜之前用的时候有少许兼容性问题,不然连 retrolambda 也可以扔掉了
    iFlicker
    iFlicker
    Sep 19, 2016
    8
    DataBinding 看起来很方便 没用过。
    不过楼上说有兼容性是指的 5.0 以下么?
    SorcererXW
    SorcererXW
    Sep 19, 2016 via Android
    9
    TextView[ ]
    Gehrman
    Gehrman
    OP
    Sep 19, 2016
    10
    设置 tag 这个方法可行,而且正好我不止要填充,还要取值.databinding 看起来需要更多的时间来理解
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2549 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 11:35 · PVG 19:35 · LAX 03:35 · JFK 06:35
    ♥ Do have faith in what you're doing.