खोज…


टिप्पणियों

एंड्रॉइड ऐप में उपयोगकर्ता इंटरफ़ेस के टुकड़े बहुत महत्वपूर्ण घटक हैं। उन्हें पहले एंड्रॉइड 3.0 (हनीकॉम्ब) एपीआई में पेश किया गया था।


फ्रेगमेंट्स के डिजाइन प्रतिमान को समझना

टैबलेट जैसे बड़े स्क्रीन उपकरणों पर मुख्य रूप से मॉड्यूलर और लचीले यूआई का समर्थन करने के लिए टुकड़े पेश किए गए थे।

टुकड़े एक गतिविधि द्वारा प्रबंधित किए जाते हैं। आमतौर पर प्रत्येक टुकड़ा एक स्क्रीन के एक हिस्से का प्रतिनिधित्व करता है। एक गतिविधि में एक से अधिक टुकड़े हो सकते हैं। टुकड़े को उप-गतिविधियाँ भी कहा जा सकता है । जब आप अपनी गतिविधि लेआउट के एक हिस्से के रूप में एक टुकड़ा जोड़ते हैं, तो यह गतिविधि के दृश्य पदानुक्रम के अंदर एक व्यूग्रुप में रहता है और टुकड़ा अपने स्वयं के लेआउट को परिभाषित करता है।


जीवन चक्र

गतिविधियों की तरह, टुकड़ों में भी जीवनचक्र होता है। एक टुकड़ा निम्नलिखित घटनाओं के लिए अधिसूचित हो जाता है।

  1. गतिविधि से जुड़े रहें - onAachach (गतिविधि)
  2. टुकड़ा बनाएँ - onCreate (बंडल)
  3. व्यू बनाएँ - onCreateView (लेआउटइन्फ़्लैटर, व्यूग्रुप, बंडल)
  4. गतिविधि निर्माण - onActivityCreated (बंडल)
  5. राज्य देखें बहाल - onViewStateRestored (बंडल)
  6. उपयोगकर्ता के लिए दृश्यमान बनाया - onStart ()
  7. उपयोगकर्ता इंटरैक्शन की शुरुआत - onResume ()
  8. उपयोगकर्ता बातचीत का विराम - onPause ()
  9. उपयोगकर्ता के लिए अदृश्य बनाया - onStop ()
  10. विनाश देखें - onDestroyView ()
  11. टुकड़े को नष्ट करें - onDestroy ()
  12. एक गतिविधि से अलग हो जाओ - onDachach ()

एक प्रोग्रामर के रूप में, आपको विभिन्न जीवनचक्र कॉलबैक विधियों को ओवरराइड करना चाहिए, आमतौर पर हम ऑनक्रिएट (), ऑनक्रिएट व्यू () और ऑनपॉज () तरीकों को लागू करते हैं।


सुगंध के उपवर्ग

  1. DialogFragment - फ्लोटिंग डायलॉग प्रदर्शित करने के लिए
  2. ListFragment - मदों की सूची प्रदर्शित करने के लिए
  3. वरीयता - सेटिंग गतिविधि बनाने के लिए उपयोगी

संदर्भ

  1. https://developer.android.com/guide/components/fragments.html
  2. https://developer.android.com/reference/android/app/Fragment.html

टुकड़े जोड़ना

स्टैगमेंट को स्टैटिकली जोड़ना

फ़ाइल: activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" >  
  
    <fragment  
        android:id="@+id/fragment2"  
        android:name="com.example.fragmentexample.Fragment2"  
        android:layout_width="0px"  
        android:layout_height="match_parent"   
        android:layout_weight="1"  
        />  
  
    <fragment  
        android:id="@+id/fragment1"  
        android:name="com.example.fragmentexample.Fragment1"  
        android:layout_width="0px"  
        android:layout_height="match_parent"  
        android:layout_weight="1"  
         />  
  
</LinearLayout>  

फ़ाइल: fragment1.xml

<?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"  
    android:background="#00ff00"  
     >  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="fragment frist"  
        android:textAppearance="?android:attr/textAppearanceLarge" />  
  
</LinearLayout>  

फ़ाइल: fragment2.xml

<?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"  
    android:background="#0000ff"  
     >  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Second Fragment"  
        android:textAppearance="?android:attr/textAppearanceLarge" />  
  
</LinearLayout>

फ़ाइल: MainActivity.java

package com.example.fragmentexample;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
public class MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
    }  
}  

फ़ाइल: Fragment1.java

package com.example.fragmentexample;  
  
import android.app.Fragment;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
  
public class Fragment1 extends Fragment {  
    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        return inflater.inflate(R.layout.fragment1,container, false);  
    }  
  
}  

फ़ाइल: Fragment2.java

package com.example.fragmentexample;  
  
import android.app.Fragment;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
  
public class Fragment2 extends Fragment {  
      
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        return inflater.inflate(R.layout.fragment2,container, false);  
    }  
  
}  

गतिशील रूप से एक टुकड़ा जोड़ना

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" >  
  
    <FrameLayout  
        android:id="@+id/container1"   
        android:layout_width="0px"  
        android:layout_height="match_parent"   
        android:layout_weight="1"  
        />  
  
    <FrameLayout  
        android:id="@+id/container2"   
        android:layout_width="0px"  
        android:layout_height="match_parent"  
        android:layout_weight="1"  
         />  
  
</LinearLayout>

FrameLayout टुकड़ा कंटेनर के रूप में कार्य कर रहा है।

मेनएक्टिविटी क्लास

फ़ाइल: MainActivity.java

package com.example.fragmentexample;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
public class MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        loadFragment(this, R.id.container1,new Fragment1(),"fragment1");
        loadFragment(this, R.id.container2,new Fragment2(),"fragment2");    
    }  

    public static void loadFragment(Activity activity, int containerId, Fragment fragment, String tag)
    {
        activity.getSupportFragmentManager().beginTransaction().
            replace(containerId, fragment,tag).commitAllowingStateLoss();
    }
}  


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow