लेआउट व्यू को आर्किटेक्चर कॉम्पोनेंट से बाइंड करें

AndroidX लाइब्रेरी में आर्किटेक्चर कॉम्पोनेंट शामिल हैं. इनका इस्तेमाल करके, मज़बूत, टेस्ट किए जा सकने वाले, और आसानी से मैनेज किए जा सकने वाले ऐप्लिकेशन डिज़ाइन किए जा सकते हैं. डेटा बाइंडिंग लाइब्रेरी, आर्किटेक्चर कॉम्पोनेंट के साथ आसानी से काम करती है. इससे आपके यूज़र इंटरफ़ेस (यूआई) को डेवलप करने की प्रोसेस और आसान हो जाती है. आपके ऐप्लिकेशन के लेआउट, आर्किटेक्चर कॉम्पोनेंट के डेटा से बाइंड हो सकते हैं. इससे आपको यूज़र इंटरफ़ेस (यूआई) कंट्रोलर की लाइफ़साइकल को मैनेज करने और डेटा में होने वाले बदलावों के बारे में यूज़र इंटरफ़ेस (यूआई) को सूचना देने में मदद मिलती है.

इस दस्तावेज़ में, अपने ऐप्लिकेशन में आर्किटेक्चर कॉम्पोनेंट को शामिल करने का तरीका बताया गया है. इससे आपको डेटा बाइंडिंग लाइब्रेरी का ज़्यादा से ज़्यादा फ़ायदा मिलेगा.

डेटा में हुए बदलावों के बारे में यूज़र इंटरफ़ेस (यूआई) को सूचना देने के लिए, LiveData का इस्तेमाल करना

डेटा बाइंडिंग सोर्स के तौर पर LiveData ऑब्जेक्ट का इस्तेमाल किया जा सकता है, ताकि डेटा में होने वाले बदलावों के बारे में यूज़र इंटरफ़ेस (यूआई) को अपने-आप सूचना मिल सके. इस आर्किटेक्चर कॉम्पोनेंट के बारे में ज़्यादा जानने के लिए, LiveData की खास जानकारी देखें.

Observable को लागू करने वाले ऑब्जेक्ट, जैसे कि ऑब्ज़र्वेबल फ़ील्ड के उलट, LiveData ऑब्जेक्ट को डेटा में होने वाले बदलावों की सदस्यता लेने वाले ऑब्ज़र्वर की लाइफ़साइकल के बारे में पता होता है. इस जानकारी से कई फ़ायदे मिलते हैं. इनके बारे में 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 का इस्तेमाल करना

डेटा बाइंडिंग लाइब्रेरी, ViewModel कॉम्पोनेंट के साथ आसानी से काम करती है. ViewModel उस डेटा को दिखाता है जिसे लेआउट मॉनिटर करता है और उसमें होने वाले बदलावों के हिसाब से काम करता है. डेटा बाइंडिंग लाइब्रेरी के साथ ViewModel कॉम्पोनेंट का इस्तेमाल करने पर, यूज़र इंटरफ़ेस (यूआई) लॉजिक को लेआउट से कॉम्पोनेंट में ले जाया जा सकता है. इन्हें टेस्ट करना आसान होता है. डेटा बाइंडिंग लाइब्रेरी यह पक्का करती है कि ज़रूरत पड़ने पर, व्यू को डेटा सोर्स से बाइंड और अनबाइंड किया जाए. बाकी बचे ज़्यादातर काम में यह पक्का करना शामिल है कि सही डेटा दिखाया जा रहा है. इस आर्किटेक्चर कॉम्पोनेंट के बारे में ज़्यादा जानने के लिए, 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()}" />

बाइंडिंग अडैप्टर पर ज़्यादा कंट्रोल पाने के लिए, Observable ViewModel का इस्तेमाल करना

Observable इंटरफ़ेस को लागू करने वाले ViewModel कॉम्पोनेंट का इस्तेमाल करके, डेटा में हुए बदलावों के बारे में ऐप्लिकेशन के अन्य कॉम्पोनेंट को सूचना दी जा सकती है. यह ठीक उसी तरह काम करता है जिस तरह LiveData ऑब्जेक्ट का इस्तेमाल किया जाता है.

कुछ मामलों में, LiveData ऑब्जेक्ट के बजाय Observable इंटरफ़ेस को लागू करने वाले ViewModel कॉम्पोनेंट का इस्तेमाल करना बेहतर हो सकता है. भले ही, आपको LiveData की लाइफ़साइकल मैनेजमेंट की सुविधाएं न मिलें. Observable को लागू करने वाले ViewModel कॉम्पोनेंट का इस्तेमाल करने से, आपको अपने ऐप्लिकेशन में बाइंडिंग अडैप्टर पर ज़्यादा कंट्रोल मिलता है. उदाहरण के लिए, इस पैटर्न से आपको डेटा में बदलाव होने पर सूचनाओं पर ज़्यादा कंट्रोल मिलता है. साथ ही, इससे आपको टू-वे डेटा बाइंडिंग में किसी एट्रिब्यूट की वैल्यू सेट करने के लिए, कस्टम तरीका तय करने की सुविधा भी मिलती है.

ऑब्ज़र्व किए जा सकने वाले 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);
    }
}

अन्य संसाधन

डेटा बाइंडिंग के बारे में ज़्यादा जानने के लिए, यहां दिए गए अन्य संसाधन देखें.