为什么我推荐你用ViewBinding 替换findViewById?

本文探讨了为何推荐使用ViewBinding而非findViewById或ButterKnife。ViewBinding提供了更简洁的代码,减少了手动查找视图的步骤,同时具备类型安全和null安全特性。在Activity和Fragment中集成ViewBinding的方法也被详细说明,其优点包括简化代码、提高编译速度和增强安全性。然而,它不支持布局变量和双向数据绑定。此外,文章还分享了Android程序员的进阶资源和大厂内部调优方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为什么推荐你使用ViewBinding 替换findViewById 和 ButterKnife ? 因为太爽了,太上头了。 用过一次就爱上了,再也不想回去。真心的。不信你看下文!

定义

ViewBinding 是google推出Jetpack库的一个组件,主要用于视图绑定,替代 findViewById操作.Viewbinding会根据xml文件生成一个对应的绑定类, 比如我们xml文件是: activity_login_layout.xml 生成的绑定类就是ActivityLoginLayoutBinding 这么一个类.在使用的时候直接通过生成的绑定类调用我们xml中的视图组件, 不用findViewById,也不用声明组件. 接下来看下我们集成和项目就能很快的理解.

集成

首先在我们工程build.gradld中引入viewBind

    //引入ViewBinding
    viewBinding {
        viewBinding = true
    }

然后我们就可以在代码中使用ViewBinding了

代码

创建了一个登录页面, activity_login_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    tools:viewBindingIgnore="false"
    android:padding="16dp">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tv_login_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="登录"
        android:textColor="@color/design_default_color_primary_variant"
        android:textSize="19sp" />

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/et_login_account"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:hint="用户名"
        android:paddingStart="10dp"
        android:paddingEnd="10dp" />

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/et_login_pwd"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:hint="密码"
        android:paddingStart="10dp"
        android:paddingEnd="10dp" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:background="@color/design_default_color_secondary"
        android:text="登录"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tv_find_pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:text="找回密码"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="16sp" />
</LinearLayout>

在Fragment中使用ViewBinding绑定第二个界面(通常是指启动另一个Activity或Fragment),需要遵循以下步骤,这里以Java为例: 1. 首先,在对应的模块(例如AppCompatActivity或者其他支持Fragment的Activity)上添加ViewBinding依赖,如果还没有的话。在build.gradle文件中添加: ```groovy dependencies { implementation 'androidx.viewbinding:viewbinding-rxjava2:2.0.0' } ``` 2. 创建一个ViewBinding类。假设你要绑定的是SecondActivity的布局(second_activity.xml),在对应的包下创建一个名为`SecondActivityBinding`的类: ```java public class SecondActivityBinding extends ActivityBinding { public final TextView textView; // 替换为你需要的视图 public SecondActivityBinding(@NonNull LayoutInflater inflater) { super(inflater, R.layout.second_activity); textView = findViewById(R.id.your_text_view_id); // 替换为你的文本视图ID } } ``` 3. 在需要打开第二个界面的地方,比如Fragment的onCreateView()方法中,获取并设置数据: ```java @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 获取当前Activity实例 AppCompatActivity activity = (AppCompatActivity) getActivity(); // 初始化ViewBinding对象 SecondActivityBinding binding = DataBindingUtil.setContentView(activity, R.layout.second_activity); // 设置数据到TextView或其他视图 binding.textView.setText("Hello from Fragment"); // 示例文字 // 如果需要,可以设置其他属性或监听事件 // 使用startActivityForResult()或startActivity()打开新的Activity Intent intent = new Intent(activity, SecondActivity.class); // ...其他启动逻辑... return null; } ``` 4. 对于返回结果或处理逻辑,记得在SecondActivity关闭后回调到原来的Fragment。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值