请教大佬们 ViewBinding 和 findViewById 的差异 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hu8245
V2EX    Android

请教大佬们 ViewBinding 和 findViewById 的差异

  •  
  •   hu8245 2022-03-21 23:43:10 +08:00 12794 次点击
    这是一个创建于 1385 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Android 新手。在实现一个功能,简单来说,就是一个 静音按钮,是一个 ImageView ,点击静音按钮,无色的 icon_passive 变为 icon_active 。 我一开始用 ViewBinding ,发现,点击后,红色的 icon_active 是叠加在 icon_passive 之上的。但是 indViewById 就是替换的效果,不会叠加。想问各位大佬,ViewBinding 和 findViewById 有使用上的区别吗?在 Google/StackOverFlow 找不到理想的回答。

    protected void initButtonAndEvents(){ // ImageView muteAudio = binding.ivMuteAudio; // 会出现 图像 叠加 ImageView muteAudio = getActivity().findViewById(R.id.iv_mute_audio); // 不会出现图像叠加 muteAudio.setImageDrawable(getResources().getDrawable(R.drawable.ic_mute_audio_default, null)); muteAudio.setOnClickListener(v -> { mViewModel.setIsMuteAudio(!mViewModel.getIsMuteAudio()); muteAudio.setImageDrawable(getResources().getDrawable(mViewModel.getIsMuteAudio() ? R.drawable.ic_mute_audio_active : R.drawable.ic_mute_audio_default, null)); }); } 
    7 条回复    2022-03-25 11:33:35 +08:00
    Tolcol
        1
    Tolcol  
       2022-03-21 23:53:57 +08:00   1
    可以看看 setContentView 那边,可能 ViewBinding 设置了两次?
    hu8245
        2
    hu8245  
    OP
       2022-03-22 00:15:11 +08:00
    @Tolcol 多谢。问题不在这里,这两个获得的 view 对象是同一个。根本原因在其他地方(后来发现切换横屏导致了这段代码执行了两次。)。thread 结束
    251
        3
    251  
       2022-03-22 00:16:36 +08:00
    直接学 compose API 吧。
    FringJX
        4
    FringJX  
       2022-03-22 00:25:49 +08:00 via iPhone
    你直接 binding.ivmute.setxxx 这样调用呢
    debuggeeker
        5
    debuggeeker  
       2022-03-22 09:03:28 +08:00
    在编译文件中找到布局名字 ViewBinding 的 class 这个类,看看里面的逻辑你就会明白了。
    先说结论,我看过。生成的 xxxxViewBinding 这个类的代码做的事情,就是 findViewById 。
    iovekkk
        6
    iovekkk  
       2022-03-22 09:31:50 +08:00
    既然使用 viewbinding 了,点击事件就设置在 xml 文件中吧
    android:OnClick="@{() -> click.onclick()}"
    在 activity 中,创建一个 click 类,写一个成员方法即可
    tuimaochang
        7
    tuimaochang  
       2022-03-25 11:33:35 +08:00
    以前 kotlin 还能用 kotlin-android-extensions 来实现直接用 xml 里的 id ,后来升级这玩意就废了……
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3943 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 51ms UTC 05:12 PVG 13:12 LAX 21:12 JFK 00:12
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86