2016-11-05 49 views
1

這裏是我的BindingAdapter的Android`@ BindingAdapter`制定者不會被調用

public class Bindings{ 
    @BindingAdapter({"font"}) 
    public static void setFont(TextView textView, String fontName) { 
     textView.setTypeface(FontCache.getInstance(textView.getContext()).get(fontName)); 
    } 
} 

*而不是使用 「字體」 作爲註解的參數,我試過的 「綁定:字體」,「機器人:字體」和‘應用程序:字體’,並提出在佈局中所有相應的變化,但BindingAdapter仍然不叫

這裏就是我使用BindingAdapter(bind_layout.xml)的佈局:

<?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></data> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@null" 
      font="@{`fontawesome-webfront`}" 
      android:text="@string/double_left"/> 
    </LinearLayout> 
</layout> 

*此佈局包括在活動的佈局,使用DatabindingUtils.setContentView

在此處設置是其佈局包括bind_layout.xml活動:

public class ACreateSemester extends AppCompatActivity { 
    private List<CreateItemView> mCreateItemViews; 
    private LinearLayout mItemContainer; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     DataBindingUtil.setContentView(this,R.layout.a_create_items); 
     mItemContainer = (LinearLayout) findViewById(R.id.item_container); 
     mItemContainer.addView(new CreateItemView(this, Item.getDefaultItem())); 
    } 

} 

這三個文件,我在這裏引用其全部列出。

我知道BindingAdapter沒有被調用的方式是因爲我在方法和主體上設置了斷點,並且斷點永遠不會到達。

任何想法爲什麼BindingAdapter沒有發射?

+0

您是否嘗試在'setFont'方法中放置斷點? – Darwind

+0

@Dwindwind先生。這就是我知道它沒有被調用的原因。做出了適當的編輯來重新表達。 – shoe

+0

嘗試:'@BindingAdapter({「bind:font」})'和'bind:font =「@ {'fontawesome-webfront'}」'? – pskink

回答

0

我想你錯過了將視圖與viewModel連接起來的活動。

public class ACreateSemester extends AppCompatActivity { 
private List<CreateItemView> mCreateItemViews; 
private LinearLayout mItemContainer; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ACreateItemsBinding binding = DataBindingUtil.setContentView(this,R.layout.a_create_items); 
    mItemContainer = (LinearLayout) findViewById(R.id.item_container); 
    mItemContainer.addView(new CreateItemView(this,Item.getDefaultItem())); 
    binding.setView(YourViewHere); 
}