Assalomu alaykum. Bugun sizlar bilan birgalikda Android Studio-da fragmentlar bilan ishlashni ko'rib chiqamiz. Sizlarga foydali bo'ladi, degan umiddaman. Demak boshladik.

Fragment activity tarkibida mavjud bo'lib, o'ziga xos hayot sikliga ega. activity tashqarisida u alohida mavjud bo'lolmaydi. Har bir activity bir nechta fragment-larga ega bo'lishi mumkin.

Fragment bilan ishlashni boshlash uchun Android Studio-ni ochib, yangi ilova yarataylik. Va avval, birinchi fragment-ni yarataylik.

Fragmentlarda activity bilan bir xil elementlar mavjud. Shuning uchun res/layout papkasida yangi fragment_content.xml faylini yarataylik. Yaratib bo'lgandan keyin ichidagi kodlar quyidagicha bo'lishi kerak:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/update_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Yangilash" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Bu fayl fragment interfeysini tashkil etadigan tugma va matn maydoni(TextView)ni yaratadi.

Endi fragment sinfini o'zini yarataylik. Buning uchun MainActivity.java fayli mavjud papkaga yangi sinf qo'shamiz. Buni amalga oshirish uchun papkaga sichqonchani olib borib, sichqonchaning o'ng tugmasini bosing va menyudan New -> Java Class -ni tanlang. Keling, yangi sinfga ContentFragment deb nom beramiz va kod yozishni boshlaymiz:

package com.example.android.fragmentlar;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import java.util.Date;

public class ContentFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content, container, false);

        Button updateButton = view.findViewById(R.id.update_button);
        final TextView updateBox = view.findViewById(R.id.text);

        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String curDate = new Date().toString();
                updateBox.setText(curDate);
            }
        });
        return view;
    }
}

Shu sinf Fragment sinfidan meros bo'lib o'tadi.

Vizual interfeysni yaratish uchun fragment onCreateView() metodini bekor qiladi. Unda uchta parametr mavjud:

  • LayoutInflater ob'ekti interfeys yaratish uchun layout resursini o'rnatish uchun ishlatiladi.
  • ViewGroup container parametri interfeys konteynerini o'rnatadi.
  • Bundle savedInstanceState parametri avval saqlangan holatni beradi.

Interfeys yaratish uchun LayoutInflater ob'ektining inflate() metodi qo'llaniladi. U berilgan fragment uchun layout manbasini, interfeysni o'z ichiga oladigan konteynerni oladi va uchinchi mantiqiy parametr ikkinchi parametrdan konteynerga layout qo'shish yoki qo'shmasligini aniqlaydi.

activity-da bo'lgani kabi, bu kodda biz bosish "tinglovchisi" voqealarini boshqarishimiz mumkin. Bizni kodda, joriy sana matn maydonida ko'rsatiladi.

Shuni ham ta'kidlash kerakki, Android Studioda fragment qo'shish uchun maxsus shablon mavjud. Uni ishlatish uchun, sinflar to'plami(MainActivity.java, ContentFragment.java) mavjud papkaga sichqonchani olib borib, sichqonchaning o'ng tugmasini bosib, ochilgan menyudan New -> Fragment -> Fragment(Blank) -ni tanlashimiz kerak:

 

Ushbu shablon darhol loyihaga fragment sinfini ham, unga tegishli layout (xml) faylni ham qo'shadi.

 

fragment-ni activityga qo'shamiz. Buning uchun MainActivity interfeysini belgilaydigan activity_main.xml faylini o'zgartiramiz:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/list_fragment"
        android:name="com.example.android.fragmentlar.ContentFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Har bir fragment <fragment> elementi yordamida aniqlanadi. Har bir fragment-ning balandligi, kengligi, id va nomi bo'lishi kerak. name(nomi) paketga asosan to'liq sinf nomiga o'rnatiladi: android:name="com.example.android.fragmentlar.ContentFragment"

activity_main.xml layoutiga fragment-dan tashqari biz boshqa elementlarni yoki fragmentlar qo'sha olamiz, ammo bizni kodda biz faqat bitta fragment bilan cheklanib qolamiz.

MainActivity.java faylini o'z holicha qo'yamiz:

package com.example.android.fragmentlar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Butun ilova quyidagicha bo'ladi:

 

Agar biz dasturni ishga tushiradigan bo'lsak, biz activity orqali amalga oshirgan interfeysni ko'rishimiz mumkin, faqat bizni holatda interfeys fragment-da aniqlanadi:

 

Kodda fragment qo'shish.

Interfeysning xml -faylida fragmentni aniqlashdan tashqari, biz uni activity-ga dinamik ravishda qo'shishimiz mumkin.

Buning uchun activity_main.xml faylini o'zgartiramiz:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Va albatta MainActivity.java faylini quyidagicha o'zgartiramiz:

package com.example.android.fragmentlar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new ContentFragmant()).commit();
        }
    }
}

getSupportFragmentManager() metodi fragmentlarni boshqaradigan FragmentManager ob'ektini qaytaradi.

FragmentManager ob'ekti FragmentTransaction ob'ekti yaratish uchun beginTransaction() metodidan foydalanadi.

FragmentTransaction ikkita metodni bajaradi: add() va commit(). add() metodi fragment-ni qo'shadi: add(R.id.container, new ContentFragmant()) - birinchi parametr fragment qo'shilishi kerak bo'lgan layout resursi. Va commit() metodi qo'shish amalini bajaradi va yakunlaydi.

Shundan so'ng, ilovani ishga tushirsangiz, oldingidek ishlaydi. Ya'ni o'zgarmaydi.

 

Sizlarga ulashmoqchi bo'lgan ma'lumotlarim shulardan iborat. Ko'proq o'rganish uchun havola:

FRAGMENT

 

"ONE MILLION UZBEK CODERS" BILAN YANGI MARRALARNI ZABT ETING!