11-Ma’ruza Mavzu: Resurslar va fragmentlar. Reja: Resurslar bilan ishlash


Download 345.81 Kb.
bet9/12
Sana23.06.2023
Hajmi345.81 Kb.
#1652619
1   ...   4   5   6   7   8   9   10   11   12
Bog'liq
11-maruza

Fragmentga mantiq qo'shish. Fragman tugmachani belgilaydi. Keling, bu tugmachaga ba'zi harakatlarni qo'shamiz. Buning uchun ContentFragment sinfini o'zgartiramiz:
package com.example.fragmentapp;
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.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.util.Date;
public class ContentFragment extends Fragment {
public ContentFragment(){
super(R.layout.fragment_content);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button updateButton = (Button) view.findViewById(R.id.updateButton);
TextView updateBox = (TextView) view.findViewById(R.id.dateTextView);
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String curDate = new Date().toString();
updateBox.setText(curDate);
}
});
}
}
Bu erda Fragment sinfining onViewCreated usuli bekor qilinadi, u bu qismni ifodalovchi vizual interfeys uchun View ob'ektini yaratgandan so'ng chaqiriladi. Yaratilgan View obyekti birinchi parametr sifatida uzatiladi. Va keyin biz ushbu View ob'ektida, xususan, TextView va Button da maxsus boshqaruv elementlarini olishimiz va ular yordamida ba'zi harakatlarni bajarishimiz mumkin. Bunday holda, tugmani bosish ishlovchisida joriy sana matn maydonida ko'rsatiladi.

Kodga fragment qo'shish. xaml interfeys faylidagi fragmentni aniqlashdan tashqari, biz uni activity ga dinamik ravishda qo'shishimiz mumkin.
Buni amalga oshirish uchun, activity_main.xml faylini o'zgartiramiz:


xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Shuningdek, MainActivity sinfini o'zgartiring:


package com.example.fragmentapp;
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.fragment_container_view, ContentFragment.class, null)
.commit();
}
}
}
getSupportFragmentManager() usuli fragmentlarni boshqaruvchi FragmentManager obyektini qaytaradi.
FragmentManager obyekti FragmentTransaction obyektini yaratish uchun beginTransaction() usulidan foydalanadi.
FragmentTransaction ikkita usulni bajaradi: add() va commit(). add() usuli fragment qo'shadi: add(R.id.fragment_container_view, new ContentFragment())-birinchi argument - bu fragment qo'shilishi kerak bo'lgan markirovka manbai (bu androidx.fragment.app.FragmentContainerView elementida aniqlangan) activity_main.xml). Va commit() usuli qo'shish operatsiyasini tasdiqlaydi va tugatadi.
Fragmentni shu tarzda qo'shishning aniq ta'siri, xuddi siz interfeysni belgilashda FragmentContainerView elementi orqali fragmentni aniq belgilagandek bo'ladi.

4. Fragmentlarning hayot aylanishi.


Har bir fragment sinfi asosiy Fragment sinfidan meros bo'lib o'tadi va bir necha bosqichlardan iborat o'z hayotiy tsikliga ega:

Hayot tsiklining har bir bosqichi Lifecycle.State ro'yxatining doimiylaridan biri bilan tavsiflanadi:
1   ...   4   5   6   7   8   9   10   11   12




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling