2012-11-19 9 views
18

पर काम नहीं कर रहा है मेरे पास ListFragment उपयोगकर्ता द्वारा बनाई गई वस्तुओं की एक सूची प्रदर्शित करता है। मेरे पास ListView है जिसका आईडी "@android: id/list" और TextView पर "@android: id/blank" आईडी के साथ सेट है।FragmentTransaction.replace()

मेरे पास सूची के लिए अधिक प्रविष्टियां बनाने की अनुमति देने के लिए एक खंड प्रदर्शित करने के लिए एक एक्शन बार बटन है। यहाँ onOptionsItemSelected() विधि है: इस प्रकार

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    Fragment frag = null; 

    // Right now I only have code for the addCourse() fragment, will add more soon 
    switch (item.getItemId()) { 
    case R.id.addCourse: 
     frag = new AddCourseFragment(); 
     break; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 

    // The support library is being dumb and replace isn't actually 
    getFragmentManager().beginTransaction() 
     .remove(this).add(getId(), frag).addToBackStack(null).commit(); 
    return true; 

} 

AddCourseFragment कोड है:

public class AddCourseFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_addcourse, container, false); 
//  if (view.findViewById(R.id.timesFrame)!=null) { 
     Fragment timesFrag = new TimesFragment(); 
     getChildFragmentManager().beginTransaction() 
      .add(R.id.timesFrame, timesFrag).commit(); 
//  } 
    return view; 
    } 
} 

जैसी उम्मीद थी, जब सूची आबादी है, एंड्रॉयड TextView में पाठ को दर्शाता है। मैं फिर ऐड कोर्स बटन दबाता हूं और ऐसा होता है: enter image description here
यह खाली टेक्स्ट के साथ-साथ नया टुकड़ा दिखाता है।

उत्तर

20

मुझे एक ही समस्या थी।

this के अनुसार जब आपको Fragments प्रोग्रामेटिक रूप से जोड़ने की आवश्यकता है, तो आपको उन्हें मौजूदा ViewGroup में जोड़ना चाहिए।

इसलिए मैंने Fragment को एक्सएमएल से हटा दिया और replace() विधि में FrameLayout घटक का उपयोग किया।

आप here देख सकते हैं।

उम्मीद है कि इससे मदद मिलती है।

2

एक अन्य समस्या जो मेरे मामले में इस मुद्दे को हल करती है, अलग-अलग एपीआई आयात थे।

सुनिश्चित करें कि आप "support.v4" या API 11 से "मूल" कार्यान्वयन का उपयोग करते हैं और उन्हें मिश्रण नहीं करते हैं। (यह मेरे मामले में, हो सकता है: TransactionManager v4 था और गतिविधि एक पुराने संस्करण, या दूसरी तरह के आसपास)

0

एक अन्य समाधान टुकड़ा करने के बजाय LinearLayout का उपयोग इस

तरह

<Fragment 
     android:name="path/to.package.MyClass" 
     android:id="@+id/fragmentContainer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="100dp" 
     /> 

उपयोग यह है

<LinearLayout 
     android:name="path/to.package.MyClass" 
     android:id="@+id/fragmentContainer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="100dp" 
     /> 
3

बच्चे के मुख्य लेआउट में पृष्ठभूमि रंग दें।

एंड्रॉयड की तरह: पृष्ठभूमि = "# FFFFFF"

यह मेरे लिए काम किया !!!!

0

मैंने लाइनरलाउट के लिए रिलेवेटिवआउट को बदलने का हल किया ... उम्मीद है कि इससे मदद मिलती है!

<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="@color/windowBackground"> 


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


</LinearLayout> 
संबंधित मुद्दे