1. परिचय
मटीरियल कॉम्पोनेंट (एमडीसी), डेवलपर को मटीरियल डिज़ाइन लागू करने में मदद करते हैं. एमडीसी को Google के इंजीनियर और यूएक्स डिज़ाइनर की टीम ने बनाया है. इसमें कई शानदार और काम के यूज़र इंटरफ़ेस (यूआई) कॉम्पोनेंट शामिल हैं. यह Android, iOS, वेब, और Flutter.material.io/develop के लिए उपलब्ध है |
कोडलैब MDC-101 में, आपने लॉगिन पेज बनाने के लिए दो मटीरियल कॉम्पोनेंट (एमडीसी) का इस्तेमाल किया था: टेक्स्ट फ़ील्ड और इंक रिपल वाले बटन. अब नेविगेशन, स्ट्रक्चर, और डेटा जोड़कर, इस फ़ाउंडेशन को और बेहतर बनाते हैं.
आपको क्या बनाना होगा
इस कोडलैब में, आपको Shrine नाम के ऐप्लिकेशन के लिए होम स्क्रीन बनानी होगी. यह एक ई-कॉमर्स ऐप्लिकेशन है, जो कपड़े और घर का सामान बेचता है. इसमें ये चीज़ें शामिल होंगी:
- टॉप ऐप्लिकेशन बार
- प्रॉडक्ट से भरी ग्रिड लिस्ट

इस कोडलैब में मौजूद MDC-Android कॉम्पोनेंट
- AppBarLayout
- MaterialCardView
आपको इन चीज़ों की ज़रूरत होगी
- Android डेवलपमेंट की बुनियादी जानकारी
- Android Studio (अगर आपके पास यह पहले से नहीं है, तो इसे यहां से डाउनलोड करें)
- Android एम्युलेटर या डिवाइस (Android Studio के ज़रिए उपलब्ध)
- सैंपल कोड (अगला चरण देखें)
Android ऐप्लिकेशन बनाने के अपने अनुभव को किस तरह से रेटिंग दी जा सकती है?
2. अपना डेवलपमेंट एनवायरमेंट सेट अप करना
क्या आपको MDC-101 से आगे बढ़ना है?
अगर आपने MDC-101 पूरा कर लिया है, तो आपका कोड इस कोडलैब के लिए तैयार होना चाहिए. सीधे तीसरे चरण पर जाएं: टॉप ऐप्लिकेशन बार जोड़ना.
क्या आपको नए सिरे से शुरुआत करनी है?
स्टार्टर कोडलैब ऐप्लिकेशन डाउनलोड करना
स्टार्टर ऐप्लिकेशन, material-components-android-codelabs-102-starter/kotlin डायरेक्ट्री में मौजूद है. शुरू करने से पहले, उस डायरेक्ट्री में cd करना न भूलें.
...या इसे GitHub से क्लोन करें
GitHub से इस कोडलैब का क्लोन बनाने के लिए, ये कमांड दें:
git clone https://github.com/material-components/material-components-android-codelabs cd material-components-android-codelabs/ git checkout 102-starter
Android Studio में स्टार्टर कोड लोड करना
- सेटअप विज़र्ड पूरा होने और Android Studio में आपका स्वागत है विंडो दिखने के बाद, कोई मौजूदा Android Studio प्रोजेक्ट खोलें पर क्लिक करें. उस डायरेक्ट्री पर जाएं जहां आपने सैंपल कोड इंस्टॉल किया था. इसके बाद, शिपिंग प्रोजेक्ट खोलने के लिए, kotlin -> shrine को चुनें या अपने कंप्यूटर पर shrine खोजें.
- Android Studio को प्रोजेक्ट बनाने और सिंक करने के लिए कुछ समय दें. Android Studio विंडो के सबसे नीचे मौजूद गतिविधि इंडिकेटर से पता चलता है कि प्रोजेक्ट बन रहा है और सिंक हो रहा है.
- इस समय, Android Studio में बिल्ड से जुड़ी कुछ गड़बड़ियां दिख सकती हैं. ऐसा इसलिए हो सकता है, क्योंकि आपके पास Android SDK या बिल्ड टूल नहीं हैं. जैसे, यहां दिखाया गया टूल. Android Studio में दिए गए निर्देशों का पालन करके, इन्हें इंस्टॉल/अपडेट करें और अपने प्रोजेक्ट को सिंक करें.
प्रोजेक्ट डिपेंडेंसी जोड़ना
प्रोजेक्ट को MDC Android सपोर्ट लाइब्रेरी पर निर्भर होना चाहिए. आपने जो सैंपल कोड डाउनलोड किया है उसमें यह डिपेंडेंसी पहले से ही मौजूद होनी चाहिए. हालांकि, यह पक्का करने के लिए कि यह डिपेंडेंसी मौजूद है, यहां दिया गया तरीका अपनाएं.
appमॉड्यूल कीbuild.gradleफ़ाइल पर जाएं. साथ ही, पक्का करें किdependenciesब्लॉक में MDC Android की डिपेंडेंसी शामिल हो:
api 'com.google.android.material:material:1.1.0-alpha06'
- (ज़रूरी नहीं) अगर ज़रूरी हो, तो
build.gradleफ़ाइल में बदलाव करके, ये डिपेंडेंसी जोड़ें और प्रोजेक्ट को सिंक करें.
dependencies {
api 'com.google.android.material:material:1.1.0-alpha06'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:core:1.1.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test:runner:1.2.0-alpha05'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha05'
}
स्टार्टर ऐप्लिकेशन चलाना
|
हो गया! आपको MDC-101 कोडलैब से Shrine का लॉगिन पेज दिखेगा.

अब लॉगिन स्क्रीन अच्छी दिख रही है. चलिए, ऐप्लिकेशन में कुछ प्रॉडक्ट जोड़ते हैं.
3. टॉप ऐप्लिकेशन बार जोड़ना
लॉगिन पेज को खारिज करने पर, होम स्क्रीन दिखती है. इस स्क्रीन पर "आपने कर दिखाया!" लिखा होता है. बहुत बढ़िया! हालांकि, अब हमारे उपयोगकर्ता के पास कोई कार्रवाई करने का विकल्प नहीं है. इसके अलावा, उसे यह भी नहीं पता कि वह ऐप्लिकेशन में कहां है. इस समस्या को हल करने के लिए, अब नेविगेशन जोड़ने का समय आ गया है.
मटीरियल डिज़ाइन, नेविगेशन पैटर्न उपलब्ध कराता है. इससे यह पक्का होता है कि ऐप्लिकेशन को इस्तेमाल करना आसान हो. सबसे ज़्यादा दिखने वाले कॉम्पोनेंट में से एक, टॉप ऐप्लिकेशन बार है.
नेविगेशन की सुविधा देने और उपयोगकर्ताओं को अन्य कार्रवाइयों का तुरंत ऐक्सेस देने के लिए, चलिए टॉप ऐप्लिकेशन बार जोड़ते हैं.
AppBar विजेट जोड़ना
shr_product_grid_fragment.xml में, "आपने कर दिखाया!" वाले <LinearLayout> ब्लॉक को मिटाएं TextView और उसकी जगह यह डालें:
shr_product_grid_fragment.xml
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/app_bar"
style="@style/Widget.Shrine.Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="@string/shr_app_name" />
</com.google.android.material.appbar.AppBarLayout>
अब आपका shr_product_grid_fragment.xml ऐसा दिखना चाहिए:
shr_product_grid_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProductGridFragment">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/app_bar"
style="@style/Widget.Shrine.Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="@string/shr_app_name" />
</com.google.android.material.appbar.AppBarLayout>
</FrameLayout>
कई ऐप्लिकेशन बार में, टाइटल के बगल में एक बटन होता है. चलिए, हम अपने ऐप्लिकेशन में मेन्यू आइकॉन जोड़ते हैं.
नेविगेशन आइकॉन जोड़ना
shr_product_grid_fragment.xml में रहते हुए, Toolbar एक्सएमएल कॉम्पोनेंट में यह कोड जोड़ें. यह कॉम्पोनेंट आपने अभी-अभी अपने लेआउट में जोड़ा है:
shr_product_grid_fragment.xml
app:navigationIcon="@drawable/shr_menu"
आपका shr_product_grid_fragment.xml ऐसा दिखना चाहिए:
shr_product_grid_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProductGridFragment">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/app_bar"
style="@style/Widget.Shrine.Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="@drawable/shr_menu"
app:title="@string/shr_app_name" />
</com.google.android.material.appbar.AppBarLayout>
</FrameLayout>
ऐक्शन बटन जोड़ना और सबसे ऊपर मौजूद ऐप्लिकेशन बार को स्टाइल करना
ऐप्लिकेशन बार के आखिर में बटन भी जोड़े जा सकते हैं. Android में, इन्हें ऐक्शन बटन कहा जाता है. हम टॉप ऐप्लिकेशन बार को स्टाइल करेंगे और प्रोग्राम के हिसाब से उसके मेन्यू में ऐक्शन बटन जोड़ेंगे.
ProductGridFragment.kt के onCreateView फ़ंक्शन में, activity के Toolbar को ActionBar के तौर पर इस्तेमाल करने के लिए सेट करें. इसके लिए, setSupportActionBar का इस्तेमाल करें. inflater की मदद से व्यू बनाने के बाद, ऐसा किया जा सकता है.
ProductGridFragment.kt
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment with the ProductGrid theme
val view = inflater.inflate(R.layout.shr_product_grid_fragment, container, false)
// Set up the toolbar.
(activity as AppCompatActivity).setSupportActionBar(view.app_bar)
return view;
}
इसके बाद, टूलबार सेट अप करने के लिए हमने जिस तरीके को बदला है उसके ठीक नीचे, onCreateOptionsMenu को बदलें, ताकि shr_toolbar_menu.xml के कॉन्टेंट को टूलबार में शामिल किया जा सके:
ProductGridFragment.kt
override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.shr_toolbar_menu, menu)
super.onCreateOptionsMenu(menu, menuInflater)
}
आखिर में, ProductGridFragment.kt में onCreate() को बदलें. इसके बाद, super() को कॉल करने के बाद, true के साथ setHasOptionMenu को कॉल करें:
ProductGridFragment.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
ऊपर दिए गए कोड स्निपेट, हमारे एक्सएमएल लेआउट से ऐप्लिकेशन बार को इस गतिविधि के लिए कार्रवाई बार के तौर पर सेट करते हैं. कॉलबैक onCreateOptionsMenu, गतिविधि को बताता है कि हमारे मेन्यू के तौर पर क्या इस्तेमाल करना है. इस मामले में, यह R.menu.shr_toolbar_menu से मेन्यू आइटम को ऐप्लिकेशन बार में डाल देगा. मेन्यू फ़ाइल में दो आइटम शामिल हैं: "खोजें" और "फ़िल्टर करें".
shr_toolbar_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/shr_search"
android:title="@string/shr_search_title"
app:showAsAction="always" />
<item
android:id="@+id/filter"
android:icon="@drawable/shr_filter"
android:title="@string/shr_filter_title"
app:showAsAction="always" />
</menu>
इन बदलावों के बाद, आपकी ProductGridFragment.kt फ़ाइल ऐसी दिखनी चाहिए:
ProductGridFragment.kt
package com.google.codelabs.mdc.kotlin.shrine
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.google.codelabs.mdc.kotlin.shrine.network.ProductEntry
import kotlinx.android.synthetic.main.shr_product_grid_fragment.view.*
class ProductGridFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment with the ProductGrid theme
val view = inflater.inflate(R.layout.shr_product_grid_fragment, container, false)
// Set up the tool bar
(activity as AppCompatActivity).setSupportActionBar(view.app_bar)
return view;
}
override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.shr_toolbar_menu, menu)
super.onCreateOptionsMenu(menu, menuInflater)
}
}
बनाएं और चलाएं. आपकी होम स्क्रीन कुछ इस तरह दिखेगी:

अब टूलबार में एक नेविगेशन आइकॉन, एक टाइटल, और दाईं ओर दो ऐक्शन आइकॉन हैं. टूलबार में, एलिवेशन को दिखाने के लिए हल्की शैडो का इस्तेमाल किया जाता है. इससे पता चलता है कि यह कॉन्टेंट से अलग लेयर पर है.
4. कार्ड जोड़ें
अब हमारे ऐप्लिकेशन का स्ट्रक्चर तैयार हो गया है. चलिए, कॉन्टेंट को कार्ड में रखकर व्यवस्थित करते हैं.
कोई कार्ड जोड़ना
चलिए, सबसे ऊपर मौजूद ऐप्लिकेशन बार के नीचे एक कार्ड जोड़ते हैं. कार्ड में इमेज, टाइटल, और सेकंडरी टेक्स्ट के लेबल के लिए एक सेक्शन होना चाहिए. AppBarLayout के नीचे shr_product_grid_fragment.xml में यह जोड़ें.
shr_product_grid_fragment.xml
<com.google.android.material.card.MaterialCardView
android:layout_width="160dp"
android:layout_height="180dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="70dp"
app:cardBackgroundColor="?attr/colorPrimaryDark"
app:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#FFFFFF"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="@string/shr_product_title"
android:textAppearance="?attr/textAppearanceHeadline6" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="@string/shr_product_description"
android:textAppearance="?attr/textAppearanceBody2" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
बनाएं और चलाएं:

इस झलक में, आपको दिखेगा कि कार्ड को बाईं ओर से इनसेट किया गया है. साथ ही, इसके कोने गोल हैं और इसमें एक शैडो है. इससे कार्ड की ऊंचाई का पता चलता है. इस पूरे एलिमेंट को "कंटेनर" कहा जाता है. कंटेनर के अलावा, इसके अंदर मौजूद सभी एलिमेंट ज़रूरी नहीं हैं.
कंटेनर में ये एलिमेंट जोड़े जा सकते हैं: हेडर टेक्स्ट, थंबनेल या अवतार, सबहेड टेक्स्ट, डिवाइडर, और बटन और आइकॉन. उदाहरण के लिए, हमने जो कार्ड अभी बनाया है उसमें LinearLayout में दो TextView हैं. इनमें से एक टाइटल के लिए और दूसरा सेकंडरी टेक्स्ट के लिए है. ये दोनों कार्ड के सबसे नीचे अलाइन किए गए हैं.
कार्ड आम तौर पर, अन्य कार्ड के साथ कलेक्शन में दिखाए जाते हैं. इस कोडलैब के अगले सेक्शन में, हम उन्हें ग्रिड में कलेक्शन के तौर पर लेआउट करेंगे.
5. कार्ड का ग्रिड बनाना
जब किसी स्क्रीन पर कई कार्ड मौजूद होते हैं, तो उन्हें एक या उससे ज़्यादा कलेक्शन में ग्रुप किया जाता है. ग्रिड में मौजूद कार्ड कोप्लेनर होते हैं. इसका मतलब है कि वे एक-दूसरे के साथ एक ही रेस्टिंग एलिवेशन शेयर करते हैं. हालांकि, ऐसा तब तक होता है, जब तक उन्हें पिक अप या ड्रैग न किया जाए. हालांकि, हम इस कोडलैब में इसे कवर नहीं करेंगे.
कार्ड के ग्रिड को सेट अप करना
हमने आपके लिए जो shr_product_card.xml फ़ाइल उपलब्ध कराई है उसे देखें:
shr_product_card.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/white"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/product_image"
android:layout_width="match_parent"
android:layout_height="@dimen/shr_product_card_image_height"
android:background="?attr/colorPrimaryDark"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/product_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/shr_product_title"
android:textAppearance="?attr/textAppearanceHeadline6" />
<TextView
android:id="@+id/product_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/shr_product_description"
android:textAppearance="?attr/textAppearanceBody2" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
इस कार्ड लेआउट में एक कार्ड है, जिसमें एक इमेज (इस मामले में, एक NetworkImageView, जो हमें किसी यूआरएल से इमेज लोड करने और दिखाने की अनुमति देता है) और दो TextViews हैं.
इसके बाद, हमारी ओर से उपलब्ध कराए गए ProductCardRecyclerViewAdapter को देखें. यह ProductGridFragment के साथ ही उपलब्ध है.
ProductCardRecyclerViewAdapter.kt
package com.google.codelabs.mdc.kotlin.shrine
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.google.codelabs.mdc.kotlin.shrine.network.ProductEntry
/**
* Adapter used to show a simple grid of products.
*/
class ProductCardRecyclerViewAdapter(private val productList: List<ProductEntry>) : RecyclerView.Adapter<ProductCardViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductCardViewHolder {
val layoutView = LayoutInflater.from(parent.context).inflate(R.layout.shr_product_card, parent, false)
return ProductCardViewHolder(layoutView)
}
override fun onBindViewHolder(holder: ProductCardViewHolder, position: Int) {
// TODO: Put ViewHolder binding code here in MDC-102
}
override fun getItemCount(): Int {
return productList.size
}
}
ऊपर दी गई अडैप्टर क्लास, हमारे ग्रिड के कॉन्टेंट को मैनेज करती है. हर व्यू को अपने कॉन्टेंट के साथ क्या करना चाहिए, यह तय करने के लिए हम जल्द ही onBindViewHolder() के लिए कोड लिखेंगे.
इसी पैकेज में, ProductCardViewHolder भी देखा जा सकता है. यह क्लास उन व्यू को सेव करती है जो हमारे कार्ड लेआउट पर असर डालते हैं. इससे हम उन्हें बाद में बदल सकते हैं.
package com.google.codelabs.mdc.kotlin.shrine
import android.view.View
import androidx.recyclerview.widget.RecyclerView
class ProductCardViewHolder(itemView: View) //TODO: Find and store views from itemView
: RecyclerView.ViewHolder(itemView)
ग्रिड सेट अप करने के लिए, सबसे पहले हमें shr_product_grid_fragment.xml से प्लेसहोल्डर MaterialCardView को हटाना होगा. इसके बाद, आपको कार्ड के ग्रिड को दिखाने वाला कॉम्पोनेंट जोड़ना चाहिए. इस मामले में, हम RecyclerView का इस्तेमाल करेंगे. RecyclerView कॉम्पोनेंट को अपने shr_product_grid_fragment.xml में, AppBarLayout एक्सएमएल कॉम्पोनेंट के नीचे जोड़ें:
shr_product_grid_fragment.xml
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
android:background="@color/productGridBackgroundColor"
android:paddingStart="@dimen/shr_product_grid_spacing"
android:paddingEnd="@dimen/shr_product_grid_spacing"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.core.widget.NestedScrollView>
आपका shr_product_grid_fragment.xml ऐसा दिखना चाहिए:
shr_product_grid_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProductGridFragment">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/app_bar"
style="@style/Widget.Shrine.Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="@drawable/shr_menu"
app:title="@string/shr_app_name" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
android:background="@color/productGridBackgroundColor"
android:paddingStart="@dimen/shr_product_grid_spacing"
android:paddingEnd="@dimen/shr_product_grid_spacing"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.core.widget.NestedScrollView>
</FrameLayout>
आखिर में, onCreateView() में RecyclerView के शुरू होने का कोड ProductGridFragment.kt में जोड़ें. ऐसा setUpToolbar(view) को कॉल करने के बाद और return स्टेटमेंट से पहले करें:
ProductGridFragment.kt
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment with the ProductGrid theme
val view = inflater.inflate(R.layout.shr_product_grid_fragment, container, false)
// Set up the toolbar.
(activity as AppCompatActivity).setSupportActionBar(view.app_bar)
// Set up the RecyclerView
view.recycler_view.setHasFixedSize(true)
view.recycler_view.layoutManager = GridLayoutManager(context, 2, RecyclerView.VERTICAL, false)
val adapter = ProductCardRecyclerViewAdapter(
ProductEntry.initProductEntryList(resources))
view.recycler_view.adapter = adapter
val largePadding = resources.getDimensionPixelSize(R.dimen.shr_product_grid_spacing)
val smallPadding = resources.getDimensionPixelSize(R.dimen.shr_product_grid_spacing_small)
view.recycler_view.addItemDecoration(ProductGridItemDecoration(largePadding, smallPadding))
return view;
}
ऊपर दिए गए कोड स्निपेट में, RecyclerView को सेट अप करने के लिए ज़रूरी शुरुआती चरण शामिल हैं. इसमें RecyclerView का लेआउट मैनेजर सेट करना, RecyclerView का अडैप्टर शुरू करना, और उसे सेट करना शामिल है.
आपकी ProductGridFragment.kt फ़ाइल अब कुछ ऐसी दिखनी चाहिए:
ProductGridFragment .kt
package com.google.codelabs.mdc.kotlin.shrine
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.codelabs.mdc.kotlin.shrine.network.ProductEntry
import kotlinx.android.synthetic.main.shr_product_grid_fragment.view.*
class ProductGridFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment with the ProductGrid theme
val view = inflater.inflate(R.layout.shr_product_grid_fragment, container, false)
// Set up the toolbar.
(activity as AppCompatActivity).setSupportActionBar(view.app_bar)
// Set up the RecyclerView
view.recycler_view.setHasFixedSize(true)
view.recycler_view.layoutManager = GridLayoutManager(context, 2, RecyclerView.VERTICAL, false)
val adapter = ProductCardRecyclerViewAdapter(
ProductEntry.initProductEntryList(resources))
view.recycler_view.adapter = adapter
val largePadding = resources.getDimensionPixelSize(R.dimen.shr_product_grid_spacing)
val smallPadding = resources.getDimensionPixelSize(R.dimen.shr_product_grid_spacing_small)
view.recycler_view.addItemDecoration(ProductGridItemDecoration(largePadding, smallPadding))
return view;
}
override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.shr_toolbar_menu, menu)
super.onCreateOptionsMenu(menu, menuInflater)
}
}
बनाएं और चलाएं:
कार्ड अब वहां मौजूद हैं! इनमें अब तक कोई डेटा नहीं दिख रहा है. इसलिए, चलिए कुछ प्रॉडक्ट डेटा जोड़ते हैं.
इमेज और टेक्स्ट जोड़ना
हर कार्ड के लिए, एक इमेज, प्रॉडक्ट का नाम, और कीमत जोड़ें. हमारे ViewHolder एब्स्ट्रैक्शन में हर कार्ड के व्यू होते हैं. ViewHolder में, तीन व्यू इस तरह जोड़ें.
ProductCardViewHolder.kt
package com.google.codelabs.mdc.kotlin.shrine
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.toolbox.NetworkImageView
class ProductCardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var productImage: NetworkImageView = itemView.findViewById(R.id.product_image)
var productTitle: TextView = itemView.findViewById(R.id.product_title)
var productPrice: TextView = itemView.findViewById(R.id.product_price)
}
हर प्रॉडक्ट व्यू के लिए टाइटल, कीमत, और प्रॉडक्ट की इमेज सेट करने के लिए, ProductCardRecyclerViewAdapter में onBindViewHolder() मेथड को अपडेट करें. इसके लिए, यहां दिया गया तरीका अपनाएं:
ProductCardRecyclerViewAdapter.kt
override fun onBindViewHolder(holder: ProductCardViewHolder, position: Int) {
if (position < productList.size) {
val product = productList[position]
holder.productTitle.text = product.title
holder.productPrice.text = product.price
ImageRequester.setImageFromUrl(holder.productImage, product.url)
}
}
ऊपर दिया गया कोड, ViewHolder का इस्तेमाल करके हमारे RecyclerView के अडैप्टर को बताता है कि हर कार्ड के साथ क्या करना है.
यहां, यह हर ViewHolder के TextView पर टेक्स्ट डेटा सेट करता है. साथ ही, यूआरएल से इमेज पाने के लिए, ImageRequester को कॉल करता है. ImageRequester एक क्लास है, जिसे हमने आपकी सुविधा के लिए उपलब्ध कराया है. यह Volley लाइब्रेरी का इस्तेमाल करती है. यह इस कोडलैब के दायरे से बाहर का विषय है, लेकिन आप चाहें, तो कोड को खुद एक्सप्लोर करें.
बनाएं और चलाएं:

हमारे प्रॉडक्ट अब ऐप्लिकेशन में दिख रहे हैं!
6. रीकैप
हमारे ऐप्लिकेशन में एक बुनियादी फ़्लो है, जो उपयोगकर्ता को लॉगिन स्क्रीन से होम स्क्रीन पर ले जाता है. यहां प्रॉडक्ट देखे जा सकते हैं. कोड की कुछ ही लाइनों में, हमने टाइटल और तीन बटन वाला एक टॉप ऐप्लिकेशन बार जोड़ा. साथ ही, अपने ऐप्लिकेशन का कॉन्टेंट दिखाने के लिए, कार्ड का एक ग्रिड जोड़ा. हमारी होम स्क्रीन अब आसान और काम की है. इसमें बुनियादी स्ट्रक्चर और काम का कॉन्टेंट मौजूद है.
अगले चरण
हमने MDC-Android लाइब्रेरी से, मटीरियल डिज़ाइन के चार मुख्य कॉम्पोनेंट इस्तेमाल किए हैं. इनमें टॉप ऐप्लिकेशन बार, कार्ड, टेक्स्ट फ़ील्ड, और बटन शामिल हैं! MDC-Android Catalog पर जाकर, और भी कॉम्पोनेंट एक्सप्लोर किए जा सकते हैं.
हालांकि, यह पूरी तरह से काम करता है, लेकिन हमारा ऐप्लिकेशन अभी तक किसी खास ब्रैंड या स्टाइल को नहीं दिखाता है. MDC-103: Material Design Theming with Color, Shape, Elevation and Type में, हम इन कॉम्पोनेंट की स्टाइल को पसंद के मुताबिक बनाएंगे, ताकि एक शानदार और मॉडर्न ब्रैंड को दिखाया जा सके.
