是时候忘记 findViewById 了

作者: Hevin丶 | 来源:发表于2016-06-27 23:05 被阅读4431次

在 Android 开发中 findViewById 相信是大家写得最多的方法之一。一个稍微复杂点的界面就需要写一大段的 findViewById,相当的浪费时间。之前已经有了很多的工具来帮助我们,但是现在我们终于迎来了官方支持的方法了。

首先,在你应用的 build.gradle 中添加:

android {
  ...
  dataBinding.enabled = ture
}

之后,在你的界面文件中,用 <layout></layout> 把已有的布局包裹起来。就像下面这样:

<?xml version="1.0" encoding="utf-8"?>
<layout    
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">     
     <android.support.v4.widget.DrawerLayout
          android:id="@+id/drawer_layout"        
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"        
          tools:openDrawer="start">
   
        <TextView           
            android:id="@+id/tv_hello"            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </android.support.v4.widget.DrawerLayout>
</layout>

然后就可以在 Activity 中用下面的方式来绑定数据了:

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.tvHello.setText("Hello World");

其中的 ActivityMainBinding,是根据具体的 layout 文件名来决定的。之后就能够使用 binding 和对应组件的 id 来进行一系列的操作啦。在这里需要注意资源文件和代码中名称的对应关系,可以看到 TextView 的 id 为 tv_hello,代码中就相应变为 tvHello;布局文件的名称为 activity_main,对应 Binding 的名称就为 ActivityMainBinding。

当使用 RecyclerView,ViewPager 等不是调用 setContentView 的控件时,可以用下面的方法:

ActivityMainBinding binding = DataBindingUtil.inflate(
  getLayoutInflater(), container, attchToContainer);

当需要将渲染的视图添加到其他 ViewGroup 中时,可以用 getRoot() 来得到根视图:

linearLayout.addView(binding.getRoot());

是不是感觉很方便?但更好的是这并没有用到反射或任何相对复杂的技术,在不影响性能的情况下,可以告别那麻烦又冗长的 findViewById 了。

当然 Android Data Binding 还有很多进阶用法,大家可以参考这里:
https://developer.android.com/topic/libraries/data-binding/index.html

后续,我也会继续写这方面的内容,在这里就权当抛砖迎玉吧。: )

相关文章

  • 是时候忘记 findViewById 了

    在 Android 开发中 findViewById 相信是大家写得最多的方法之一。一个稍微复杂点的界面就需要写一...

  • 2016-06-28 阅读整理

    Android 是时候忘记 findViewById 了 -- DataBingdingAndroid实战之自定义...

  • 是时候忘记你了!

    回忆只会让你我停滞不前,忘记过去,追求更美得未来。 我不喜欢去回忆过去,我怕回忆,因为回忆实在是太美好了,美好的让...

  • 是时候该忘记你了

    认识他的时候,刚好有个客人投诉我。 那天那位客人喝多了,没带身份证,非要我给他开个房间,按照规定没有身份证就...

  • 我想,是时候忘记你了

    三年来,你杳无音信 我行走在川流的人群中 像一辆破旧的二蛋,满心惶急 却不敢鸣笛 晚安 上海又下雨了,绍兴的樱花又...

  • findViewById的另类效率写法

    在Android中最常见的代码,莫过于findViewById了,通过findViewById能够直接引用资源文件...

  • findViewById()

    欢迎访问[Android日记][1],如有转载请注明Android日记 http://androiddiary.s...

  • findViewById

    findViewById分为两种,1.acivity中的findViewById 调用Window中的findVi...

  • OnClickMe一款自动生成OnClick代码的插件

    1. 使用背景 对于findViewById和OnClick,大部分时候, 我们使用ButterKnife就可以了...

  • 关于findViewByid的简写

    关于findViewByid findViewById只是查找到对象的引用,不应该叫做控件的实例化,实例化是创建出...

网友评论

  • As_Javen:不错,学习了。
  • BKQ_SYC:看着挺好的,http://blog.csdn.net/sinat_17314503/article/details/51711319这里写的好全我要用的很溜估计要好久
  • 醉酒肆之:明天试试,不行就来打你😁
  • 0f7d096211a6:我是通过读布局文件直接生成对应HelloWorldBinding ,原始的写法,但是不用手写findviewById
  • reading_man:我还是想说,谁知道他背后的实现不是用的反射呢?有人看源码了吗?
  • leisurehuang:用Butterknife也会很方便吧?
    满月写:@Hevin丶 这是DataBinding,减少findViewById只是他的一个副作用,主要用来控件和数据绑定,不要本末倒置
    Hevin丶:@leisurehuang 嗯,只是说这个是 Google 官方支持的。
  • eoeoops:HelloWorldBinding binding = HelloWorldBinding.inflate(
    getLayoutInflater(), container, attachToContainer);
    好像不对,应该是:
    HelloWorldBinding binding = DataBindingUtil.inflate(
    getLayoutInflater(), container, attachToContainer);
    Hevin丶:@刘豆 嗯,是的,已修改。: )
  • xiaolei123:and why ? why can do this?
  • JTDQGS:试试

本文标题:是时候忘记 findViewById 了

本文链接:https://www.haomeiwen.com/subject/mdezdttx.html