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

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

    init {
        val result = Repository.userName
        userName = Transformations.map(result) { result -> result.value }
    }
}

Java

class ScheduleViewModel extends ViewModel {
    LiveData username;

    public ScheduleViewModel() {
        String result = Repository.userName;
        userName = Transformations.map(result, result -> result.value);
    }
}

यूज़र इंटरफ़ेस (यूआई) से जुड़े डेटा को मैनेज करने के लिए, ViewModel का इस्तेमाल करना

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

डेटा बाइंडिंग लाइब्रेरी के साथ 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 ऑब्जेक्ट.

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

मॉनिटर किए जा सकने वाले 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);
    }
}

अन्य संसाधन

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