2016-02-23 3 views
13

मैं कस्टम दृश्य (एक संभव उपयोग जॉर्ज माउंट दिखाया here) के साथ डेटा बाइंडिंग का उपयोग करने की कोशिश कर रहा हूँ।एंड्रॉयड डेटा बाइंडिंग <merge> साथ काम नहीं कर रहा विशेषताएँ

एक <merge> टैग के बिना परिसर विचारों के निर्माण की कल्पना नहीं कर सकते हैं। हालांकि, इस स्थिति डेटा बाइंडिंग में विफल रहता है:

MyCompoundView वर्ग:

public class MyCompoundView extends RelativeLayout { 

MyCompoundViewBinding binding; 

public MyCompoundView (Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context); 
} 

private void init(Context context){ 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    binding = MyCompoundViewBinding.inflate(inflater, this, true); 
} 

my_compound_view.xml: app:isGone="@{!data.isViewVisible}" से मैं पूरे परिसर को देखने की दृश्यता को नियंत्रित

<?xml version="1.0" encoding="utf-8"?> 

<layout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    > 

    <data> 
     <variable name="data" type="com.example.MyViewModel"/> 
    </data> 

    <merge 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:isGone="@{!data.isViewVisible}"> 

     <ImageView 
      android:id="@+id/image_image" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      app:imageUrl="@{data.imagePhotoUrl}"/> 

     <!-- tons of other views--> 

    </merge> 

</layout> 

संकलक त्रुटियों के लिए आशा व्यक्त की:

Error:(13) No resource identifier found for attribute 'isGone' in package 'com.example' 
Error:(17, 21) No resource type specified (at 'isGone' with value '@{!data.isViewVisible}'). 

मैं सभी neede है डी @BindingAdapter विधियों। अब मैं FrameLayout से दृश्य प्राप्त करता हूं और <merge> के बजाय <RelativeLayout> का उपयोग करता हूं - और यह काम करता है। लेकिन मेरे पास अतिरिक्त घोंसला वाला लेआउट है।

प्रश्न:merge attrs अनदेखा कर रहे हैं। क्या काम करने का कोई तरीका है?

एंड्रॉयड स्टूडियो 1.5.1 स्थिर

Gradle प्लगइन com.android.tools.build:gradle:1.5.0

+0

यह अगर तुम बजाय का उपयोग सबसे अच्छा है। –

+2

@ रेकबी राजभांदरी, हां, डाटाबेसिंग ' 'टैग के साथ ठीक काम करता है। हालांकि, यह कस्टम व्यू को बढ़ाने के दौरान अतिरिक्त व्यूग्रुप की समस्या का समाधान नहीं करेगा। –

+0

कृपया मेरे प्रश्न पर एक नज़र डालें। मैं टैग और आंतरिक कक्षा का उपयोग कर रहा हूँ। https://stackoverflow.com/q/45143721/5214893 –

उत्तर

8

वहाँ मुद्रास्फीति के बाद कोई मर्ज वस्तु है, इसलिए किसी मर्ज टैग के साथ करने के लिए मान निर्दिष्ट की कोई बात नहीं है। मैं किसी बाध्यकारी टैग के बारे में नहीं सोच सकता जो विलय पर काम करेगा।

आप मूल तत्व को टैग निर्दिष्ट और BindingAdapter का उपयोग आप क्या चाहते हैं कर सकते हैं।

<layout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    > 

    <data> 
     <variable name="data" type="com.example.MyViewModel"/> 
    </data> 

    <merge 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <ImageView 
      app:isGone="@{!data.isViewVisible}" 
      android:id="@+id/image_image" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      app:imageUrl="@{data.imagePhotoUrl}"/> 
     <!-- tons of other views--> 
    </merge> 
</layout> 

आप बाध्यकारी वर्ग के साथ ही कुछ करना चाहते हैं, उसे दृश्य वस्तु को खोजने के लिए DataBindingUtil उपयोग कर सकते हैं।

@BindingAdapter("isGone") 
public static void setGone(View view, boolean isGone) { 
    ViewDataBinding binding = DataBindingUtil.findBinding(view); 
    //... do what you want with the binding. 
} 
+2

सटीक रूप से एंड्रॉइड डेवलपर्स ने इसके बारे में [दस्तावेज़ीकरण] में उल्लेख किया है (https://developer.android.com/topic/libraries/data-binding/index।एचटीएमएल # लेआउट_डेट) – MrOnyszko

+0

क्या आप कुछ कोड के साथ अपनी सलाह विस्तृत कर सकते हैं? धन्यवाद :) –

+2

हैलो, मुझे काम करने के लिए मर्ज टैग नहीं मिल रहा है। यह त्रुटि दिखाता है 'एलिमेंट मर्ज यहां अनुमति नहीं है'। देखें: https://pastebin.com/QhQKYN71 –

0
Android दस्तावेज़ से

(Data Binding Library: Includes)

चर एक शामिल लेआउट के से बाध्यकारी एक विशेषता में आवेदन नाम स्थान और चर नाम का उपयोग करके लेआउट युक्त में पारित किया जा सकता है

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:bind="http://schemas.android.com/apk/res-auto"> 
    <data> 
     <variable name="user" type="com.example.User"/> 
    </data> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <include layout="@layout/name" 
      bind:user="@{user}"/> 
     <include layout="@layout/contact" 
      bind:user="@{user}"/> 
    </LinearLayout> 
</layout> 

में डेटा-बाइंडिंग वस्तु में पारित करने के लिए याद रखें <include> टैग (इस उदाहरण में: bind:[email protected]{user}), और शीर्ष पर xmlns:bind="http://schemas.android.com/apk/res-auto" जोड़ें।

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