10

मैं एक संवाद में एक टुकड़ा जोड़ना चाहता हूं (यह या तो एक संवादप्रवाह या नियमित संवाद हो सकता है)। मैं उसको कैसे करू?एक संवाद में एक टुकड़ा जोड़ना

<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" /> 

यहाँ मेरी मुख्य गतिविधि है::

public class MyDialogFragment extends DialogFragment { 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     MyDialogFragment2 dialog = new MyDialogFragment2(); 
     View v = inflater.inflate(R.layout.news_articles, container, false); 
     getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, dialog).commit(); 
     return v; 
    } 

} 

यहाँ news_article.xml है:

यहाँ मेरी DialogFragment है

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      MyDialogFragment dialog = new MyDialogFragment(); 
      dialog.show(getSupportFragmentManager(), "asdf"); 
     } 
    }); 
} 

लेकिन जब मैंने उसे कोशिश मैं:

No view found for id 0x7f070002 for fragment MyDialogFragment2 

मुझे लगता है कि ऐसा इसलिए है क्योंकि गतिविधि का FragmentManager वह नहीं है जिसे मुझे जोड़ना चाहिए, लेकिन मुझे डायलॉग फ्रैगमेंट में से कोई नहीं मिल रहा है, यह कहां है? ।

+2

'getChildFragmentManager()। StartTransaction() ....' – Luksprog

+0

धन्यवाद, लेकिन यह केवल एपीआई 17 से काम करता है, है ना? – Kalisky

+1

देशी टुकड़ों के साथ हां, क्योंकि उन्हें 4.2 से पेश किया गया था। लेकिन आपके पास हमेशा समर्थन संगतता पैकेज से टुकड़े का विकल्प होता है जो 'getChildFragmentManager()' विधि के साथ काम करता है। – Luksprog

उत्तर

9

जवाब (@Luksprog करने के लिए धन्यवाद) getActivity के बजाय getChildFragmentManager उपयोग कर रहा है() getSupportFragmentManager

यह क्योंकि मैं यहां बताए अनुसार मेरी समर्थन-v4 जार उन्नत करने के लिए, था, मेरे लिए उपलब्ध नहीं था: android.support.v4.app.Fragment: undefined method getChildFragmentManager()

7

संवाद के लिए लेआउट - R.layout.view_with_plus

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.util.me.TestActivity" 
    > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Details"/> 
    <fragment 
     android:layout_toRightOf="@+id/text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/email" 
     class="com.util.me.test.PlusOneFragment" 
     android:layout_centerHorizontal="true"/> 

</RelativeLayout> 

संवाद

दिखाने के लिए कैसे

कक्षा = "com.util.me.test.PlusOneFragment" में खंड एंड्रॉइड स्टूडियो द्वारा उत्पन्न एक प्लसऑनफ्रैगमेंट है।

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