flowable 绑定视图_android视图绑定入门

flowable 绑定视图

View Binding is one of the many new architecture components introduced by android for a better and faster development experience. In simple terms, it lets you use your views defined in your xml files in your activities/fragments without creating many objects of many different view types.

View Binding是android引入的许多新架构组件之一,以提供更好,更快的开发体验。 简单来说,它使您可以在活动/片段中使用在xml文件中定义的视图,而无需创建许多不同视图类型的对象。

Let’s take an example xml layout: activity_main.xml

让我们以XML布局示例为例: activity_main.xml

Normally, if we have to use a view in our activity we do something like this:

通常,如果我们必须在活动中使用视图,则可以执行以下操作:

public class MainActivity extends AppCompatActivity{


    LinearLayout titles;
    ImageView imageTitles;
    TextView textTitels;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        titles = findViewById(R.id.titles);
        textTitels = findViewById(R.id.text_titles);
        imageTitles = findViewById(R.id.image_titles);
    }
}

Creating objects of specific view types and then initializing them using findViewById() method. This is time consuming and makes up a lot of the boilerplate code.

创建特定视图类型的对象,然后使用findViewById()方法对其进行初始化。 这很耗时,并且构成许多样板代码。

View Binding is the solution we all have been looking for a while. With ViewBinding, you don’t have to create new objects for different view types of initialize them for that matter. All of this will be done for you. Let’s see how.

视图绑定是我们大家一直在寻找的解决方案。 使用ViewBinding,您不必为不同的视图类型创建新的对象,也不必为此初始化它们。 所有这些都将为您完成。 让我们看看如何。

  • First step is enabling view binding. For this, put the following line in your app’s build.gradle file:

    第一步是启用视图绑定。 为此, 将以下行放入应用程序的build.gradle文件中:

android {
...
viewBinding {
enabled = true
}
...
}

Note: View Binding is available in Android Studio 3.6

注意:Android Studio 3.6中提供了“视图绑定”

That’s actually it!

就是这样!

  • Now in your Activity:

    现在在您的活动中:
public class MainActivity extends BaseActivity{


private ActivityMainBinding binding;


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


   binding = ActivityMainBinding.inflate(getLayoutInflater());
   View view = binding.getRoot();
   setContentView(view);


    binding.titles.setOnClickListener(...); //The LinearLayout view
    binding.textTitles.setText("New Titles title") // TextView view
    binding.imageTitles.setImageDrawable(...) // ImageView view
}


...
}
  • For using View Binding in fragments:

    在片段中使用视图绑定:
public class MyFragment extends BaseFragment{
    
    private MyFragmentBinding binding;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


       binding = MyFragmentBinding.inflate(inflater, container, false);
       View view = binding.getRoot();


        binding.titles.setOnClickListener(...); //The LinearLayout view
        binding.textTitles.setText("New Titles title") // TextView view
        binding.imageTitles.setImageDrawable(...) // ImageView view


        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    ...
}

The respective id you give to your view in your xml view will become the member name by which you can access your views from your activities/fragments.

您在xml视图中为视图提供的相应ID将成为成员名称,您可以通过该成员名称从活动/片段中访问视图。

Visit 22Boxes.com for more Mobile & Web development resources.

访问22Boxes.com,以获取更多移动和Web开发资源。

翻译自: https://medium.com/swlh/getting-started-with-android-view-binding-416a46f52847

flowable 绑定视图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值