2017-07-25 75 views
1

類似以下內容:我們可以在Databinding的xml佈局中獲得Root View嗎?

android:onClick="@{() -> presenter.onClick(rootView)}"

的源代碼:

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

    <data> 
     <variable 
      name="presenter" 
      type="Presenter" /> 
    </data> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:onClick="@{() -> presenter.onClick(rootView)}" 
      android:text="abcde" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="tytyt" /> 
    </LinearLayout> 
</layout> 
+0

你所說的'rootView'意思?您可以使用'binding.getRoot()'獲得佈局的視圖對象。讓我知道你是不是在尋找這個。 –

+0

我想從xml中得到它,而不是從代碼 – Sam

回答

1

是的,你可以通過它的ID

android:onClick="@{() -> presenter.onClick(viewId)}" 

得到perticular鑑於你的情況,你可以做

<LinearLayout android:id="@+id/parentView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="@{() -> presenter.onClick(parentView)}" 
     android:text="abcde" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="tytyt" /> 
</LinearLayout> 

和你的onClick會

public void onClick(View view) or public void onClick(LinearLayout layout) 
{ 

} 
+0

我記得前一段時間嘗試這個,但它沒有工作,但它現在似乎工作,所以你的答案應該被標記爲接受和使用,因爲它更接近於什麼最初希望的線程起動器。誤導人。 – Dokuzov

+0

多數民衆贊成在好:)因爲總是有多種方法到達目的地。 –

+1

事實上,無論你的問題更清楚,因爲提供根的邏輯將不是必需的。這是前進的方向!乾杯 – Dokuzov

相關問題