2017-05-29 18 views
0

我是android數據綁定中的newbiew,我想在數據綁定佈局中使用泛型類型, 是否有可能請告訴我?使用泛型的Android數據綁定java

JAVA活動

public class SearchableActivity<R> extends ErpActivity<R extends MasterPojo> 
{ 

    public void callToBack(R r){ 
    } 
} 

XML佈局

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 

<data> 

    <variable 
     name="task" 
     type="R extends Masterpojo" /> 

    <variable 
     name="activity" 
     type="com.cloud9.erp.activities.SearchableActivity<R>" /> 


</data> 

<TextView 
    style="@style/valued_style" 
    android:onClick="@{() -> activity.callToBack(task)}" 
    android:text="Erp" 
    android:textColor="@android:color/black" 
    android:textSize="@dimen/fab_margin" /> 
</layout> 
+0

爲什麼你在你的活動中放置泛型? –

+0

因爲它用於多個列表使用不同的模型 –

回答

1

是的,它`可以做你的泛型類型景觀:

<data> 
    <variable 
     name="activity" 
     type="com.cloud9.erp.activities.SearchableActivity&lt;R>" /> 
</data> 

請記住,Android Studio會仍然顯示「無法解析符號」錯誤,但您的佈局文件將被編譯。

此外,這是不是做到這一點的最好的實現,你可以使用處理這類操作:

public class MyHandlers { 
    public void onClickFriend(View view) { ... } 
} 

,然後用它在你的XML:

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 
    <data> 
     <variable name="handlers" type="com.example.Handlers"/> 
     <variable name="user" type="com.example.User"/> 
    </data> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@{user.firstName}" 
      android:onClick="@{handlers::onClickFriend}"/> 
    </LinearLayout> 
</layout> 
+0

thanx兄弟,你能告訴我如何在xml中使用泛型typr就像這樣 –

+0

我也有一個quiestion,請檢查我編輯的quiestion –