Android development Tutorial-Using Data Binding (iv) in fragment

Source: Internet
Author: User

Modify the layout of a fragment

Same as in the use of activity, the use of data binding in fragment also needs to modify the layout, the same way as the activity, the outermost add <layout> tags:

<?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">    <data>    </data>    <页面布局.../></layout>

Similarly, in order to avoid repetition, the data binding in <data> is covered in this article.

Binding in Fragment

There are some differences in how bindings are created in activity, but the purpose is to obtain a reference to the bound object.

For example, our fragment layout file is: Frag_main.xml, the specific way is as follows:

    1. Defining member variables
private FragMainBinding mBinding;
    1. Initialize the mbinding in Oncreateview () and return to the view
@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {    mBinding = FragMainBinding.inflate(inflater);    mBinding.tvExample.setText("Binding Text");    return mBinding.getRoot();}

At this point, the binding object can be operated normally.

Summarize

Similar to getting the data binding object in activity, the method is slightly different.

In addition to using data binding in activity and fragment, another common scenario is to use data binding in the adapter of a list, as we'll talk about.

In the next article, we'll start by explaining how <data> tags work in layouts, that is, how to bind data to a layout file.

Android development Tutorial-Using Data Binding (iv) in fragment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.