current position:Home>Android DataBinding Jetpack component
Android DataBinding Jetpack component
2023-01-25 11:31:02【Someone from Yungu Li】
Android之DataBinding
DataBinding
数据绑定
亮点
- There is no need to hold a reference to the control during development
- Has the feature of two-way binding
- 数据与UI同步
使用DataBinding
1、gradle中添加dataBinding
android {
dataBinding {
enabled = true
}
}
2、绑定xml
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
.......................
}
单向绑定数据
定义数据模型
// The first way of one-way refresh
public class User extends BaseObservable {
private String name;
private String age;
public User(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
// 全量刷新
notifyChange();
}
@Bindable
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
// single refresh
notifyPropertyChanged(BR.age);
}
}
编写databingding样式的xml
<?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" xmlns:tools="http://schemas.android.com/tools">
<!--绑定数据-->
<data>
<variable name="user" type="com.example.databingding.User" />
</data>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="@{user.age}" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="@{user.name}" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="改变参数" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</layout>
Activity中使用ViewModel
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User("张三", "60");
mainBinding.setUser(user);
mainBinding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
user.setAge("30");
user.setName("李思");
}
});
}
}
There is another way of defining data,Binding data is the same as above
// The second way of one-way refresh
public class Bean {
public ObservableField<String> id = new ObservableField<>();
public ObservableField<String> name = new ObservableField<>();
}
双向绑定
只需要修改一下xml就可以,在@后面加上一个 = 符号
比如:
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@={user.name}" />
The complete example code is attached for referenceDataBinding例子代码
copyright notice
author[Someone from Yungu Li],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2023/025/202301251120395496.html
The sidebar is recommended
- The front end of actual combat: copy write millet's official website on the first day
- 1. Linux application programming and network programming---file IO notes in Linux system
- [Front-end notes - CSS] 10. Cascading and inheritance + selector
- 8. Linux application programming and network programming---Linux network programming notes
- [Java|golang] 1828. Count the number of points in a circle
- [Front-end notes——CSS] 11. Box model + background and border
- C + + large Numbers together, according to a combined
- 3. Linux application programming and network programming --- get system information notes
- The use and principle of Kafka message queue
- 5. Linux application programming and network programming---signal notes in Linux
guess what you like
6 · Linux application programming and system programming - senior IO notes
Java collection common interview questions (4)
072-JAVA project training: Imitation QQ instant messaging software series lecture 7 (explaining the realization of the chat interface and functions)
Coordination center performance comparison: how zookeeper solves the load balancing problem
070-JAVA project training: imitation QQ instant messaging software series lecture five (explain user registration function)
Ubuntu installation and configuration (brief)
073-JAVA project training: imitation QQ instant messaging software series lecture eight (explain query and add friend function)
SQL injection classification and error injection EXP
All basic commands in linux fail, showing that the command cannot be found
4. Linux application programming and network programming---Linux process full solution notes (difference between process and program)
Random recommended
- Linux system - basic IO
- Hanlp's understanding of user-defined dictionaries (java version)
- Brief description and configuration of Maven
- 071-JAVA project training: imitation QQ instant messaging software series lecture six (explaining the function of QQ main interface)
- 【Maximum LeetCode】January Algorithm Training (12) Linked List
- 【Max LeetCode】January Algorithm Training (13) Doubly Linked List
- [Big Data Management] Java implements Bloom filter
- [Maximum LeetCode] Algorithm training in January (14) stack
- [Machine Learning] Adaboost Integrated Algorithm
- [Big Data Management] Java implements cuckoo filter (CF)
- Chaozhou Xiangqiao: "Charming Ancient City, Cultural Sharing" Spring Festival Intangible Cultural Heritage Market Opens
- [Big data management] Java realizes the dictionary tree TireTree
- [Max LeetCode] January Algorithm Training (11) Matrix
- New Express (Web framework based on HTTP module encapsulation NodeJS)
- JavaScript error-prone questions (stack processing, call function, prototype chain questions)
- Space "travel", lion and crane dance, intangible cultural heritage experience...During the Spring Festival, Zhuhai Jinwan is so fun!
- Wine 8.0 official release: better support for running Windows applications on Linux and other systems
- Zhongke Sugon: Sugon's new computer "participates in" "The Wandering Earth 2"
- Linux actual combat notes finishing (1.24)
- Winter Vacation JavaScript Tutorial Chapter 1 Variable Promotion
- Automatically execute the specified sql when the springBoot project starts
- Winter vacation javascript cram school Chapter 2 let & const temporary dead zone
- Microsoft CEO Nadella: We firmly believe that the wave of artificial intelligence is unstoppable
- [Bi She] Style Migration Based on CycleGAN [3] Code Migration to Server (Linux) and Environment Construction
- Axiba HTML
- Explore the vachar, test, longtext storage limits of the database mysql
- Web front-end development technology: Web front-end development technology
- Python data structure implements various sorting algorithms (including algorithm introduction and stability, complexity summary)
- Web front-end development technology: Vue routing
- 【 MySQL 】 the seventh part of the process control function
- Web front-end development technology: Vuex state management
- Important concepts and installation of Linux + vim common operations
- [MySQL] Part 9 MySQL Information Functions
- [MySQL] Part VIII Encryption and Decryption Functions
- PAT 1014 Sherlock Holmes' appointment (C++ implementation)
- Android gradle custom plug-in implementation
- Python numpy ValueError: setting an array element with a sequence.
- On Spring notes (annotation-based development)
- Ubuntu18.04 install Hadoop
- Android proguard code obfuscation and decompilation tool