ไลบรารี AndroidX มีคอมโพเนนต์สถาปัตยกรรม ซึ่งคุณสามารถใช้เพื่อออกแบบแอปที่มีประสิทธิภาพ ทดสอบได้ และบำรุงรักษาได้ ไลบรารีการเชื่อมโยงข้อมูลทำงานร่วมกับคอมโพเนนต์สถาปัตยกรรมได้อย่างราบรื่นเพื่อลดความซับซ้อนในการพัฒนา UI ของคุณให้มากยิ่งขึ้น เลย์เอาต์ในแอปสามารถเชื่อมโยงกับข้อมูลในคอมโพเนนต์สถาปัตยกรรม ซึ่งช่วยให้คุณจัดการวงจรของตัวควบคุม UI และแจ้งเตือน UI เกี่ยวกับการเปลี่ยนแปลงในข้อมูลได้
เอกสารนี้แสดงวิธีรวมคอมโพเนนต์สถาปัตยกรรมเข้ากับแอปเพื่อให้ได้ประโยชน์สูงสุดจากการใช้ไลบรารีการเชื่อมโยงข้อมูล
ใช้ LiveData เพื่อแจ้งเตือน UI เกี่ยวกับการเปลี่ยนแปลงข้อมูล
คุณสามารถใช้ออบเจ็กต์ LiveData เป็นแหล่งที่มาของการเชื่อมโยงข้อมูลเพื่อแจ้งเตือน UI เกี่ยวกับการเปลี่ยนแปลงในข้อมูลโดยอัตโนมัติ ดูข้อมูลเพิ่มเติมเกี่ยวกับคอมโพเนนต์สถาปัตยกรรมนี้ได้ที่ภาพรวม LiveData
วัตถุ LiveData จะทราบเกี่ยวกับวงจรของผู้สังเกตการณ์ที่สมัครรับข้อมูลการเปลี่ยนแปลง ซึ่งต่างจากวัตถุที่ใช้ Observable เช่น ช่องที่สังเกตได้ ความรู้นี้มีประโยชน์มากมาย ซึ่งอธิบายไว้ในข้อดีของการใช้ LiveData ใน Android Studio เวอร์ชัน 3.1 ขึ้นไป คุณสามารถแทนที่ช่องที่สังเกตได้ด้วยออบเจ็กต์ LiveData ในโค้ดการเชื่อมโยงข้อมูล
หากต้องการใช้ออบเจ็กต์ LiveData กับคลาสการเชื่อมโยง คุณต้องระบุเจ้าของวงจรเพื่อกำหนดขอบเขตของออบเจ็กต์ LiveData ตัวอย่างต่อไปนี้ระบุกิจกรรมเป็นเจ้าของวงจรหลังจากสร้างอินสแตนซ์คลาสการเชื่อมโยงแล้ว
Kotlin
class ViewModelActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Inflate view and obtain an instance of the binding class.
val binding: UserBinding = DataBindingUtil.setContentView(this, R.layout.user)
// Specify the current activity as the lifecycle owner.
binding.setLifecycleOwner(this)
}
}
Java
class ViewModelActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Inflate view and obtain an instance of the binding class.
UserBinding binding = DataBindingUtil.setContentView(this, R.layout.user);
// Specify the current activity as the lifecycle owner.
binding.setLifecycleOwner(this);
}
}
คุณสามารถใช้คอมโพเนนต์ ViewModel ตามที่อธิบายไว้ในส่วนต่อไปนี้เพื่อเชื่อมโยงข้อมูลกับเลย์เอาต์ ในคอมโพเนนต์ ViewModel คุณสามารถใช้ออบเจ็กต์ LiveData เพื่อเปลี่ยนรูปแบบข้อมูลหรือผสานแหล่งข้อมูลหลายแหล่งได้ ตัวอย่างต่อไปนี้แสดงวิธีเปลี่ยนรูปแบบข้อมูลใน ViewModel
Kotlin
class ScheduleViewModel : ViewModel() {
val userName: LiveData<String>
init {
val result = Repository.userName
userName = Transformations.map(result) { result -> result.value }
}
}
Java
class ScheduleViewModel extends ViewModel {
LiveData<String> username;
public ScheduleViewModel() {
String result = Repository.userName;
userName = Transformations.map(result, result -> result.value);
}
}
ใช้ ViewModel เพื่อจัดการข้อมูลที่เกี่ยวข้องกับ UI
ไลบรารีการเชื่อมโยงข้อมูลทำงานร่วมกับคอมโพเนนต์ ViewModel ได้อย่างราบรื่น ViewModel จะแสดงข้อมูลที่เลย์เอาต์สังเกตและตอบสนองต่อการเปลี่ยนแปลง
การใช้คอมโพเนนต์ ViewModel กับไลบรารีการเชื่อมโยงข้อมูลช่วยให้คุณย้ายตรรกะ UI ออกจากเลย์เอาต์และเข้าไปในคอมโพเนนต์ ซึ่งจะทดสอบได้ง่ายขึ้น ไลบรารีการเชื่อมโยงข้อมูลช่วยให้มั่นใจได้ว่ามุมมองจะเชื่อมโยงและยกเลิกการเชื่อมโยงจากแหล่งข้อมูลเมื่อจำเป็น งานที่เหลือส่วนใหญ่คือการตรวจสอบว่าคุณแสดงข้อมูลที่ถูกต้อง ดูข้อมูลเพิ่มเติมเกี่ยวกับคอมโพเนนต์สถาปัตยกรรมนี้ได้ที่ภาพรวม ViewModel
หากต้องการใช้คอมโพเนนต์ ViewModel กับไลบรารีการเชื่อมโยงข้อมูล คุณต้องสร้างอินสแตนซ์คอมโพเนนต์ซึ่งรับค่ามาจากคลาส ViewModel รับอินสแตนซ์ของคลาสการเชื่อมโยง และกำหนดคอมโพเนนต์ ViewModel ให้กับพร็อพเพอร์ตี้ในคลาสการเชื่อมโยง ตัวอย่างต่อไปนี้แสดงวิธีใช้คอมโพเนนต์กับไลบรารี
Kotlin
class ViewModelActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Obtain the ViewModel component.
val userModel: UserModel by viewModels()
// Inflate view and obtain an instance of the binding class.
val binding: UserBinding = DataBindingUtil.setContentView(this, R.layout.user)
// Assign the component to a property in the binding class.
binding.viewmodel = userModel
}
}
Java
class ViewModelActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Obtain the ViewModel component.
UserModel userModel = new ViewModelProvider(this).get(UserModel.class);
// Inflate view and obtain an instance of the binding class.
UserBinding binding = DataBindingUtil.setContentView(this, R.layout.user);
// Assign the component to a property in the binding class.
binding.viewmodel = userModel;
}
}
ในเลย์เอาต์ ให้กำหนดพร็อพเพอร์ตี้และเมธอดของคอมโพเนนต์ ViewModel ให้กับมุมมองที่เกี่ยวข้องโดยใช้นิพจน์การเชื่อมโยง ดังที่แสดงในตัวอย่างต่อไปนี้
<CheckBox
android:id="@+id/rememberMeCheckBox"
android:checked="@{viewmodel.rememberMe}"
android:onCheckedChanged="@{() -> viewmodel.rememberMeChanged()}" />
ใช้ ViewModel ที่สังเกตได้เพื่อควบคุมอะแดปเตอร์การเชื่อมโยงได้มากขึ้น
คุณสามารถใช้คอมโพเนนต์ ViewModel ที่ใช้อินเทอร์เฟซ Observable เพื่อแจ้งคอมโพเนนต์อื่นๆ ของแอปเกี่ยวกับการเปลี่ยนแปลงในข้อมูลได้ คล้ายกับวิธีที่คุณใช้ออบเจ็กต์ LiveData
ในบางสถานการณ์ คุณอาจต้องการใช้คอมโพเนนต์ ViewModel ที่ใช้อินเทอร์เฟซ Observable มากกว่าการใช้ออบเจ็กต์ LiveData แม้ว่าคุณจะสูญเสียความสามารถในการจัดการวงจรของ LiveData ไปก็ตาม การใช้คอมโพเนนต์ ViewModel ที่ใช้ Observable ช่วยให้คุณควบคุมอะแดปเตอร์การเชื่อมโยงในแอปได้มากขึ้น ตัวอย่างเช่น รูปแบบนี้ช่วยให้คุณควบคุมการแจ้งเตือนได้มากขึ้นเมื่อข้อมูลมีการเปลี่ยนแปลง และยังช่วยให้คุณระบุเมธอดที่กำหนดเองเพื่อตั้งค่าแอตทริบิวต์ในการเชื่อมโยงข้อมูลแบบ 2 ทางได้ด้วย
หากต้องการใช้คอมโพเนนต์ ViewModel ที่สังเกตได้ คุณต้องสร้างคลาสที่รับค่ามาจากคลาส ViewModel และใช้อินเทอร์เฟซ Observable คุณสามารถระบุตรรกะที่กำหนดเองเมื่อผู้สังเกตการณ์สมัครรับหรือยกเลิกการสมัครรับการแจ้งเตือนโดยใช้เมธอด addOnPropertyChangedCallback() และ removeOnPropertyChangedCallback() นอกจากนี้ คุณยังระบุตรรกะที่กำหนดเองซึ่งจะทำงานเมื่อพร็อพเพอร์ตี้เปลี่ยนแปลงในเมธอด notifyPropertyChanged() ได้ด้วย ตัวอย่างโค้ดต่อไปนี้แสดงวิธีใช้ ViewModel ที่สังเกตได้
Kotlin
/**
* A ViewModel that is also an Observable,
* to be used with the Data Binding Library.
*/
open class ObservableViewModel : ViewModel(), Observable {
private val callbacks: PropertyChangeRegistry = PropertyChangeRegistry()
override fun addOnPropertyChangedCallback(
callback: Observable.OnPropertyChangedCallback) {
callbacks.add(callback)
}
override fun removeOnPropertyChangedCallback(
callback: Observable.OnPropertyChangedCallback) {
callbacks.remove(callback)
}
/**
* Notifies observers that all properties of this instance have changed.
*/
fun notifyChange() {
callbacks.notifyCallbacks(this, 0, null)
}
/**
* Notifies observers that a specific property has changed. The getter for the
* property that changes must be marked with the @Bindable annotation to
* generate a field in the BR class to be used as the fieldId parameter.
*
* @param fieldId The generated BR id for the Bindable field.
*/
fun notifyPropertyChanged(fieldId: Int) {
callbacks.notifyCallbacks(this, fieldId, null)
}
}
Java
/**
* A ViewModel that is also an Observable,
* to be used with the Data Binding Library.
*/
class ObservableViewModel extends ViewModel implements Observable {
private PropertyChangeRegistry callbacks = new PropertyChangeRegistry();
@Override
protected void addOnPropertyChangedCallback(
Observable.OnPropertyChangedCallback callback) {
callbacks.add(callback);
}
@Override
protected void removeOnPropertyChangedCallback(
Observable.OnPropertyChangedCallback callback) {
callbacks.remove(callback);
}
/**
* Notifies observers that all properties of this instance have changed.
*/
void notifyChange() {
callbacks.notifyCallbacks(this, 0, null);
}
/**
* Notifies observers that a specific property has changed. The getter for the
* property that changes must be marked with the @Bindable annotation to
* generate a field in the BR class to be used as the fieldId parameter.
*
* @param fieldId The generated BR id for the Bindable field.
*/
void notifyPropertyChanged(int fieldId) {
callbacks.notifyCallbacks(this, fieldId, null);
}
}
แหล่งข้อมูลเพิ่มเติม
ดูข้อมูลเพิ่มเติมเกี่ยวกับการเชื่อมโยงข้อมูลได้ที่แหล่งข้อมูลเพิ่มเติมต่อไปนี้
แนะนำสำหรับคุณ
- หมายเหตุ: ข้อความลิงก์จะแสดงเมื่อ JavaScript ปิดอยู่
- ทำงานกับออบเจ็กต์ข้อมูลที่ได้รับอนุญาตให้สังเกตพฤติกรรมผู้ใช้ได้
- โหลดและแสดงข้อมูลแบบแบ่งหน้า
- ใช้โครูทีน Kotlin กับคอมโพเนนต์ที่รับรู้ถึงวงจร