Android: use of data binding (2)

Share some common basics of Data Binding in the project, click events and input box operations.

DataBinding allows us to fill in DataBinding expressions in some event properties of view (such as onClick, etc.) in xml, and it can also be implemented by binding listener.

 

First, write a class to represent the click event

 public class Presenter{public void onClick(View view){
            Toast.makeText(MainActivity.this,"123",Toast.LENGTH_LONG).show();
        }

        public void onClickDataBinding(Employee employee){
            Toast.makeText(MainActivity.this,employee.getLastName(),Toast.LENGTH_LONG).show();
        }
    }

For layout files: (this kind of Presenter is added to data)

  <data>
     
        <variable
            name="presenter"
            type="com.zhangqie.databinding.demo1.MainActivity.Presenter"/>
    </data>

Event code:

   <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Click events"
       android:onClick="@{presenter::onClick}"
        />
        <!-- android:layout_margin="@{@dimen/n1 + @dimen/n2}"-->
    <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Click event transfer"
       android:onClick="@{() -> presenter.onClickDataBinding(employee)}"
      />

In this way, our event binding is completed, and the running effect is the same as usual.

 

Next, enter the auto refresh data in the input box EditText, ObservableArrayMap, ObservableArrayList

 

First, create a User class

public class User extends BaseObservable {

    public User(String frstName,String lastName){
        this.frstName = frstName;
        this.lastName = lastName;
        observableArrayMap.put("zq","zhhhhh");
        list.add("aaa");
        list.add("bbb");
    }

    private String frstName;

    private String lastName;

    public ObservableArrayMap<String,String> observableArrayMap = new ObservableArrayMap<>();

    public ObservableArrayList<String> list = new ObservableArrayList<>();

    @Bindable
    public String getFrstName() {
        return frstName;
    }


    public void setFrstName(String frstName) {
        this.frstName = frstName;
        //Specify refresh UI
        notifyPropertyChanged(com.zhangqie.databinding.BR.frstName);
    }

    @Bindable
    public String getLastName() {
        return lastName;
    }


    public void setLastName(String lastName) {
        this.lastName = lastName;
        //refresh all
        //notifyChange();
    }
}

You can see the refresh data operation, ObservableArrayMap, ObservableArrayList

Add another event listening class of input box

 public class Presenter{

        public void onTextChanged(CharSequence s, int start, int before, int count){
            user.setFrstName(s.toString());
        }

    }

Layout file: (data of ObservableArrayMap and ObservableArrayList are added to the layout)

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="user"
            type="com.zhangqie.databinding.demo2.User" />

        <variable
            name="presenter"
            type="com.zhangqie.databinding.demo2.Demo2.Presenter" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onTextChanged="@{presenter.onTextChanged}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.frstName}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text='@{user.observableArrayMap["zq"]}' />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text='@{user.list[0]}' />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.list[1]}" />
    </LinearLayout>

</layout>

Class Activity

public class Demo2 extends AppCompatActivity {


    User user = new User("Cut and speak","mis");

    DemoActivityBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        binding = DataBindingUtil.setContentView(this, R.layout.demo_activity);
        binding.setPresenter(new Presenter());
        binding.setUser(user);

    }

    //Input box monitoring
    public class Presenter{

        public void onTextChanged(CharSequence s, int start, int before, int count){
            user.setFrstName(s.toString());
        }
    }
}

When you input information, you can see that the first TextView is updated in real time.

 

Renderings: to be recorded

 

 

 

Source address: to be uploaded

Tags: Android xml encoding

Posted on Fri, 31 Jan 2020 16:13:15 -0500 by okok