Laboratoriya ishi №16 Mavzu: Voqealar (intents)
Download 1.54 Mb.
|
V4Z-xNaOmQAahUwVDNttZzzoKCT-FHY0
Laboratoriya ishi №17
Mavzu: Fragmentlardan foydalanish Android Studioda fragment sinfini yaratish: Fragment yaratish uchun birinchi navbatda biz Fragment sinfini kengaytiramiz, so'ngra Faollik sinfidagi kabi ilova mantig'ini kiritish uchun asosiy hayot aylanish usullarini bekor qilamiz. Fragment yaratishda tartibni aniqlash va Fragmentni ishga tushirish uchun onCreateView() qayta qo'ng'iroq qilishimiz kerak. import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.ViewGroup; public class FirstFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_first, container, false); } } Bu erda inflater parametri tartibni to'ldirish uchun ishlatiladigan LayoutInflater, konteyner parametri esa bizning Fragment joylashuvimiz kiritiladigan asosiy ViewGroup (faoliyat tartibidan). savedInstanceState parametri Fragmentning oldingi nusxasi haqida ma'lumot beruvchi to'plamdir. inflate() usuli uchta argumentga ega, birinchisi, biz o'rnatmoqchi bo'lgan resurs tartibi, ikkinchisi - ViewGroup - shishirilgan tartibning ota-onasi bo'lishi. Konteynerdan o'tish tizimning asosiy ko'rinishda ko'rsatilgan shishirilgan joylashuvning asosiy ko'rinishiga joylashtirish parametrlarini qo'llashi uchun muhim ahamiyatga ega va uchinchi parametr boolean qiymat bo'lib, shishirilgan sxema ilovaga biriktirilishi kerakligini ko'rsatadi. Inflyatsiya davrida ViewGroup (ikkinchi parametr). Fragmentni Android-da amalga oshirish Honeycomb (3.0) yoki undan keyingi versiyasini talab qiladi: Fragmentlar Androidning Honeycomb versiyasiga, ya'ni API 11-versiyasiga qo'shilgan. Fragmentlarga tegishli ba'zi birlamchi sinflar mavjud: 1. FragmentActivity: Muvofiqlikka asoslangan Fragment (va yuklovchi) funksiyalaridan foydalangan holda barcha harakatlar uchun asosiy sinf. 2. Fragment: Fragmentning barcha ta'riflari uchun asosiy sinf. 3. FragmentManager: Faoliyat ichidagi Fragment ob'ektlari bilan o'zaro ishlash uchun sinf. 4. FragmentTransaction: Fragmentni almashtirish yoki qo'shish kabi Fragment operatsiyalarining atom to'plamini bajarish uchun sinf. Android Studio'da 1-misol fragmenti: Quyida Fragmentning misoli keltirilgan. Ushbu misolda biz ikkita Fragment yaratamiz va ularni tugmachani bosish orqali yuklaymiz. Biz Faoliyatimizda ikkita tugma va FrameLayoutni ko'rsatamiz va ikkala tugmachada setOnClickListener hodisasini bajaramiz. Birinchi tugmani bosish bilan biz Birinchi Fragmentni almashtiramiz va Ikkinchi Tugmachani bosish bilan Ikkinchi Fragmentni layout (FrameLayout) bilan almashtiramiz. Ikkala Fragmentda biz TextView va Tugmachani ko'rsatamiz va Tugmachani bosish orqali biz Fragment nomini Tushdi yordamida ko'rsatamiz. android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> android:id="@+id/firstFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/button_background_color" android:text="First Fragment" android:textColor="@color/white" android:textSize="20sp" /> android:id="@+id/secondFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="@color/button_background_color" android:text="Second Fragment" android:textColor="@color/white" android:textSize="20sp" /> android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" /> Ushbu bosqichda biz MainActivity-ni ochamiz va tugmani ishga tushirish uchun kodni qo'shamiz. Shundan so'ng biz ikkala tugmachada setOnClickListener hodisasini bajaramiz. Birinchi tugmani bosish bilan biz Birinchi Fragmentni almashtiramiz va Ikkinchi Tugmachani bosish bilan Ikkinchi Fragmentni layout (FrameLayout) bilan almashtiramiz. Fragmentni FrameLayout bilan almashtirish uchun biz birinchi navbatda Fragment menejerini yaratamiz va keyin Fragment Transaction yordamida tranzaktsiyani boshlaymiz va nihoyat Fragmentni maket, ya'ni FrameLayout bilan almashtiramiz. package com.abhiandroid.fragmentexample; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button firstFragment, secondFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // get the reference of Button's firstFragment = (Button) findViewById(R.id.firstFragment); secondFragment = (Button) findViewById(R.id.secondFragment); // perform setOnClickListener event on First Button firstFragment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // load First Fragment loadFragment(new FirstFragment()); } }); // perform setOnClickListener event on Second Button secondFragment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // load Second Fragment loadFragment(new SecondFragment()); } }); }
// create a FragmentManager FragmentManager fm = getFragmentManager(); // create a FragmentTransaction to begin the transaction and replace the Fragment FragmentTransaction fragmentTransaction = fm.beginTransaction(); // replace the FrameLayout with new Fragment fragmentTransaction.replace(R.id.frameLayout, fragment); fragmentTransaction.commit(); // save the changes } } Endi bizga 2 ta fragment va 2 xml maket kerak. Shunday qilib, paket papkangizni o'ng tugmasini bosib ikkita bo'lak yarating va sinflarni yarating va ularni FirstFragment va SecondFragment deb nomlang va mos ravishda quyidagi kodni qo'shing. FirstFragment.class Ushbu fragmentda biz birinchi navbatda tartibni kengaytiramiz va tugmachaning havolasini olamiz. Shundan so'ng biz Tugmachada setOnClickListener hodisasini amalga oshiramiz, shuning uchun foydalanuvchi tugmani bosganida, tost yordamida ekranda "Birinchi parcha" xabari ko'rsatiladi. package com.abhiandroid.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; public class FirstFragment extends Fragment { View view; Button firstButton; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment view = inflater.inflate(R.layout.fragment_first, container, false); // get the reference of Button firstButton = (Button) view.findViewById(R.id.firstButton); // perform setOnClickListener on first Button firstButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // display a message by using a Toast Toast.makeText(getActivity(), "First Fragment", Toast.LENGTH_LONG).show(); } }); return view; } } SecondFragment.class Ushbu fragmentda biz birinchi navbatda tartibni kengaytiramiz va tugmachaning havolasini olamiz. Shundan so'ng biz Tugmachada setOnClickListener hodisasini amalga oshiramiz, shuning uchun foydalanuvchi tugmani bosganida, tost yordamida ekranda "Ikkinchi fragment" xabari ko'rsatiladi. package com.abhiandroid.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; public class SecondFragment extends Fragment { View view; Button secondButton; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment view = inflater.inflate(R.layout.fragment_second, container, false); // get the reference of Button secondButton = (Button) view.findViewById(R.id.secondButton); // perform setOnClickListener on second Button secondButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // display a message by using a Toast Toast.makeText(getActivity(), "Second Fragment", Toast.LENGTH_LONG).show(); } }); return view; } } Endi res/layout -> New -> Layout Resource File-ni o'ng tugmasini bosib 2 ta xml maketini yarating va ularni fragment_first va fragment_second deb nomlang va tegishli fayllarga quyidagi kodni qo'shing. Bu erda biz ikkala xml-da TextView va Button yordamida asosiy oddiy UI-ni loyihalashtiramiz. fragment_first.xml android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.abhiandroid.fragmentexample.FirstFragment"> android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:text="This is First Fragment" android:textColor="@color/black" android:textSize="25sp" /> android:id="@+id/firstButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="@color/green" android:text="First Fragment" android:textColor="@color/white" android:textSize="20sp" android:text /> fragment_second.xml android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.abhiandroid.fragmentexample.SecondFragment"> android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:text="This is Second Fragment" android:textColor="@color/black" android:textSize="25sp" /> android:id="@+id/secondButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="@color/green" android:text="Second Fragment" android:textColor="@color/white" android:textSize="20sp" android:text /> res ->values ->colors.xml ni oching Ushbu bosqichda biz xml faylimizda ishlatiladigan rangni aniqlaymiz. AndroidManifest.xml ni oching Ushbu bosqichda biz hech narsa qilmaydigan Android Manifest faylini ko'rsatamiz, chunki bizga faqat bitta Faoliyat kerak, ya'ni unda allaqachon aniqlangan MainActivity. Loyihamizda biz ikkita Fragment yaratamiz, lekin Fragmentni manifestda belgilashimiz shart emas, chunki Fragment faoliyatning bir qismidir. android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > android:label="@string/app_name" > Endi ilovani ishga tushiring va siz ikkita tugmachani olasiz. Birinchi tugmani bosish Birinchi Fragmentni ko'rsatadi va Ikkinchi Tugmachani bosish bilan aslida maketni (FrameLayout) almashtiradigan Ikkinchi Fragmentni ko'rsatadi. Download 1.54 Mb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling