Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रणाली >> Android

Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण

डेटा बाइंडिंग एक तकनीक है जिसका उपयोग तब किया जाता है जब आप सूचना के टुकड़ों (आपके डेटा) को कुछ विज़ुअल उपयोगकर्ता इनपुट तत्वों से जोड़ना चाहते हैं। इस प्रक्रिया में, जब भी इनपुट अपडेट होता है, तो उसके पीछे का डेटा भी अपडेट हो जाता है।

यह कोई नई अवधारणा नहीं है, और ऐसे ढेर सारे फ्रेमवर्क हैं जिन्हें अपने डिज़ाइन में शामिल किया है (जैसे AngularJS/React/Vue)।

इस लेख में हमारा ध्यान फ्रंट-एंड फ्रेमवर्क पर नहीं बल्कि मोबाइल विकास पर है। Google ने Android में Data Binding Library पेश की, जो Android Jetpack का हिस्सा है।

यदि आप Jetpack लाइब्रेरी सूट से परिचित नहीं हैं, तो ऐसा इसलिए हो सकता है क्योंकि Google ने घोषणा की है कि वह अपने सपोर्ट लाइब्रेरी के लिए विकास छोड़ देगा। इसके बजाय, यह AndroidX लाइब्रेरी (जो सपोर्ट लाइब्रेरी के नए संस्करण हैं) का समर्थन करने की ओर बढ़ेगा।

मैं जानता हूं कि एडॉप्टर का उपयोग करके डेटा बाइंडिंग का उपयोग कैसे करें, इस बारे में कई लेख हैं, लेकिन यह लेख उस पर ध्यान केंद्रित नहीं करेगा। इसके बजाय, मैं डेटा बाइंडिंग के लिए एक न्यूनतम दृष्टिकोण दिखाऊंगा जो आपके द्वारा लिखे जाने वाले कोड की मात्रा को कम करके आपका समय बचा सकता है।

डेटा बाइंडिंग का उपयोग क्यों करें?

यदि आप अभी तक आश्वस्त नहीं हैं, तो आइए एक उदाहरण दिखाकर डेटा बाइंडिंग का उपयोग करने के लाभ को समझाने में कुछ मिनट लगाते हैं। मान लीजिए आपके पास तीन कस्टम बटन वाला एक मेनू है, जहां प्रत्येक बटन अपने आप में एक लेआउट है।

Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण
हमारा लेआउट

इस सब को उत्पन्न करने का एक तरीका चार अलग-अलग XML लेआउट का उपयोग करना है: एक मुख्य लेआउट के लिए और तीनों बटनों में से प्रत्येक के लिए एक।

आपको ऐसा करने की आवश्यकता है क्योंकि प्रत्येक बटन उपयोगकर्ता को आपके एप्लिकेशन के एक अलग हिस्से में निर्देशित करेगा और इसलिए इसे अलग टेक्स्ट और एक अलग छवि की आवश्यकता होती है।

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content"> <ImageView android:id="@+id/imageView" android:layout_height="100dp" android:layout_width="100dp" android:src="@drawable/image_name" android:adjustViewBounds="true" android:scaleType="centerInside" /> <TextView android:id="@+id/textView" android:gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Image Text" android:textSize="16sp" /> </LinearLayout>
एक बटन का लेआउट

यह मानते हुए, यह बहुत अधिक कोड डुप्लीकेशन नहीं है क्योंकि हम केवल तीन लेआउट संभाल रहे हैं। लेकिन अगर आप इसके बारे में सोचते हैं, तो यह उस सभी कोड से निपटने में समय की काफी बर्बादी है। यदि आप एक ऐसे एप्लिकेशन को ध्यान में रखते हैं जिसमें अधिक जटिल लेआउट है, जो उत्पादों और उनकी छवियों को प्रदर्शित कर सकता है, तो इससे बहुत थकाऊ कोड प्रतिकृति हो सकती है।

डेटा बाइंडिंग के साथ, हम केवल एक XML लेआउट बनाने का प्रबंधन करेंगे जिसका उपयोग हमारे सभी बटनों द्वारा किया जाएगा।

हम कहां से शुरू करें?

हमें अपने प्रोजेक्ट को डेटा बाइंडिंग सक्षम करने देना होगा। उसके लिए, हमें अपने एप्लिकेशन की build.gradle फ़ाइल में dataBinding तत्व जोड़ना होगा:

android { compileSdkVersion 29 buildToolsVersion "29.0.2" defaultConfig { applicationId "com.tomerpacific.example" minSdkVersion 15 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" dataBinding { //<------- enabled = true } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } }
build.gradle

अपने प्रोजेक्ट को सिंक्रनाइज़ करने के बाद, हम अपना डेटा क्लास बनाएंगे, जिसका उपयोग हम लेआउट से बाइंड करने के लिए करेंगे।

package com.tomerpacific.example import android.graphics.drawable.Drawable data class ButtonData(val buttonText: String, val buttonImageSrc : Drawable) 
ButtonData.kt

इस तथ्य पर ध्यान दें कि हमारे ButtonData क्लास में दो फ़ील्ड हैं:

  • buttonText — यह वह टेक्स्ट है जो हमारी छवि के नीचे दिखाई देगा।
  • buttonImageSrc — यह बटन की छवि के लिए जिम्मेदार है।

अगर हम अधिक डेटा चाहते, तो हम बस अपने डेटा क्लास में अधिक फ़ील्ड जोड़ देते।

वास्तविक बाइंडिंग

अगला, हमें अपने लेआउट में एक वेरिएबल प्रॉपर्टी घोषित करने की आवश्यकता है ताकि इसका उपयोग किया जा सके। यह वेरिएबल उस डेटा क्लास से बाउंड होगा जिसे हमने बनाया है। ऐसा करने के लिए, हमें दो चीजें करने की आवश्यकता है:

  • अपने रूट लेआउट तत्व को लेआउट टैग में लपेटें।
  • एक डेटा टैग जोड़ें जिसमें हमारे वेरिएबल (buttonData) की घोषणा होगी।
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="https://schemas.android.com/apk/res/android"> // <---- 1 <data> <variable name="buttonData" type="com.tomerpacific.example.ButtonData"/> // <---- 2 </data> <androidx.constraintlayout.widget.ConstraintLayout xmlns:app="https://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Example" android:textSize="30dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/linearLayout3" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2"> <ImageButton android:layout_width="100dp" android:layout_height="100dp" android:adjustViewBounds="true" android:scaleType="centerInside" android:src="@drawable/android"> </ImageButton> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Image Text" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" app:layout_constraintVertical_bias="0.504"> <ImageButton android:layout_width="100dp" android:layout_height="100dp" android:adjustViewBounds="true" android:scaleType="centerInside" android:src="@drawable/android_p_logo"> </ImageButton> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Image Text" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.200" app:layout_constraintStart_toEndOf="@+id/linearLayout3" app:layout_constraintTop_toBottomOf="@+id/textView2" app:layout_constraintVertical_bias="0.504"> <ImageButton android:layout_width="100dp" android:layout_height="100dp" android:adjustViewBounds="true" android:scaleType="centerInside" android:src="@drawable/android_studio_icon"> </ImageButton> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Image Text" android:textSize="16sp" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout> </layout>
activity_main.xml

ध्यान दें कि हम अपने पिछले मुख्य लेआउट से स्कीमा को हटा सकते हैं क्योंकि इसे रूट लेआउट टैग में ले जाया गया था। साथ ही, हमने जो वेरिएबल जोड़ा है, वह सीधे हमारे डेटा क्लास से जुड़ा हुआ है।

हमारी MainActivity फ़ाइल में, हमें बाइंडिंग को संभालने के लिए कोड जोड़ने की आवश्यकता है:

package com.tomerpacific.example import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.databinding.DataBindingUtil import com.tomerpacific.example.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val binding: ActivityMainBinding = DataBindingUtil.setContentView( this, R.layout.activity_main) binding.buttonData = ButtonData("First", resources.getDrawable(R.drawable.android)) } }
MainActivity.kt

हर बार जब आप किसी लेआउट के अंदर एक वेरिएबल बनाते हैं, तो उस लेआउट के लिए एक बाइंडिंग क्लास स्वचालित रूप से उत्पन्न होती है। हमारे मामले में, हमारा लेआउट activity_main कहलाता है, इसलिए बाइंडिंग क्लास का नाम ActivityMainBinding होगा।

सम्मेलन हमेशा लेआउट के नाम के अंत में Binding जोड़ने का होता है।

चूंकि हमने लेआउट में buttonData को अपने वेरिएबल के रूप में घोषित किया था, यह बाइंडिंग ऑब्जेक्ट से जुड़ जाता है और हम इसे अपने ButtonData क्लास का एक नया इंस्टेंस असाइन कर सकते हैं।

यह सब करने के बाद, हम अंततः उस डेटा का उपयोग अपने लेआउट में कर सकते हैं जिसे हमने अभी बाउंड किया है।

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/linearLayout3" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2"> <ImageButton android:layout_width="100dp" android:layout_height="100dp" android:adjustViewBounds="true" android:scaleType="centerInside" android:src="@{buttonData.buttonImageSrc}"> // <---- </ImageButton> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@{buttonData.buttonText}" // <---- android:textSize="16sp" />
बाउंड वेरिएबल buttonData का उपयोग करना

और परिणाम है:

Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण
काफी स्मूथ, है ना?

एक सेकंड रुकिए…

हमारे पास तीन बटन हैं और हमारा डेटा क्लास केवल एक बटन के लिए उपयोग किया जा सकता है, तो हम इससे कैसे पार पाते हैं?

package com.tomerpacific.example data class ButtonsData(val buttonsData : List<ButtonData>) { fun get(index: Int) : ButtonData { return buttonsData.get(index) } }
ButtonsData.kt

हमें get मेथड को ओवरराइड करना पड़ा क्योंकि जब हम इसे अपने लेआउट में उपयोग करते हैं तो इसे पहचाना जाना चाहिए।

फिर, हमें अपने activity_main.xml में संदर्भ बदलने की आवश्यकता है:

 <data> <variable name="buttonsData" type="com.tomerpacific.example.ButtonsData"/> </data>
हमने वेरिएबल नाम और क्लास को ButtonsData से मेल खाने के लिए बदल दिया

और हमें नए डेटा क्लास के लिए एक नई बाइंडिंग बनानी होगी:

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val binding: ActivityMainBinding = DataBindingUtil.setContentView( this, R.layout.activity_main) val firstButton : ButtonData = ButtonData("First", resources.getDrawable(R.drawable.android)) val secondButton : ButtonData = ButtonData("Second", resources.getDrawable(R.drawable.android_p_logo)) val thirdButton : ButtonData = ButtonData("Third", resources.getDrawable(R.drawable.android_studio_icon)) val buttonsData : ButtonsData = ButtonsData(listOf(firstButton, secondButton, thirdButton)) binding.buttonsData = buttonsData }
MainActivity.kt

हम ButtonData क्लास के तीन इंस्टेंस बनाते हैं। फिर हम एक ButtonsData ऑब्जेक्ट इंस्टेंशिएट करते हैं और इसे अपने बाइंडिंग ऑब्जेक्ट से जोड़ते हैं।

अंत में, हम अब अपने डेटा क्लास को अपने लेआउट में ठीक से उपयोग कर सकते हैं:

 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/linearLayout3" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2"> <ImageButton android:layout_width="100dp" android:layout_height="100dp" android:adjustViewBounds="true" android:scaleType="centerInside" android:src="@{buttonsData.get(0).buttonImageSrc}"> // <------- </ImageButton> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@{buttonsData.get(0).buttonText}" // <-------- android:textSize="16sp" /> </LinearLayout>
एक तत्व के उपयोग का उदाहरण
Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण
काम करता है जैसे जादू

यह लेख निम्नलिखित एप्लिकेशन को विकसित करने के मेरे अनुभव के कारण लिखा गया था:

(आप कभी नहीं जानते, यह उपयोगी हो सकता है)

Laundry Symbols - Apps on Google Play
कभी उन लॉन्ड्री प्रतीकों का अर्थ जानने में कठिनाई हुई? खैर, आप भाग्यशाली हैं! इस ऐप में उन अजीब दिखने वाले सभी प्रतीकों की व्याख्या है, एक आसान इंटरफ़ेस और चित्रों के साथ।
Android Data Binding मास्टर करें: सहज UI डेटा एकीकरणtomerpacific
Apps on Google Play
Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण

इसे Google Play Store के माध्यम से प्राप्त करें

आप संपूर्ण स्रोत कोड यहां देख सकते हैं:

TomerPacific/LaundrySymbols
Kotlin में लिखा गया एक एप्लिकेशन जो विभिन्न लॉन्ड्री प्रतीकों को व्याख्या के साथ दिखाता है :cyclone: - TomerPacific/LaundrySymbols
Android Data Binding मास्टर करें: सहज UI डेटा एकीकरणTomerPacific
GitHub
Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण

पूर्ण स्रोत कोड देखें

  1. Android पर डेटा उपयोग सीमा कैसे सेट करें Android पर डेटा उपयोग सीमा कैसे सेट करें

    क्या आप अपने Android डिवाइस पर डेटा उपयोग सीमा सेट करना चाहते हैं? यदि आपके पास अनलिमिटेड डेटा प्लान नहीं है, तो अपने हैंडसेट पर ऐप्स और सेवाओं को प्रतिबंधित किए बिना अपने डेटा को प्रबंधित करना मुश्किल हो सकता है। यदि आपको काम के लिए इंटरनेट की आवश्यकता है, तो यह मोबाइल डेटा के बिना रहना और भी कठिन

  1. ओवरएज रोकें: Systweak के Check Data Usage ऐप से Android डेटा यूसेज मास्टर करें ओवरएज रोकें: Systweak के Check Data Usage ऐप से Android डेटा यूसेज मास्टर करें

    इंटरनेट किसी भी स्मार्टफोन की जीवन रेखा है और दुख की बात है कि यह फ्री नहीं है। विभिन्न ISPs द्वारा विभिन्न प्लान प्रदान किए जाते हैं और यह एक देश से दूसरे देश में भी भिन्न होता है। विश्व स्तर पर फ्री Wi-Fi मिलने से पहले, हमें मोबाइल डेटा सेवर ऐप जैसे Check Data Usage का उपयोग करके Android स्मार्टफो

  1. एंड्रॉइड मोबाइल डेटा समस्याओं को तुरंत ठीक करें एंड्रॉइड मोबाइल डेटा समस्याओं को तुरंत ठीक करें

    जब आपको अपना पहला स्मार्टफोन मिलता है, तो सेल्युलर डेटा या मोबाइल डेटा उन चीजों में से एक बन जाता है जो जीवन की आवश्यकता बन जाती है। यह केवल तब पता चलता है जब आप कनेक्टिविटी खो देते हैं कि आपको पता नहीं है कि Google Maps के बिना आप कहां जा रहे हैं, Flipboard के बिना क्या पढ़ना है, और WhatsApp के बिन