2011-07-12 21 views
86

मुझे टुकड़े के अंदर टुकड़े पर काम करने के बारे में मदद चाहिए, वास्तव में मैं बैक बटन दबाकर एक समस्या का सामना कर रहा हूं। आवेदन मुख्य स्क्रीन में प्रत्येक बटन दृश्य पर नए खंड (और उस टुकड़े को किसी अन्य टुकड़े के अंदर शामिल किया गया है) के साथ प्रति बटन दृश्य पर दबाकर दबाया जाता है, गतिशील रूप से जोड़ना/प्रतिस्थापित करना बटन 1 खंड को दबाकर ठीक काम कर रहा है, बटन दबाते समय ऐसा ही होता है, ,टुकड़े टुकड़े के अंदर टुकड़े

"Duplicate id 0x7f05000a, tag null, or parent id 0x7f050009 with 
another fragment for com........ fragmentname" 

का मतलब टुकड़ा या भीतरी टुकड़े पहले से ही जोड़ रहे हैं और मैं उन्हें फिर से जोड़ने के लिए कोशिश कर रहा हूँ किसी को भी विचार कैसे टुकड़ा अंदर और टुकड़ा के साथ काम करने होते हैं: लेकिन अगर मैं बटन दबाएं, एक अपवाद मिला बिना किसी समस्या के पीछे और आगे बढ़ना,के लिए धन्यवादसमर्थन।

मुख्य गतिविधि, जहां टुकड़े गतिशील जोड़े गए हैं और प्रतिस्थापित किया गया है। main.xml

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

    <LinearLayout android:id="@+id/linearLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
      <Button android:layout_width="wrap_content" 
        android:id="@+id/button1" 
        android:layout_height="wrap_content" 
        android:text="Button1"></Button> 
      <Button android:text="Button2" 
        android:id="@+id/button2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"></Button> 
      <Button android:text="Button3" 
        android:id="@+id/button3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"></Button> 
      <Button android:text="Button4" 
        android:id="@+id/button4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"></Button> 
    </LinearLayout> 

    <LinearLayout android:id="@+id/linearLayout2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"></LinearLayout> 
    </LinearLayout> 

आशा मैं मेरी समस्या स्पष्ट करने की कोशिश की:

public class FragmentInsideFragmentTestActivity extends Activity { 

    private Button button1; 
    private Button button2; 
    private Button button3; 
    private Button button4; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    button1 =(Button) this.findViewById(R.id.button1); 
    button1.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      onButtonClick(view); 

      } 
    }); 

    button2 =(Button) this.findViewById(R.id.button2); 
    button2.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      onButtonClick(view); 

      } 
    }); 

    button3 =(Button) this.findViewById(R.id.button3); 
    button3.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      onButtonClick(view); 

      } 
    }); 

    button4 =(Button) this.findViewById(R.id.button4); 
    button4.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      onButtonClick(view); 

      } 
    }); 

    } 

    public void onButtonClick(View v) { 
      Fragment fg; 
      switch (v.getId()) { 
      case R.id.button1: 
        fg=FirstFragment.newInstance(); 
        replaceFragment(fg); 
        break; 
      case R.id.button2: 
        fg=SecondFragment.newInstance(); 
        replaceFragment(fg); 
        break; 
      case R.id.button3: 
        fg=FirstFragment.newInstance(); 
        replaceFragment(fg); 
        break; 
      case R.id.button4: 
        fg=SecondFragment.newInstance(); 
        replaceFragment(fg); 
        break; 
      } 
    } 

    private void replaceFragment(Fragment newFragment) { 

    FragmentTransaction trasection = 
    getFragmentManager().beginTransaction(); 
    if(!newFragment.isAdded()){ 
      try{ 
        //FragmentTransaction trasection = 
      getFragmentManager().beginTransaction(); 
      trasection.replace(R.id.linearLayout2, newFragment); 
      trasection.addToBackStack(null); 
      trasection.commit(); 

      }catch (Exception e) { 
          // TODO: handle exception 
        //AppConstants.printLog(e.getMessage()); 

        } 
    }else 
      trasection.show(newFragment); 

     } 
    } 

यहाँ लेआउट है।

+0

कोड से ऊपर एंड्रॉइड 3 के साथ मेरे लिए बिल्कुल ठीक काम कर रहा है।1 – Vivek

+3

अधिक जानकारी के लिए [Fragments के भीतर टुकड़े] के संभावित डुप्लिकेट (http://stackoverflow.com/questions/6847460/fragments-within-fragments) – Vladimir

+0

देखें http://stackoverflow.com/a/11020531/1219456 –

उत्तर

214

AFAIK, टुकड़े अन्य टुकड़े नहीं रख सकते हैं।


अद्यतन

Android समर्थन पैकेज के वर्तमान संस्करण के साथ - getChildFragmentManager() के माध्यम से आप घोंसला टुकड़े कर सकते हैं, - एपीआई स्तर 17 और उच्च पर या देशी टुकड़े। ध्यान दें कि इसका मतलब है कि आपको API स्तर 11-16 पर खंडों के एंड्रॉइड सपोर्ट पैकेज संस्करण का उपयोग करने की आवश्यकता है, क्योंकि उन उपकरणों पर खंडों का मूल संस्करण होने के बावजूद, उस संस्करण में getChildFragmentManager() नहीं है।

+17

प्लेटफ़ॉर्म वास्तव में यहाँ बेकार है। मैंने इसे तीन घंटे डिबग करने में बिताया क्योंकि आंतरिक टुकड़ा पहली बार ठीक होगा, लेकिन स्क्रीन अभिविन्यास परिवर्तन के बाद * गायब हो जाएगा *। कोई अपवाद नहीं, लॉग जानकारी, कुछ भी नहीं। 'GetChildFragmentManager()' पर स्विच करना और आंतरिक खंड (करुणा) से 'setRetainInstance (true)' को हटाकर इसे ठीक किया गया है। मेरे बेकन को फिर से सहेजने के लिए धन्यवाद, @ कॉमन्सवेयर। – Felix

1

मैपफ्रैगमेंट के लिए कोई समर्थन नहीं है, एंड्रॉइड टीम का कहना है कि एंड्रॉइड 3.0 के बाद से यह काम कर रहा है। इस मुद्दे के बारे में अधिक जानकारी http://code.google.com/p/android/issues/detail?id=15347&utm_source=buffer&buffer_share=acc72 लेकिन आप एक फ्रैगमेंट बनाकर ऐसा कर सकते हैं जो MapActivity देता है। यहां एक कोड उदाहरण है। धन्यवाद inazaruk रहे हैं: https://github.com/inazaruk/examples/tree/master/MapFragmentExample

यह कैसे काम करता:

-MainFragmentActivity गतिविधि कि FragmentActivity प्रदान करता है और दो MapFragments होस्ट करता है। -MyMapActivity MapActivity बढ़ाता है और MapView है। -LocalActivityManagerFragment होस्ट LocalActivityManager। -MyMapFragment LocalActivityManagerFragment को बढ़ाता है और TabHost की सहायता से MyMapActivity का आंतरिक उदाहरण बनाता है। यदि आप कोई संदेह है तो कृपया मुझे पता है

10

टुकड़े अन्य टुकड़े के अंदर जोड़ा जा सकता है चलो लेकिन फिर आप जब onDestroyView() माता-पिता टुकड़ा की विधि कहा जाता है हर बार माता-पिता टुकड़ा से निकालने के लिए की आवश्यकता होगी। और फिर इसे पेरेंट फ्रैगमेंट की ऑनक्रेट व्यू() विधि में जोड़ें।

बस इस तरह कार्य करें:

@Override 
    public void onDestroyView() 
    { 
       FragmentManager mFragmentMgr= getFragmentManager(); 
     FragmentTransaction mTransaction = mFragmentMgr.beginTransaction(); 
       Fragment childFragment =mFragmentMgr.findFragmentByTag("qa_fragment") 
     mTransaction.remove(childFragment); 
     mTransaction.commit(); 
     super.onDestroyView(); 
    } 
+3

यह मेरे लिए काम नहीं किया। मेरे पास एक टुकड़ा था जिसने दो बच्चों के विचारों वाले दृश्य को फुलाया था। 'GetArivityMreager()' का उपयोग करके 'एक्टिविटीक्रेटेड()' में दो खंडों को जोड़ा गया, प्रत्येक दृश्य में से एक। यह पहली बार काम किया, लेकिन घूर्णन पर और फिर से शुरू टुकड़ों में से एक दिखाई दे रहा था। व्यवहार 'getChildFragmentManager()' के बजाय सही था। यहां सुझाए गए दृष्टिकोण ने अपवाद फेंक दिया क्योंकि गतिविधि की 'ऑनसेवस्टेंसस्टेट()' पहले से ही बुलाया जा चुका था। 'CommitAllowingStateLoss() 'का उपयोग करके लेनदेन को अपवाद से बचाया लेकिन मूल समस्या को हल नहीं किया। – user1978019

3

आप टुकड़ा करने के लिए FrameLayout जोड़ सकते हैं और एक और टुकड़ा के साथ बदलने जब यह initializes कर सकते हैं।

इस तरह, आप दूसरे खंड को पहले खंड के अंदर होने पर विचार कर सकते हैं।

0

हाय मैंने प्रति फ्रेगमेंट को अलग लेआउट में डालकर इस समस्या को हल किया। और मैंने सिर्फ संबंधित लेआउट को दृश्यमान बनाया और अन्य दृश्यताएं चलीं।

मेरा मतलब है:

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

    <LinearLayout android:id="@+id/linearLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
      <Button android:layout_width="wrap_content" 
        android:id="@+id/button1" 
        android:layout_height="wrap_content" 
        android:text="Button1"></Button> 
      <Button android:text="Button2" 
        android:id="@+id/button2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"></Button> 
      <Button android:text="Button3" 
        android:id="@+id/button3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"></Button> 
      <Button android:text="Button4" 
        android:id="@+id/button4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"></Button> 
    </LinearLayout> 
    <LinearLayout android:layout_width="full_screen" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:id="action_For_Button1" 
       android:visibility="visible"> 
        <Fragment android:layout_width="full_screen" 
           android:layout_height="full_screen" 
           android:id="fragment1" 
             . 
             . 
             . 
      /> 
    </LinearLayout> 

    <LinearLayout android:layout_width="full_screen" 
       android:layout_height="0dp" 
       android:id="action_For_Button1" 
       android:layout_weight="1" 
       android:visibility="gone"> 
         <Fragment android:layout_width="full_screen" 
           android:layout_height="full_screen" 
           android:id="fragment2" 
              . 
              . 
              . 
      /> 
     </LinearLayout> 
        . 
        . 
        . 
     </LinearLayout> 

मैं मान लिया है कि आप अपने पृष्ठ खुल जाएगा के रूप में बटन 1 clicked.You क्लिक action.You पर अपने टुकड़ा के दृश्यताएं नियंत्रित कर सकते हैं संबंधित लेआउट दृश्यमान बना सकते हैं और दूसरों से चले गए और द्वारा फ्रैगमेंट मैनेजर आप अपना टुकड़ा ले सकते हैं। यह दृष्टिकोण मेरे लिए काम करता था। और देखें कि दृश्यता है: चला गया अदृश्य है, और यह लेआउट उद्देश्यों के लिए कोई जगह नहीं लेता है, मुझे लगता है कि यह दृष्टिकोण किसी भी स्थान की समस्या का कारण नहीं बनता है।

पीएस: मैंने बस अपने समाधान कोड को समझाने की कोशिश की है सिंटैक्स गलतियों या अपूर्ण संरचना हो सकती है।

57

के बाद से Android 4.2 (एपीआई 17) नेस्ट टुकड़े http://developer.android.com/about/versions/android-4.2.html#NestedFragments

अन्य टुकड़ा उपयोग getChildFragmentManager अंदर टुकड़ा रखने के लिए उपलब्ध हो जाते हैं()

यह भी समर्थन पुस्तकालय में उपलब्ध है!

7

मैंने इस समस्या को हल किया। आप समर्थन पुस्तकालय और ViewPager का उपयोग कर सकते हैं। अगर आपको इशारा द्वारा स्वाइप करने की आवश्यकता नहीं है तो आप स्वाइपिंग अक्षम कर सकते हैं। तो यहाँ मेरी समाधान में सुधार के लिए कुछ कोड है:

public class TestFragment extends Fragment{ 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.frag, container, false); 
    final ArrayList<Fragment> list = new ArrayList<Fragment>(); 

    list.add(new TrFrag()); 
    list.add(new TrFrag()); 
    list.add(new TrFrag()); 

    ViewPager pager = (ViewPager) v.findViewById(R.id.pager); 
    pager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) { 
     @Override 
     public Fragment getItem(int i) { 
      return list.get(i); 
     } 

     @Override 
     public int getCount() { 
      return list.size(); 
     } 
    }); 
    return v; 
} 
} 

P.S.It परीक्षण के लिए बदसूरत कोड है, लेकिन यह बेहतर बनाता है कि यह संभव है।

P.P.S टुकड़ा ChildFragmentManager अंदर नेस्टेड टुकड़ा में ViewPagerAdapter

1

curently को पारित किया जाना चाहिए, नेस्टेड एक (रों) केवल समर्थन कर रहे हैं यदि वे प्रोग्राम के रूप में उत्पन्न कर रहे हैं! तो इस समय xml लेआउट योजना में कोई नेस्टेड खंड लेआउट समर्थित नहीं है! Nested Fragment

2

getChildFragmentManager उपयोग करें(), लिंक का पालन करें।

उदाहरण:

जनक टुकड़ा:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    rootView = inflater.inflate(R.layout.parent_fragment, container, 
      false); 


    } 

    //child fragment 
    FragmentManager childFragMan = getChildFragmentManager(); 
    FragmentTransaction childFragTrans = childFragMan.beginTransaction(); 
    ChildFragment fragB = new ChildFragment(); 
    childFragTrans.add(R.id.FRAGMENT_PLACEHOLDER, fragB); 
    childFragTrans.addToBackStack("B"); 
    childFragTrans.commit();   


    return rootView; 
} 

जनक लेआउट (parent_fragment.xml):

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



    <FrameLayout 
     android:id="@+id/FRAGMENT_PLACEHOLDER" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 




</LinearLayout> 

बाल टुकड़ा:

public class ChildFragment extends Fragment implements View.OnClickListener{ 

    View v ; 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
          @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     View rootView = inflater.inflate(R.layout.child_fragment, container, false); 


     v = rootView; 


     return rootView; 
    } 



    @Override 
    public void onClick(View view) { 


    } 


} 
3

आप getChildFragmentManager() फ़ंक्शन का उपयोग कर सकते हैं:

35

मुझे कुछ और संदर्भ की आवश्यकता है, इसलिए मैंने यह दिखाने के लिए एक उदाहरण दिया कि यह कैसे किया जाता है।सबसे उपयोगी बात मैं जबकि तैयारी कर पढ़ यह था:

गतिविधि

activity_main.xml

अपनी गतिविधि के लिए एक FrameLayout जोड़े माता पिता धारण करने के लिए टुकड़ा।

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Activity"/> 

    <FrameLayout 
     android:id="@+id/parent_fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="200dp"/> 

</LinearLayout> 

MainActivity.java

लोड माता पिता टुकड़ा और टुकड़ा श्रोताओं को लागू। (fragment communication देखें।)

import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 

public class MainActivity extends AppCompatActivity implements ParentFragment.OnFragmentInteractionListener, ChildFragment.OnFragmentInteractionListener { 

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

     // Begin the transaction 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     ft.replace(R.id.parent_fragment_container, new ParentFragment()); 
     ft.commit(); 
    } 

    @Override 
    public void messageFromParentFragment(Uri uri) { 
     Log.i("TAG", "received communication from parent fragment"); 
    } 

    @Override 
    public void messageFromChildFragment(Uri uri) { 
     Log.i("TAG", "received communication from child fragment"); 
    } 
} 

जनक टुकड़ा

fragment_parent.xml

बच्चे टुकड़ा के लिए एक और FrameLayout कंटेनर जोड़ें।

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Parent fragment"/> 

    <FrameLayout 
     android:id="@+id/child_fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </FrameLayout> 

</LinearLayout> 

ParentFragment.java

onViewCreated में उपयोग getChildFragmentManager बच्चे टुकड़ा स्थापित करने के लिए।

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction;  

public class ParentFragment extends Fragment { 

    private OnFragmentInteractionListener mListener; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_parent, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     Fragment childFragment = new ChildFragment(); 
     FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
     transaction.replace(R.id.child_fragment_container, childFragment).commit(); 
    } 


    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void messageFromParentFragment(Uri uri); 
    } 
} 

बाल टुकड़ा

fragment_child.xml

यहाँ विशेष कुछ भी नहीं है।

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Child fragment"/> 
</LinearLayout> 

ChildFragment.java

यहाँ भी कुछ खास नहीं है, या तो नहीं है।

import android.support.v4.app.Fragment; 

public class ChildFragment extends Fragment { 

    private OnFragmentInteractionListener mListener; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_child, container, false); 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 


    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void messageFromChildFragment(Uri uri); 
    } 
} 

नोट्स

  • समर्थन पुस्तकालय इस्तेमाल किया जा रहा है, ताकि nested fragments Android 4.2 से पहले इस्तेमाल किया जा सकता।
+0

यह मेरे लिए काम कर रहा है। –

1

यह कुछ भी जटिल नहीं है। हम getFragmentManager() का उपयोग यहां नहीं कर सकते हैं। एक टुकड़े के अंदर टुकड़े टुकड़े का उपयोग करने के लिए, हम getChildFragmentManager() का उपयोग करते हैं। बाकी वही होगा।

संबंधित मुद्दे