6

के साथ डेटा बाइंडिंग मैंने , Fragment और RecyclerView में लागू किया है। अब इसे Dialog में करने की कोशिश कर रहा है, लेकिन इसके अंदर कस्टम दृश्य को सेट करने के तरीके के बारे में थोड़ा उलझन में है?एंड्रॉइड डायलॉग

यहां कोड है जिसे मैंने Dialog के लिए लागू किया है।

Dialog dialog = new Dialog(context); 
dialog.getWindow(); 

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 

LayoutTermsBinding termsBinding; 

dialog.setContentView(R.layout.layout_terms); 
dialog.getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

dialog.show(); 

मैं जानता हूँ कि अगर यह Activity है हम DataBindingUtil.setContentView() प्रदर्शन कर सकते हैं और Fragment के लिए हम DataBindingUtil.inflate() प्रदर्शन कर सकते हैं, लेकिन मैं कैसे DataBinding साथ dialog.setContentView(R.layout.layout_terms); कन्वर्ट करने के लिए के बारे में भ्रमित कर रहा हूँ। आप अपने Binding प्राप्त करना होगा

<layout> 
    <data> 
     <!--You don't even need to use this one, this is important/necessary for the inflate method --> 
     <variable name="testVariable" value="String" /> 
    </data> 
    <LinearLayout> 
     <TextView /> 
    </LinearLayout> 
</layout> 

पहले,:

+1

दिलचस्प सवाल। – pRaNaY

+0

आप डायलॉगफ्रैगमेंट का उपयोग क्यों नहीं करते? –

उत्तर

4

इस तरह मान लिया जाये कि कुछ अपने layout_terms.xml है। यह बस इसे बढ़ा-चढ़ाकर द्वारा किया जाता है:

/* 
* This will only work, if you have a variable or something in your 'layout' tag, 
* maybe build your project beforehand. Only then the inflate method can be found. 
* context - the context you are in. The binding is my activities binding. 
* You can get the root view somehow else. 
*/ 
LayoutTermsBinding termsBinding = LayoutTermsBinding 
    .inflate(LayoutInflater.from(context), (ViewGroup) binding.getRoot(), false); 

//without a variable this would be 
LayoutTermsBinding termsBinding = DataBindingUtil. 
     inflate(LayoutInflater.from(context), R.layout.layout_terms, (ViewGroup) mainBinding.getRoot(), false); 

दूसरा कदम: अपनी termsBinding.getRoot() रूप ContentView:

dialog.setContentView(termsBinding.getRoot()); 

और बस हो गया। :)

+0

हां ने काम किया, धन्यवाद। –

+0

थोड़ा बदलाव है, आप इसे भूल सकते हैं, मैंने अपना जवाब जोड़ा है। –

+0

हालांकि अगर मैंने अपना विचार शामिल किया है तो आपका जवाब बिल्कुल सही है, लेकिन यहां मैं संवाद का उपयोग कर रहा हूं ताकि यह एक और एक्सएमएल फ़ाइल में हो और इसमें मेरे मुख्य एक्सएमएल में शामिल न हो। –

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