2016-03-16 34 views
6

Mam klasę CustomView z układem databound, który pobiera zmienną. W layoucie zawierającym CustomView chcę przekazać atrybut do CustomView i przekazać go do niestandardowego powiązania. Oto, co mam:Uzyskaj wiązanie z klasy widoku

public class CustomView extends LinearLayout 
{ 
public CustomView(Context inContext, AttributeSet inAttrs) 
{ 
    super(inContext, inAttrs); 

    inflate(inContext, R.layout.custom_view, null); 
} 



@BindingAdapter({"app:variable"}) 
public static void SetVariable(CustomView inCustomView, VariableType inMyVariable) 
{ 
    CustomViewBinding binding = DataBindingUtil.getBinding(inCustomView); 

    binding.setMyVariable(inMyVariable); 
} 
} 

To zawiesza się podczas próby wyodrębnienia powiązania z widoku. Czy to możliwe? Oto ślad stosu:

java.lang.NullPointerException: Attempt to invoke virtual method 'void xxx.databinding.CustomViewBinding.setVariableType(xxx.VariableType)' on a null object reference 
                      at xxx.CustomView.SetDynamicList(CustomView.java:32) 
                      at xxx.MyFragmentBinding.executeBindings(MyFragmentBinding.java:116) 
                      at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:350) 
                      at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:167) 
                      at android.databinding.ViewDataBinding$7.doFrame(ViewDataBinding.java:233) 
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856) 
                      at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                      at android.view.Choreographer.doFrame(Choreographer.java:603) 
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

A jeśli zmienię

DataBindingUtil.getBinding(inCustomView) 

do

DataBindingUtil.bind(inCustomView) 

następnie uzyskać to:

java.lang.IllegalArgumentException: View is not a binding layout 
                      at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:166) 
                      at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:140) 
                      at xxx.CustomView.SetDynamicList(CustomView.java:30) 
        -              at xxx.databinding.MyFragmentBinding.executeBindings(MyFragmentBinding.java:116) 
                      at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:350) 
                      at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:167) 
                      at android.databinding.ViewDataBinding$7.doFrame(ViewDataBinding.java:233) 
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856) 
                      at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                      at android.view.Choreographer.doFrame(Choreographer.java:603) 
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Może to oznaczać, że układ plik nie jest sformatowany do tworzenia kopii danych, b to jest. Ma element layoutu i element danych ze zmiennymi i wszystkim.

+0

Czy możesz przesłać swój ślad stosu? – Submersed

+0

Pod warunkiem, wraz z trochę więcej informacji – KairisCharm

Odpowiedz

15

Musisz powiązać zawyżony widok, aby utworzyć powiązanie danych. W twoim przykładzie wiążesz kontenera układu.

Możesz to zrobić na kilka sposobów. Najprostszym jest związanie go jako część inflacji:

public class CustomView extends LinearLayout 
{ 
    CustomViewBinding mBinding; 
    public CustomView(Context inContext, AttributeSet inAttrs) 
    { 
    super(inContext, inAttrs); 
    LayoutInflater inflater = LayoutInflater.from(inContext); 
    // I assume you want it inflated into this ViewGroup 
    mBinding = CustomViewBinding.inflate(inflater, this, true); 
    } 

    public void setVariable(CustomView inCustomView, VariableType inMyVariable) { 
    mBinding.setVariable(inMyVariable); 
    } 
    ... 
} 

Tak naprawdę nie potrzeba adaptera wiążące, chyba nie chcesz ustawiająca jako część widoku niestandardowego. W tym przypadku będzie trzeba jeszcze sposób, aby uzyskać wiązania, więc trzeba dodać coś takiego:

public CustomViewBinding getBinding() { return mBinding; } 

żeby wiążące prace adaptera.

Jeśli wiesz, że zawartość LinearLayout są wszystko będzie z nadmuchanego widzenia, można użyć adaptera wiążącej takiego:

@BindingAdapter({"app:variable"}) 
public static void setVariable(CustomView inCustomView, VariableType inMyVariable) 
{ 
    if (inCustomView.getChildCount() == 0) { 
     return; 
    } 
    View boundView = inCustomView.getChildAt(0); 
    CustomViewBinding binding = DataBindingUtil.getBinding(boundView); 

    binding.setMyVariable(inMyVariable); 
} 

Jeśli niestandardowy widok nie jest bardzo niestandardowy, można po prostu uwzględnij swój układ bezpośrednio:

<include layout="@layout/custom_view" app:variable="@{myVariableValue}"/> 

Oczywiście musisz przenieść LinearLayout do custom_view.xml.

+0

Hej @George można opisać, jak generowana klasa CustomViewBinding? – Ajay

+0

Załóżmy, że mam klasę CustomViewPager, więc czy powinienem napisać xml dla tego również do generowania klasy CustomViewPagerBinding? – Ajay

+0

Zignoruj ​​tę wiadomość Mam rozwiązać ten problem przez tworzenie dodatkowych xml dla CustomViewPager dzięki :). – Ajay