Frgament - no post

অ্যাপের ভিতরে যখন অনেকগুলো লেআউট দরকার হয় কোন লিস্টভিউ এর ডাঁটাগুলোর পুরো বর্ণনা দেখানোর জন্য তখন লেআউট এর বিকল্প হিসেবে আরেকটি যে ভিউ কম্পোনেন্ট ইউজ করা যায় সেটি হল Fragment  । এইসকল ক্ষেত্রে আলাদা আলাদা লেআউট ইউজ করার চেয়ে Fragment ইউজ করা বেটার এবং দ্রুত কাজ করে । Fragment ইউজ করার ফলে বলা যায় একের অধিক এক্টিভিটি আমরা এক এক্টিভিটির ভিতরে দেখাতে পারি 😊

আমরা যদি একটি উদাহরন দেখি । আপাতত আমরা একদম বেসিক কিছু একটা দেখবো, এরপরে লিস্টভিউ নিয়ে কাজ করবো । ধরা যাক অ্যাপের একটি স্ক্রিনের ভিতর ২টি বাটন আছে , তার নিচেই আমরা আরেকটি ভিউ ইউজ করবো, সেই বাটনে ক্লিক করার ফলে যে কাজগুলো হবে সেগুলো দেখানোর জন্য -


তো নতুন একটি প্রোজেক্ট তৈরি করা যাক । এবার
activity_main.xml  (আমরা ডিজাইন পার্ট আলোচনা করবো নাহ যেগুলো আগে দেখে আসছি ) -

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout 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"  
   android:orientation="vertical"  
   tools:context="com.example.user.fragment.MainActivity">  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="0dp"  
     android:layout_weight="3"  
     android:orientation="horizontal">  
     <Button  
       android:layout_width="0dp"  
       android:layout_height="match_parent"  
       android:id="@+id/Yellow"  
       android:text="Yellow"  
       android:textStyle="bold"  
       android:layout_weight="1"  
       android:layout_margin="8dp"  
       android:background="@color/Yellow"  
       android:onClick="changeFragment"/>  
     <Button  
       android:layout_width="0dp"  
       android:layout_height="match_parent"  
       android:id="@+id/Green"  
       android:text="Green"  
       android:textStyle="bold"  
       android:layout_weight="1"  
       android:layout_margin="8dp"  
       android:background="@color/Green"  
       android:onClick="changeFragment"/>  
   </LinearLayout>  
   <fragment  
     android:id="@+id/fragment"  
     android:layout_width="match_parent"  
     android:layout_height="0dp"  
     android:layout_weight="7"  
     android:layout_margin="8dp"  
     android:name="com.example.user.fragment.yellowFragment">  
   </fragment>  
 </LinearLayout>  

Fragment ভিউ ইউজ করার জন্য যে ভিউটি ইউজ করতে হবে সেটি হল fragment
এখানে name নামে যে ট্যাগটা ইউজ করা হইছে এটার কাজ সম্পর্কে আমরা পরে দেখবো 😊

আমাদের লেআউট ডিজাইন হল । এবার ২টি Fragment লেআউট খুলবো ২ বাটনের ২টি কাজ দেখানোর জন্য -
এজন্য java এর নিচে যে প্রথম ফোল্ডারটা আছে সেখানে রাইট বাটনে ক্লিক করে নিচের ছবির মত Fragment (Blank) এ ক্লিক করে Fragment Name এ যেকোন নাম দিয়ে Finish এ ক্লিক করুন । আমি এভাবে ২ টা Fragment তৈরি করেছি এবং একটির নাম দিয়েছি yellowFragment ও আরেকটির নাম দিয়েছি greenFragment 😊


Yellow বাটনে ক্লিক করলে yellowFragment ওপেন হবে এবং Green বাটনে ক্লিক করলে greenFragment ওপেন হবে । Fragment এর ভিতরে কিছু কাজ করা যাক । প্রথমত Fragment এর কালার একই কালার এর দিলাম আর একটি কাজ করা হল yellowFragment এ ফেসবুকের একটি লোগো এবং greenFragment এ টুইটারের একটি লোগো বসিয়ে দিলাম ।

fragment_yellow.xml -

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context="com.example.user.fragment.yellowFragment"  
   android:background="@color/Yellow">  
   <ImageView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:src="@drawable/facebook"  
     android:layout_gravity="center"/>  
 </FrameLayout>  

fragment_green.xml -

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context="com.example.user.fragment.greenFragment"  
   android:background="@color/Green">  
   <ImageView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:src="@drawable/twitter"  
     android:layout_gravity="center"/>  
 </FrameLayout>  

Fragment গুলোর Activity ক্লাস নিয়ে আপাতত কোন কাজ করলাম নাহ । এবার

MainActivity.java তে কোড করতে হবে -

onCreate ম্যাথডের বাইরে -

   public void changeFragment(View view) {  
     Fragment fragment;  
     FragmentManager fragmentManager = getSupportFragmentManager();  
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();  

changeFragment ম্যাথডটা আমাদের বাটনের এর onClick এর মধ্যে দেয়া নাম। ২টি বাটনেই একই ম্যাথড নাম দেয়া হইছে যাতে একই ফাংশনের ভিতরে গিয়ে কাজ করে ।

Fragment নিয়ে কাজ করার জন্য Fragment এর ইন্সট্যান্স তৈরি করলাম । এরপর আর যে দুইটি ক্লাসের হেল্প নিতে হবে সেগুলো হলো FragmentManager এবং FragmentTransaction । FragmentManager এ getSupportFragmentManager() এর মাধ্যমে আমাদের ইচ্ছামত  Fragment এর ক্লাসের সাহায্যে সাজিয়ে নিতে পারবো, এরপর FragmentTransaction দ্বারা Fragment শো করবে । FragmentManager এবং FragmentTransaction দুইটি বাটনের ক্ষেত্রেই ইউজ করতে হবে ।

এখন আমরা if-else বাটনের মাধ্যমে চেক করবো কোন বাটনটি ইউজার সিলেক্ট করছে -

     if (view == findViewById(R.id.Yellow)){  
       fragment = new yellowFragment();  
       fragmentTransaction.replace(R.id.fragment,fragment);  
       fragmentTransaction.commit();  
     }else {  
       fragment = new greenFragment();  
       fragmentTransaction.replace(R.id.fragment,fragment);  
       fragmentTransaction.commit();  
     }  
   }  

যখন ইউজার yellow বাটন সিলেক্ট করলো তখন yellowFragment() এর অব্জেক্ট তৈরি হবে । এখানে এভাবে অব্জেক্ট তৈরির নিয়মটা পলিমরফিজম এর উদাহরণ । এরপর যদি দেখা যায় green বাটন সিলেক্ট করা তখন সেটি replace হয়ে yellowFragment দেখাবে । যেহেতু আমরা Transaction করছি তাই সেটি commit করে দেয়া ভালো  😊😊 আর হ্যা আমরা যখন অ্যাপটি ওপেন করবো তখন কোন Fragment টি শো করবে সেটিই আমরা activity_main.xml এর fragment ভিউয়ের name ট্যাগের ভিতর সেই লেআউট এর tools:context এর প্যাথ দিয়ে দিয়েছিলাম ।

এবার অ্যাপটি রান করে চেক করুন 😊

MainActivity.java এর ফুল কোড -

 package com.example.user.fragment;  
 import android.support.v4.app.Fragment;  
 import android.support.v4.app.FragmentManager;  
 import android.support.v4.app.FragmentTransaction;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.View;  
 public class MainActivity extends AppCompatActivity {  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   }  
   public void changeFragment(View view) {  
     Fragment fragment;  
     FragmentManager fragmentManager = getSupportFragmentManager();  
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();  
     if (view == findViewById(R.id.Yellow)){  
       fragment = new yellowFragment();  
       fragmentTransaction.replace(R.id.fragment,fragment);  
       fragmentTransaction.commit();  
     }else {  
       fragment = new greenFragment();  
       fragmentTransaction.replace(R.id.fragment,fragment);  
       fragmentTransaction.commit();  
     }  
   }  
 }  
😊😊😊😊😊😊😊

মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

Toggle Button - 10 ( Android Bangla Tutorial)

Spinner View - 11 ( Android Bangla Tutorial)

ImageView ( Android Bangla Tutorial - )