美文网首页
Android-ViewBinding入门

Android-ViewBinding入门

作者: GCZeng | 来源:发表于2020-03-16 18:47 被阅读0次

前段时间Google发现AndroidStudio3.6.1正式版,这次更新带来了很多新特性,其中一个值得学习的就是ViewBinding

什么是ViewBinding

In most cases, view binding replaces findViewById.

引用官方的说法是,在大部情况下,使用ViewBinding替换findViewById。以前如果不使用第三方框架, 不管怎样封装,使用起来还是很不方便(比如找不到控件、控件ID冲突等问题);

怎样使用ViewBinding

使用条件:
1. Android Studio 3.6 Canary 11+;
2. Gradle插件3.6.1+;
准备好了吗???

在Activity在使用ViewBinding

  1. 模块的build.xml中添加ViewBinding的支持;
android {
    ...
    viewBinding {
        enabled = true
    }
}

在项目中开启了ViewBinding后,就会为每一个布局文件生成一对应的绑定文件,如果不需要生成绑定文件,只需要在根布局添加:tools:viewBindingIgnore="true"

<LinearLayout
        ...
        tools:viewBindingIgnore="true" >
    ...
</LinearLayout>
  1. 添加一个布局文件activity_main.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
  1. 添加一个ActivityMainActivity.java,代码如下
public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding viewBinding;//ActivityMainBinding就是上面activity_main.xml生成的绑定文件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //以为是这样绑定布局文件的
//        setContentView(R.layout.activity_main);

        //现在是这样绑定布局文件的
        viewBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(viewBinding.getRoot());
        //在这里设置一个显示文件
        viewBinding.tvContent.setText("这里是通过ViewBinding设置的文本");
    }
}

使用起来还是比较简单的,运行一下程序看看效果:


image.png

在Fragment在使用ViewBinding

  1. 先添加一个布局文件fragment_view_binding.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="Hello World!"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
  1. 添加一个FragmentViewBindingFragment,代码如下:
public class ViewBindingFragment extends Fragment {
    private FragmentViewBindingBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentViewBindingBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        binding.tvContent.setText("这里是在Fragment通过ViewBinding设置的文本");
        return view;
    }


    public static ViewBindingFragment newInstance() {
        Bundle args = new Bundle();
        ViewBindingFragment fragment = new ViewBindingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
  1. 把上面的activity_main.xml,修改一下,用来添加Fragment,修改后代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="Hello World!"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@id/f_layout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="1" />

    <View
        android:id="@+id/v_line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintBottom_toTopOf="@+id/f_layout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_content" />

    <FrameLayout
        android:id="@+id/f_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_content"
        app:layout_constraintVertical_weight="1" />

</androidx.constraintlayout.widget.ConstraintLayout>
  1. 同时把上面的Activity也修改一下,修改后代码如下:
public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding viewBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        setContentView(R.layout.activity_main);

        viewBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(viewBinding.getRoot());
        viewBinding.tvContent.setText("这里是通过ViewBinding设置的文本");

        //添加fragment
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction tx = fm.beginTransaction();
        tx.replace(R.id.f_layout, ViewBindingFragment.newInstance());
        tx.commit();
    }
}

运行一下程序看看效果:


image.png

Fragment中使用也是比较简单的,如注意的时,在Fragment中使用ViewBinding时,要在onDestroyView()方法中把binding的引用移除,不然会引起内存泄漏

总结:

  1. 使用起来比较方便,编译比较快,而且是官方推荐的;
  2. 目前还不支持布局变量和布局表达式;
  3. 不支持双向绑定,所以只适合一些简单的使用场境;

相关文章

网友评论

      本文标题:Android-ViewBinding入门

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